From 23d009d611750ddb6bc2ed5e34dffbd2cf170bdb Mon Sep 17 00:00:00 2001 From: sam Date: Tue, 30 Jul 2024 19:52:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BC=A9=E7=95=A5=E5=9B=BE=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=99=A8=E4=B8=AD=20exts=20=E8=A2=AB=E5=A4=9A?= =?UTF-8?q?=E6=AC=A1=E8=A7=A3=E6=9E=90(#2105)=20(#2106)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/thumb/ffmpeg.go | 29 +++++++++++++++++++---------- pkg/thumb/libreoffice.go | 30 +++++++++++++++++++----------- pkg/thumb/vips.go | 35 ++++++++++++++++++++++------------- 3 files changed, 60 insertions(+), 34 deletions(-) diff --git a/pkg/thumb/ffmpeg.go b/pkg/thumb/ffmpeg.go index d98f107..45814b3 100644 --- a/pkg/thumb/ffmpeg.go +++ b/pkg/thumb/ffmpeg.go @@ -4,14 +4,15 @@ import ( "bytes" "context" "fmt" - model "github.com/cloudreve/Cloudreve/v3/models" - "github.com/cloudreve/Cloudreve/v3/pkg/util" - "github.com/gofrs/uuid" "io" "os" "os/exec" "path/filepath" "strings" + + model "github.com/cloudreve/Cloudreve/v3/models" + "github.com/cloudreve/Cloudreve/v3/pkg/util" + "github.com/gofrs/uuid" ) func init() { @@ -24,10 +25,18 @@ type FfmpegGenerator struct { } func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (*Result, error) { - ffmpegOpts := model.GetSettingByNames("thumb_ffmpeg_path", "thumb_ffmpeg_exts", "thumb_ffmpeg_seek", "thumb_encode_method", "temp_path") + const ( + thumbFFMpegPath = "thumb_ffmpeg_path" + thumbFFMpegExts = "thumb_ffmpeg_exts" + thumbFFMpegSeek = "thumb_ffmpeg_seek" + thumbEncodeMethod = "thumb_encode_method" + tempPath = "temp_path" + ) + ffmpegOpts := model.GetSettingByNames(thumbFFMpegPath, thumbFFMpegExts, thumbFFMpegSeek, thumbEncodeMethod, tempPath) - if f.lastRawExts != ffmpegOpts["thumb_ffmpeg_exts"] { - f.exts = strings.Split(ffmpegOpts["thumb_ffmpeg_exts"], ",") + if f.lastRawExts != ffmpegOpts[thumbFFMpegExts] { + f.exts = strings.Split(ffmpegOpts[thumbFFMpegExts], ",") + f.lastRawExts = ffmpegOpts[thumbFFMpegExts] } if !util.IsInExtensionList(f.exts, name) { @@ -35,16 +44,16 @@ func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, nam } tempOutputPath := filepath.Join( - util.RelativePath(ffmpegOpts["temp_path"]), + util.RelativePath(ffmpegOpts[tempPath]), "thumb", - fmt.Sprintf("thumb_%s.%s", uuid.Must(uuid.NewV4()).String(), ffmpegOpts["thumb_encode_method"]), + fmt.Sprintf("thumb_%s.%s", uuid.Must(uuid.NewV4()).String(), ffmpegOpts[thumbEncodeMethod]), ) tempInputPath := src if tempInputPath == "" { // If not local policy files, download to temp folder tempInputPath = filepath.Join( - util.RelativePath(ffmpegOpts["temp_path"]), + util.RelativePath(ffmpegOpts[tempPath]), "thumb", fmt.Sprintf("ffmpeg_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)), ) @@ -68,7 +77,7 @@ func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, nam // Invoke ffmpeg scaleOpt := fmt.Sprintf("scale=%s:%s:force_original_aspect_ratio=decrease", options["thumb_width"], options["thumb_height"]) cmd := exec.CommandContext(ctx, - ffmpegOpts["thumb_ffmpeg_path"], "-ss", ffmpegOpts["thumb_ffmpeg_seek"], "-i", tempInputPath, + ffmpegOpts[thumbFFMpegPath], "-ss", ffmpegOpts[thumbFFMpegSeek], "-i", tempInputPath, "-vf", scaleOpt, "-vframes", "1", tempOutputPath) // Redirect IO diff --git a/pkg/thumb/libreoffice.go b/pkg/thumb/libreoffice.go index 75e871b..7492613 100644 --- a/pkg/thumb/libreoffice.go +++ b/pkg/thumb/libreoffice.go @@ -4,14 +4,15 @@ import ( "bytes" "context" "fmt" - model "github.com/cloudreve/Cloudreve/v3/models" - "github.com/cloudreve/Cloudreve/v3/pkg/util" - "github.com/gofrs/uuid" "io" "os" "os/exec" "path/filepath" "strings" + + model "github.com/cloudreve/Cloudreve/v3/models" + "github.com/cloudreve/Cloudreve/v3/pkg/util" + "github.com/gofrs/uuid" ) func init() { @@ -24,10 +25,17 @@ type LibreOfficeGenerator struct { } func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src string, name string, options map[string]string) (*Result, error) { - sofficeOpts := model.GetSettingByNames("thumb_libreoffice_path", "thumb_libreoffice_exts", "thumb_encode_method", "temp_path") + const ( + thumbLibreOfficePath = "thumb_libreoffice_path" + thumbLibreOfficeExts = "thumb_libreoffice_exts" + thumbEncodeMethod = "thumb_encode_method" + tempPath = "temp_path" + ) + sofficeOpts := model.GetSettingByNames(thumbLibreOfficePath, thumbLibreOfficeExts, thumbEncodeMethod, tempPath) - if l.lastRawExts != sofficeOpts["thumb_libreoffice_exts"] { - l.exts = strings.Split(sofficeOpts["thumb_libreoffice_exts"], ",") + if l.lastRawExts != sofficeOpts[thumbLibreOfficeExts] { + l.exts = strings.Split(sofficeOpts[thumbLibreOfficeExts], ",") + l.lastRawExts = sofficeOpts[thumbLibreOfficeExts] } if !util.IsInExtensionList(l.exts, name) { @@ -35,7 +43,7 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src } tempOutputPath := filepath.Join( - util.RelativePath(sofficeOpts["temp_path"]), + util.RelativePath(sofficeOpts[tempPath]), "thumb", fmt.Sprintf("soffice_%s", uuid.Must(uuid.NewV4()).String()), ) @@ -44,7 +52,7 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src if tempInputPath == "" { // If not local policy files, download to temp folder tempInputPath = filepath.Join( - util.RelativePath(sofficeOpts["temp_path"]), + util.RelativePath(sofficeOpts[tempPath]), "thumb", fmt.Sprintf("soffice_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)), ) @@ -66,9 +74,9 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src } // Convert the document to an image - cmd := exec.CommandContext(ctx, sofficeOpts["thumb_libreoffice_path"], "--headless", + cmd := exec.CommandContext(ctx, sofficeOpts[thumbLibreOfficePath], "--headless", "-nologo", "--nofirststartwizard", "--invisible", "--norestore", "--convert-to", - sofficeOpts["thumb_encode_method"], "--outdir", tempOutputPath, tempInputPath) + sofficeOpts[thumbEncodeMethod], "--outdir", tempOutputPath, tempInputPath) // Redirect IO var stdErr bytes.Buffer @@ -83,7 +91,7 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src return &Result{ Path: filepath.Join( tempOutputPath, - strings.TrimSuffix(filepath.Base(tempInputPath), filepath.Ext(tempInputPath))+"."+sofficeOpts["thumb_encode_method"], + strings.TrimSuffix(filepath.Base(tempInputPath), filepath.Ext(tempInputPath))+"."+sofficeOpts[thumbEncodeMethod], ), Continue: true, Cleanup: []func(){func() { _ = os.RemoveAll(tempOutputPath) }}, diff --git a/pkg/thumb/vips.go b/pkg/thumb/vips.go index ac43535..b59c13a 100644 --- a/pkg/thumb/vips.go +++ b/pkg/thumb/vips.go @@ -4,13 +4,14 @@ import ( "bytes" "context" "fmt" - model "github.com/cloudreve/Cloudreve/v3/models" - "github.com/cloudreve/Cloudreve/v3/pkg/util" - "github.com/gofrs/uuid" "io" "os/exec" "path/filepath" "strings" + + model "github.com/cloudreve/Cloudreve/v3/models" + "github.com/cloudreve/Cloudreve/v3/pkg/util" + "github.com/gofrs/uuid" ) func init() { @@ -23,10 +24,18 @@ type VipsGenerator struct { } func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (*Result, error) { - vipsOpts := model.GetSettingByNames("thumb_vips_path", "thumb_vips_exts", "thumb_encode_quality", "thumb_encode_method", "temp_path") + const ( + thumbVipsPath = "thumb_vips_path" + thumbVipsExts = "thumb_vips_exts" + thumbEncodeQuality = "thumb_encode_quality" + thumbEncodeMethod = "thumb_encode_method" + tempPath = "temp_path" + ) + vipsOpts := model.GetSettingByNames(thumbVipsPath, thumbVipsExts, thumbEncodeQuality, thumbEncodeMethod, tempPath) - if v.lastRawExts != vipsOpts["thumb_vips_exts"] { - v.exts = strings.Split(vipsOpts["thumb_vips_exts"], ",") + if v.lastRawExts != vipsOpts[thumbVipsExts] { + v.exts = strings.Split(vipsOpts[thumbVipsExts], ",") + v.lastRawExts = vipsOpts[thumbVipsExts] } if !util.IsInExtensionList(v.exts, name) { @@ -34,21 +43,21 @@ func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name } outputOpt := ".png" - if vipsOpts["thumb_encode_method"] == "jpg" { - outputOpt = fmt.Sprintf(".jpg[Q=%s]", vipsOpts["thumb_encode_quality"]) + if vipsOpts[thumbEncodeMethod] == "jpg" { + outputOpt = fmt.Sprintf(".jpg[Q=%s]", vipsOpts[thumbEncodeQuality]) } cmd := exec.CommandContext(ctx, - vipsOpts["thumb_vips_path"], "thumbnail_source", "[descriptor=0]", outputOpt, options["thumb_width"], + vipsOpts[thumbVipsPath], "thumbnail_source", "[descriptor=0]", outputOpt, options["thumb_width"], "--height", options["thumb_height"]) - tempPath := filepath.Join( - util.RelativePath(vipsOpts["temp_path"]), + outTempPath := filepath.Join( + util.RelativePath(vipsOpts[tempPath]), "thumb", fmt.Sprintf("thumb_%s", uuid.Must(uuid.NewV4()).String()), ) - thumbFile, err := util.CreatNestedFile(tempPath) + thumbFile, err := util.CreatNestedFile(outTempPath) if err != nil { return nil, fmt.Errorf("failed to create temp file: %w", err) } @@ -66,7 +75,7 @@ func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name return nil, fmt.Errorf("failed to invoke vips: %w", err) } - return &Result{Path: tempPath}, nil + return &Result{Path: outTempPath}, nil } func (v *VipsGenerator) Priority() int {