mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
feat(thumb): use libreoffice to generate thumb
This commit is contained in:
@@ -34,6 +34,10 @@ func (fs *FileSystem) GetThumb(ctx context.Context, id uint) (*response.ContentR
|
||||
}
|
||||
|
||||
file := fs.FileTarget[0]
|
||||
if !file.ShouldLoadThumb() {
|
||||
return nil, ErrObjectNotExist
|
||||
}
|
||||
|
||||
w, h := fs.GenerateThumbnailSize(0, 0)
|
||||
ctx = context.WithValue(ctx, fsctx.ThumbSizeCtx, [2]uint{w, h})
|
||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, file)
|
||||
@@ -143,21 +147,22 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) e
|
||||
src = file.SourceName
|
||||
}
|
||||
|
||||
thumbPath, err := thumb.Generators.Generate(ctx, source, src, file.Name, model.GetSettingByNames(
|
||||
thumbRes, err := thumb.Generators.Generate(ctx, source, src, file.Name, model.GetSettingByNames(
|
||||
"thumb_width",
|
||||
"thumb_height",
|
||||
"thumb_builtin_enabled",
|
||||
"thumb_vips_enabled",
|
||||
"thumb_ffmpeg_enabled",
|
||||
"thumb_libreoffice_enabled",
|
||||
))
|
||||
if err != nil {
|
||||
_ = updateThumbStatus(file, model.ThumbStatusNotAvailable)
|
||||
return fmt.Errorf("failed to generate thumb for %q: %w", file.Name, err)
|
||||
}
|
||||
|
||||
defer os.Remove(thumbPath)
|
||||
defer os.Remove(thumbRes.Path)
|
||||
|
||||
thumbFile, err := os.Open(thumbPath)
|
||||
thumbFile, err := os.Open(thumbRes.Path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open temp thumb %q: %w", thumbFile, err)
|
||||
}
|
||||
|
||||
@@ -157,10 +157,10 @@ func (image *Thumb) CreateAvatar(uid uint) error {
|
||||
|
||||
type Builtin struct{}
|
||||
|
||||
func (b Builtin) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (string, error) {
|
||||
func (b Builtin) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (*Result, error) {
|
||||
img, err := NewThumbFromFile(file, name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
img.GetThumb(thumbSize(options))
|
||||
@@ -172,15 +172,15 @@ func (b Builtin) Generate(ctx context.Context, file io.Reader, src, name string,
|
||||
|
||||
thumbFile, err := util.CreatNestedFile(tempPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create temp file: %w", err)
|
||||
return nil, fmt.Errorf("failed to create temp file: %w", err)
|
||||
}
|
||||
|
||||
defer thumbFile.Close()
|
||||
if err := img.Save(thumbFile); err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tempPath, nil
|
||||
return &Result{Path: tempPath}, nil
|
||||
}
|
||||
|
||||
func (b Builtin) Priority() int {
|
||||
|
||||
@@ -23,7 +23,7 @@ type FfmpegGenerator struct {
|
||||
lastRawExts string
|
||||
}
|
||||
|
||||
func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (string, 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")
|
||||
|
||||
if f.lastRawExts != ffmpegOpts["thumb_ffmpeg_exts"] {
|
||||
@@ -31,7 +31,7 @@ func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, nam
|
||||
}
|
||||
|
||||
if !util.IsInExtensionList(f.exts, name) {
|
||||
return "", fmt.Errorf("unsupported video format: %w", ErrPassThrough)
|
||||
return nil, fmt.Errorf("unsupported video format: %w", ErrPassThrough)
|
||||
}
|
||||
|
||||
tempOutputPath := filepath.Join(
|
||||
@@ -52,14 +52,14 @@ func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, nam
|
||||
// Due to limitations of ffmpeg, we need to write the input file to disk first
|
||||
tempInputFile, err := util.CreatNestedFile(tempInputPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create temp file: %w", err)
|
||||
return nil, fmt.Errorf("failed to create temp file: %w", err)
|
||||
}
|
||||
|
||||
defer os.Remove(tempInputPath)
|
||||
defer tempInputFile.Close()
|
||||
|
||||
if _, err = io.Copy(tempInputFile, file); err != nil {
|
||||
return "", fmt.Errorf("failed to write input file: %w", err)
|
||||
return nil, fmt.Errorf("failed to write input file: %w", err)
|
||||
}
|
||||
|
||||
tempInputFile.Close()
|
||||
@@ -79,10 +79,10 @@ func (f *FfmpegGenerator) Generate(ctx context.Context, file io.Reader, src, nam
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
util.Log().Warning("Failed to invoke ffmpeg: %s", stdErr.String())
|
||||
return "", fmt.Errorf("failed to invoke ffmpeg: %w", err)
|
||||
return nil, fmt.Errorf("failed to invoke ffmpeg: %w", err)
|
||||
}
|
||||
|
||||
return tempOutputPath, nil
|
||||
return &Result{Path: tempOutputPath}, nil
|
||||
}
|
||||
|
||||
func (f *FfmpegGenerator) Priority() int {
|
||||
|
||||
99
pkg/thumb/libreoffice.go
Normal file
99
pkg/thumb/libreoffice.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package thumb
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterGenerator(&LibreOfficeGenerator{})
|
||||
}
|
||||
|
||||
type LibreOfficeGenerator struct {
|
||||
exts []string
|
||||
lastRawExts string
|
||||
}
|
||||
|
||||
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")
|
||||
|
||||
if l.lastRawExts != sofficeOpts["thumb_libreoffice_exts"] {
|
||||
l.exts = strings.Split(sofficeOpts["thumb_libreoffice_exts"], ",")
|
||||
}
|
||||
|
||||
if !util.IsInExtensionList(l.exts, name) {
|
||||
return nil, fmt.Errorf("unsupported document format: %w", ErrPassThrough)
|
||||
}
|
||||
|
||||
tempOutputPath := filepath.Join(
|
||||
util.RelativePath(sofficeOpts["temp_path"]),
|
||||
"thumb",
|
||||
fmt.Sprintf("soffice_%s", uuid.Must(uuid.NewV4()).String()),
|
||||
)
|
||||
|
||||
tempInputPath := src
|
||||
if tempInputPath == "" {
|
||||
// If not local policy files, download to temp folder
|
||||
tempInputPath = filepath.Join(
|
||||
util.RelativePath(sofficeOpts["temp_path"]),
|
||||
"thumb",
|
||||
fmt.Sprintf("soffice_%s%s", uuid.Must(uuid.NewV4()).String(), filepath.Ext(name)),
|
||||
)
|
||||
|
||||
// Due to limitations of ffmpeg, we need to write the input file to disk first
|
||||
tempInputFile, err := util.CreatNestedFile(tempInputPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create temp file: %w", err)
|
||||
}
|
||||
|
||||
defer os.Remove(tempInputPath)
|
||||
defer tempInputFile.Close()
|
||||
|
||||
if _, err = io.Copy(tempInputFile, file); err != nil {
|
||||
return nil, fmt.Errorf("failed to write input file: %w", err)
|
||||
}
|
||||
|
||||
tempInputFile.Close()
|
||||
}
|
||||
|
||||
// Convert the document to an image
|
||||
cmd := exec.CommandContext(ctx, sofficeOpts["thumb_libreoffice_path"], "--headless",
|
||||
"-nologo", "--nofirststartwizard", "--invisible", "--norestore", "--convert-to",
|
||||
sofficeOpts["thumb_encode_method"], "--outdir", tempOutputPath, tempInputPath)
|
||||
|
||||
// Redirect IO
|
||||
var stdErr bytes.Buffer
|
||||
cmd.Stdin = file
|
||||
cmd.Stderr = &stdErr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
util.Log().Warning("Failed to invoke LibreOffice: %s", stdErr.String())
|
||||
return nil, fmt.Errorf("failed to invoke LibreOffice: %w", err)
|
||||
}
|
||||
|
||||
return &Result{
|
||||
Path: filepath.Join(
|
||||
tempOutputPath,
|
||||
strings.TrimSuffix(filepath.Base(tempInputPath), filepath.Ext(tempInputPath))+"."+sofficeOpts["thumb_encode_method"],
|
||||
),
|
||||
Continue: true,
|
||||
Cleanup: []func(){func() { _ = os.RemoveAll(tempOutputPath) }},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *LibreOfficeGenerator) Priority() int {
|
||||
return 50
|
||||
}
|
||||
|
||||
func (l *LibreOfficeGenerator) EnableFlag() string {
|
||||
return "thumb_libreoffice_enabled"
|
||||
}
|
||||
@@ -7,6 +7,9 @@ import (
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
@@ -15,7 +18,7 @@ import (
|
||||
type Generator interface {
|
||||
// Generate generates a thumbnail for a given reader. Src is the original file path, only provided
|
||||
// for local policy files.
|
||||
Generate(ctx context.Context, file io.Reader, src string, name string, options map[string]string) (string, error)
|
||||
Generate(ctx context.Context, file io.Reader, src string, name string, options map[string]string) (*Result, error)
|
||||
|
||||
// Priority of execution order, smaller value means higher priority.
|
||||
Priority() int
|
||||
@@ -24,6 +27,12 @@ type Generator interface {
|
||||
EnableFlag() string
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Path string
|
||||
Continue bool
|
||||
Cleanup []func()
|
||||
}
|
||||
|
||||
type (
|
||||
GeneratorType string
|
||||
GeneratorList []Generator
|
||||
@@ -54,19 +63,41 @@ func RegisterGenerator(generator Generator) {
|
||||
sort.Sort(Generators)
|
||||
}
|
||||
|
||||
func (p GeneratorList) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (string, error) {
|
||||
func (p GeneratorList) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (*Result, error) {
|
||||
inputFile, inputSrc, inputName := file, src, name
|
||||
for _, generator := range p {
|
||||
if model.IsTrueVal(options[generator.EnableFlag()]) {
|
||||
res, err := generator.Generate(ctx, file, src, name, options)
|
||||
res, err := generator.Generate(ctx, inputFile, inputSrc, inputName, options)
|
||||
if errors.Is(err, ErrPassThrough) {
|
||||
util.Log().Debug("Failed to generate thumbnail for %s: %s, passing through to next generator.", name, err)
|
||||
util.Log().Debug("Failed to generate thumbnail using %s for %s: %s, passing through to next generator.", reflect.TypeOf(generator).String(), name, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if res.Continue {
|
||||
util.Log().Debug("Generator %s for %s returned continue, passing through to next generator.", reflect.TypeOf(generator).String(), name)
|
||||
|
||||
// defer cleanup funcs
|
||||
for _, cleanup := range res.Cleanup {
|
||||
defer cleanup()
|
||||
}
|
||||
|
||||
// prepare file reader for next generator
|
||||
intermediate, err := os.Open(res.Path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open intermediate thumb file: %w", err)
|
||||
}
|
||||
|
||||
defer intermediate.Close()
|
||||
inputFile = intermediate
|
||||
inputSrc = res.Path
|
||||
inputName = filepath.Base(res.Path)
|
||||
continue
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
return "", ErrNotAvailable
|
||||
return nil, ErrNotAvailable
|
||||
}
|
||||
|
||||
func (p GeneratorList) Priority() int {
|
||||
|
||||
@@ -22,7 +22,7 @@ type VipsGenerator struct {
|
||||
lastRawExts string
|
||||
}
|
||||
|
||||
func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name string, options map[string]string) (string, 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")
|
||||
|
||||
if v.lastRawExts != vipsOpts["thumb_vips_exts"] {
|
||||
@@ -30,7 +30,7 @@ func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name
|
||||
}
|
||||
|
||||
if !util.IsInExtensionList(v.exts, name) {
|
||||
return "", fmt.Errorf("unsupported image format: %w", ErrPassThrough)
|
||||
return nil, fmt.Errorf("unsupported image format: %w", ErrPassThrough)
|
||||
}
|
||||
|
||||
outputOpt := ".png"
|
||||
@@ -50,7 +50,7 @@ func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name
|
||||
|
||||
thumbFile, err := util.CreatNestedFile(tempPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create temp file: %w", err)
|
||||
return nil, fmt.Errorf("failed to create temp file: %w", err)
|
||||
}
|
||||
|
||||
defer thumbFile.Close()
|
||||
@@ -63,10 +63,10 @@ func (v *VipsGenerator) Generate(ctx context.Context, file io.Reader, src, name
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
util.Log().Warning("Failed to invoke vips: %s", vipsErr.String())
|
||||
return "", fmt.Errorf("failed to invoke vips: %w", err)
|
||||
return nil, fmt.Errorf("failed to invoke vips: %w", err)
|
||||
}
|
||||
|
||||
return tempPath, nil
|
||||
return &Result{Path: tempPath}, nil
|
||||
}
|
||||
|
||||
func (v *VipsGenerator) Priority() int {
|
||||
|
||||
Reference in New Issue
Block a user