Skip to content

Commit

Permalink
fix: 缩略图生成器中 exts 被多次解析(#2105) (#2106)
Browse files Browse the repository at this point in the history
  • Loading branch information
PublicPwd authored Jul 30, 2024
1 parent 3edb00a commit 23d009d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 34 deletions.
29 changes: 19 additions & 10 deletions pkg/thumb/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -24,27 +25,35 @@ 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) {
return nil, fmt.Errorf("unsupported video format: %w", ErrPassThrough)
}

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)),
)
Expand All @@ -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
Expand Down
30 changes: 19 additions & 11 deletions pkg/thumb/libreoffice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -24,18 +25,25 @@ 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) {
return nil, fmt.Errorf("unsupported document format: %w", ErrPassThrough)
}

tempOutputPath := filepath.Join(
util.RelativePath(sofficeOpts["temp_path"]),
util.RelativePath(sofficeOpts[tempPath]),
"thumb",
fmt.Sprintf("soffice_%s", uuid.Must(uuid.NewV4()).String()),
)
Expand All @@ -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)),
)
Expand All @@ -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
Expand All @@ -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) }},
Expand Down
35 changes: 22 additions & 13 deletions pkg/thumb/vips.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -23,32 +24,40 @@ 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) {
return nil, fmt.Errorf("unsupported image format: %w", ErrPassThrough)
}

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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit 23d009d

Please sign in to comment.