fix: 缩略图生成器中 exts 被多次解析(#2105) (#2106)

pull/2127/head
sam 4 months ago committed by GitHub
parent 3edb00a648
commit 23d009d611
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -4,14 +4,15 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid"
"io" "io"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid"
) )
func init() { 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) { 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"] { if f.lastRawExts != ffmpegOpts[thumbFFMpegExts] {
f.exts = strings.Split(ffmpegOpts["thumb_ffmpeg_exts"], ",") f.exts = strings.Split(ffmpegOpts[thumbFFMpegExts], ",")
f.lastRawExts = ffmpegOpts[thumbFFMpegExts]
} }
if !util.IsInExtensionList(f.exts, name) { 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( tempOutputPath := filepath.Join(
util.RelativePath(ffmpegOpts["temp_path"]), util.RelativePath(ffmpegOpts[tempPath]),
"thumb", "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 tempInputPath := src
if tempInputPath == "" { if tempInputPath == "" {
// If not local policy files, download to temp folder // If not local policy files, download to temp folder
tempInputPath = filepath.Join( tempInputPath = filepath.Join(
util.RelativePath(ffmpegOpts["temp_path"]), util.RelativePath(ffmpegOpts[tempPath]),
"thumb", "thumb",
fmt.Sprintf("ffmpeg_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)), 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 // Invoke ffmpeg
scaleOpt := fmt.Sprintf("scale=%s:%s:force_original_aspect_ratio=decrease", options["thumb_width"], options["thumb_height"]) scaleOpt := fmt.Sprintf("scale=%s:%s:force_original_aspect_ratio=decrease", options["thumb_width"], options["thumb_height"])
cmd := exec.CommandContext(ctx, 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) "-vf", scaleOpt, "-vframes", "1", tempOutputPath)
// Redirect IO // Redirect IO

@ -4,14 +4,15 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid"
"io" "io"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid"
) )
func init() { 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) { 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"] { if l.lastRawExts != sofficeOpts[thumbLibreOfficeExts] {
l.exts = strings.Split(sofficeOpts["thumb_libreoffice_exts"], ",") l.exts = strings.Split(sofficeOpts[thumbLibreOfficeExts], ",")
l.lastRawExts = sofficeOpts[thumbLibreOfficeExts]
} }
if !util.IsInExtensionList(l.exts, name) { if !util.IsInExtensionList(l.exts, name) {
@ -35,7 +43,7 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src
} }
tempOutputPath := filepath.Join( tempOutputPath := filepath.Join(
util.RelativePath(sofficeOpts["temp_path"]), util.RelativePath(sofficeOpts[tempPath]),
"thumb", "thumb",
fmt.Sprintf("soffice_%s", uuid.Must(uuid.NewV4()).String()), 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 tempInputPath == "" {
// If not local policy files, download to temp folder // If not local policy files, download to temp folder
tempInputPath = filepath.Join( tempInputPath = filepath.Join(
util.RelativePath(sofficeOpts["temp_path"]), util.RelativePath(sofficeOpts[tempPath]),
"thumb", "thumb",
fmt.Sprintf("soffice_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)), 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 // 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", "-nologo", "--nofirststartwizard", "--invisible", "--norestore", "--convert-to",
sofficeOpts["thumb_encode_method"], "--outdir", tempOutputPath, tempInputPath) sofficeOpts[thumbEncodeMethod], "--outdir", tempOutputPath, tempInputPath)
// Redirect IO // Redirect IO
var stdErr bytes.Buffer var stdErr bytes.Buffer
@ -83,7 +91,7 @@ func (l *LibreOfficeGenerator) Generate(ctx context.Context, file io.Reader, src
return &Result{ return &Result{
Path: filepath.Join( Path: filepath.Join(
tempOutputPath, 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, Continue: true,
Cleanup: []func(){func() { _ = os.RemoveAll(tempOutputPath) }}, Cleanup: []func(){func() { _ = os.RemoveAll(tempOutputPath) }},

@ -4,13 +4,14 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid"
"io" "io"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid"
) )
func init() { 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) { 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"] { if v.lastRawExts != vipsOpts[thumbVipsExts] {
v.exts = strings.Split(vipsOpts["thumb_vips_exts"], ",") v.exts = strings.Split(vipsOpts[thumbVipsExts], ",")
v.lastRawExts = vipsOpts[thumbVipsExts]
} }
if !util.IsInExtensionList(v.exts, name) { if !util.IsInExtensionList(v.exts, name) {
@ -34,21 +43,21 @@ func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name
} }
outputOpt := ".png" outputOpt := ".png"
if vipsOpts["thumb_encode_method"] == "jpg" { if vipsOpts[thumbEncodeMethod] == "jpg" {
outputOpt = fmt.Sprintf(".jpg[Q=%s]", vipsOpts["thumb_encode_quality"]) outputOpt = fmt.Sprintf(".jpg[Q=%s]", vipsOpts[thumbEncodeQuality])
} }
cmd := exec.CommandContext(ctx, 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"]) "--height", options["thumb_height"])
tempPath := filepath.Join( outTempPath := filepath.Join(
util.RelativePath(vipsOpts["temp_path"]), util.RelativePath(vipsOpts[tempPath]),
"thumb", "thumb",
fmt.Sprintf("thumb_%s", uuid.Must(uuid.NewV4()).String()), fmt.Sprintf("thumb_%s", uuid.Must(uuid.NewV4()).String()),
) )
thumbFile, err := util.CreatNestedFile(tempPath) thumbFile, err := util.CreatNestedFile(outTempPath)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to create temp file: %w", err) 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 nil, fmt.Errorf("failed to invoke vips: %w", err)
} }
return &Result{Path: tempPath}, nil return &Result{Path: outTempPath}, nil
} }
func (v *VipsGenerator) Priority() int { func (v *VipsGenerator) Priority() int {

Loading…
Cancel
Save