diff --git a/models/defaults.go b/models/defaults.go index 339c055..97ef7b8 100644 --- a/models/defaults.go +++ b/models/defaults.go @@ -114,7 +114,9 @@ Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; verti {Name: "thumb_vips_exts", Value: "csv,mat,img,hdr,pbm,pgm,ppm,pfm,pnm,svg,svgz,j2k,jp2,jpt,j2c,jpc,gif,png,jpg,jpeg,jpe,webp,tif,tiff,fits,fit,fts,exr,jxl,pdf,heic,heif,avif,svs,vms,vmu,ndpi,scn,mrxs,svslide,bif,raw", Type: "thumb"}, {Name: "thumb_ffmpeg_path", Value: "ffmpeg", Type: "thumb"}, {Name: "thumb_ffmpeg_exts", Value: "3g2,3gp,asf,asx,avi,divx,flv,m2ts,m2v,m4v,mkv,mov,mp4,mpeg,mpg,mts,mxf,ogv,rm,swf,webm,wmv", Type: "thumb"}, - {Name: "thumb_ffmpeg_seek", Value: "00:00:01.00", Type: "thumb"}, + {Name: "thumb_libreoffice_path", Value: "soffice", Type: "thumb"}, + {Name: "thumb_libreoffice_enabled", Value: "0", Type: "thumb"}, + {Name: "thumb_libreoffice_exts", Value: "md,ods,ots,fods,uos,xlsx,xml,xls,xlt,dif,dbf,html,slk,csv,xlsm,docx,dotx,doc,dot,rtf,xlsm,xlst,xls,xlw,xlc,xlt,pptx,ppsx,potx,pomx,ppt,pps,ppm,pot,pom", Type: "thumb"}, {Name: "thumb_proxy_enabled", Value: "0", Type: "thumb"}, {Name: "thumb_proxy_policy", Value: "[]", Type: "thumb"}, {Name: "thumb_max_src_size", Value: "31457280", Type: "thumb"}, diff --git a/pkg/filesystem/image.go b/pkg/filesystem/image.go index b6ba66e..47ae179 100644 --- a/pkg/filesystem/image.go +++ b/pkg/filesystem/image.go @@ -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) } diff --git a/pkg/thumb/builtin.go b/pkg/thumb/builtin.go index 0fdd771..206d046 100644 --- a/pkg/thumb/builtin.go +++ b/pkg/thumb/builtin.go @@ -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 { diff --git a/pkg/thumb/ffmpeg.go b/pkg/thumb/ffmpeg.go index 004cd27..5ad9944 100644 --- a/pkg/thumb/ffmpeg.go +++ b/pkg/thumb/ffmpeg.go @@ -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 { diff --git a/pkg/thumb/libreoffice.go b/pkg/thumb/libreoffice.go new file mode 100644 index 0000000..75e871b --- /dev/null +++ b/pkg/thumb/libreoffice.go @@ -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" +} diff --git a/pkg/thumb/pipeline.go b/pkg/thumb/pipeline.go index bf5705d..c3c5b7c 100644 --- a/pkg/thumb/pipeline.go +++ b/pkg/thumb/pipeline.go @@ -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 { diff --git a/pkg/thumb/vips.go b/pkg/thumb/vips.go index 2450617..ac43535 100644 --- a/pkg/thumb/vips.go +++ b/pkg/thumb/vips.go @@ -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 {