This commit is contained in:
2024-02-25 08:30:34 +08:00
commit 4947f39e74
273 changed files with 45396 additions and 0 deletions

193
pkg/thumb/builtin.go Normal file
View File

@ -0,0 +1,193 @@
package thumb
import (
"context"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"path/filepath"
"strings"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"github.com/gofrs/uuid"
//"github.com/nfnt/resize"
"golang.org/x/image/draw"
)
func init() {
RegisterGenerator(&Builtin{})
}
// Thumb 缩略图
type Thumb struct {
src image.Image
ext string
}
// NewThumbFromFile 从文件数据获取新的Thumb对象
// 尝试通过文件名name解码图像
func NewThumbFromFile(file io.Reader, name string) (*Thumb, error) {
ext := strings.ToLower(filepath.Ext(name))
// 无扩展名时
if len(ext) == 0 {
return nil, fmt.Errorf("unknown image format: %w", ErrPassThrough)
}
var err error
var img image.Image
switch ext[1:] {
case "jpg", "jpeg":
img, err = jpeg.Decode(file)
case "gif":
img, err = gif.Decode(file)
case "png":
img, err = png.Decode(file)
default:
return nil, fmt.Errorf("unknown image format: %w", ErrPassThrough)
}
if err != nil {
return nil, fmt.Errorf("failed to parse image: %w (%w)", err, ErrPassThrough)
}
return &Thumb{
src: img,
ext: ext[1:],
}, nil
}
// GetThumb 生成给定最大尺寸的缩略图
func (image *Thumb) GetThumb(width, height uint) {
//image.src = resize.Thumbnail(width, height, image.src, resize.Lanczos3)
image.src = Thumbnail(width, height, image.src)
}
// GetSize 获取图像尺寸
func (image *Thumb) GetSize() (int, int) {
b := image.src.Bounds()
return b.Max.X, b.Max.Y
}
// Save 保存图像到给定路径
func (image *Thumb) Save(w io.Writer) (err error) {
switch model.GetSettingByNameWithDefault("thumb_encode_method", "jpg") {
case "png":
err = png.Encode(w, image.src)
default:
err = jpeg.Encode(w, image.src, &jpeg.Options{Quality: model.GetIntSetting("thumb_encode_quality", 85)})
}
return err
}
// Thumbnail will downscale provided image to max width and height preserving
// original aspect ratio and using the interpolation function interp.
// It will return original image, without processing it, if original sizes
// are already smaller than provided constraints.
func Thumbnail(maxWidth, maxHeight uint, img image.Image) image.Image {
origBounds := img.Bounds()
origWidth := uint(origBounds.Dx())
origHeight := uint(origBounds.Dy())
newWidth, newHeight := origWidth, origHeight
// Return original image if it have same or smaller size as constraints
if maxWidth >= origWidth && maxHeight >= origHeight {
return img
}
// Preserve aspect ratio
if origWidth > maxWidth {
newHeight = uint(origHeight * maxWidth / origWidth)
if newHeight < 1 {
newHeight = 1
}
newWidth = maxWidth
}
if newHeight > maxHeight {
newWidth = uint(newWidth * maxHeight / newHeight)
if newWidth < 1 {
newWidth = 1
}
newHeight = maxHeight
}
return Resize(newWidth, newHeight, img)
}
func Resize(newWidth, newHeight uint, img image.Image) image.Image {
// Set the expected size that you want:
dst := image.NewRGBA(image.Rect(0, 0, int(newWidth), int(newHeight)))
// Resize:
draw.BiLinear.Scale(dst, dst.Rect, img, img.Bounds(), draw.Src, nil)
return dst
}
// CreateAvatar 创建头像
func (image *Thumb) CreateAvatar(uid uint) error {
// 读取头像相关设定
savePath := util.RelativePath(model.GetSettingByName("avatar_path"))
s := model.GetIntSetting("avatar_size_s", 50)
m := model.GetIntSetting("avatar_size_m", 130)
l := model.GetIntSetting("avatar_size_l", 200)
// 生成头像缩略图
src := image.src
for k, size := range []int{s, m, l} {
out, err := util.CreatNestedFile(filepath.Join(savePath, fmt.Sprintf("avatar_%d_%d.png", uid, k)))
if err != nil {
return err
}
defer out.Close()
image.src = Resize(uint(size), uint(size), src)
err = image.Save(out)
if err != nil {
return err
}
}
return nil
}
type Builtin struct{}
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 nil, err
}
img.GetThumb(thumbSize(options))
tempPath := filepath.Join(
util.RelativePath(model.GetSettingByName("temp_path")),
"thumb",
fmt.Sprintf("thumb_%s", uuid.Must(uuid.NewV4()).String()),
)
thumbFile, err := util.CreatNestedFile(tempPath)
if err != nil {
return nil, fmt.Errorf("failed to create temp file: %w", err)
}
defer thumbFile.Close()
if err := img.Save(thumbFile); err != nil {
return nil, err
}
return &Result{Path: tempPath}, nil
}
func (b Builtin) Priority() int {
return 300
}
func (b Builtin) EnableFlag() string {
return "thumb_builtin_enabled"
}

93
pkg/thumb/ffmpeg.go Normal file
View File

@ -0,0 +1,93 @@
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(&FfmpegGenerator{})
}
type FfmpegGenerator struct {
exts []string
lastRawExts string
}
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"] {
f.exts = strings.Split(ffmpegOpts["thumb_ffmpeg_exts"], ",")
}
if !util.IsInExtensionList(f.exts, name) {
return nil, fmt.Errorf("unsupported video format: %w", ErrPassThrough)
}
tempOutputPath := filepath.Join(
util.RelativePath(ffmpegOpts["temp_path"]),
"thumb",
fmt.Sprintf("thumb_%s.%s", uuid.Must(uuid.NewV4()).String(), ffmpegOpts["thumb_encode_method"]),
)
tempInputPath := src
if tempInputPath == "" {
// If not local policy files, download to temp folder
tempInputPath = filepath.Join(
util.RelativePath(ffmpegOpts["temp_path"]),
"thumb",
fmt.Sprintf("ffmpeg_%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()
}
// 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,
"-vf", scaleOpt, "-vframes", "1", tempOutputPath)
// Redirect IO
var stdErr bytes.Buffer
cmd.Stdin = file
cmd.Stderr = &stdErr
if err := cmd.Run(); err != nil {
util.Log().Warning("Failed to invoke ffmpeg: %s", stdErr.String())
return nil, fmt.Errorf("failed to invoke ffmpeg: %w", err)
}
return &Result{Path: tempOutputPath}, nil
}
func (f *FfmpegGenerator) Priority() int {
return 200
}
func (f *FfmpegGenerator) EnableFlag() string {
return "thumb_ffmpeg_enabled"
}

99
pkg/thumb/libreoffice.go Normal file
View 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"
}

122
pkg/thumb/pipeline.go Normal file
View File

@ -0,0 +1,122 @@
package thumb
import (
"context"
"errors"
"fmt"
model "github.com/cloudreve/Cloudreve/v3/models"
"github.com/cloudreve/Cloudreve/v3/pkg/util"
"io"
"os"
"path/filepath"
"reflect"
"sort"
"strconv"
)
// Generator generates a thumbnail for a given reader.
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) (*Result, error)
// Priority of execution order, smaller value means higher priority.
Priority() int
// EnableFlag returns the setting name to enable this generator.
EnableFlag() string
}
type Result struct {
Path string
Continue bool
Cleanup []func()
}
type (
GeneratorType string
GeneratorList []Generator
)
var (
Generators = GeneratorList{}
ErrPassThrough = errors.New("pass through")
ErrNotAvailable = fmt.Errorf("thumbnail not available: %w", ErrPassThrough)
)
func (g GeneratorList) Len() int {
return len(g)
}
func (g GeneratorList) Less(i, j int) bool {
return g[i].Priority() < g[j].Priority()
}
func (g GeneratorList) Swap(i, j int) {
g[i], g[j] = g[j], g[i]
}
// RegisterGenerator registers a thumbnail generator.
func RegisterGenerator(generator Generator) {
Generators = append(Generators, generator)
sort.Sort(Generators)
}
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, inputFile, inputSrc, inputName, options)
if errors.Is(err, ErrPassThrough) {
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 != nil && 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 nil, ErrNotAvailable
}
func (p GeneratorList) Priority() int {
return 0
}
func (p GeneratorList) EnableFlag() string {
return ""
}
func thumbSize(options map[string]string) (uint, uint) {
w, h := uint(400), uint(300)
if wParsed, err := strconv.Atoi(options["thumb_width"]); err == nil {
w = uint(wParsed)
}
if hParsed, err := strconv.Atoi(options["thumb_height"]); err == nil {
h = uint(hParsed)
}
return w, h
}

74
pkg/thumb/tester.go Normal file
View File

@ -0,0 +1,74 @@
package thumb
import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"strings"
)
var (
ErrUnknownGenerator = errors.New("unknown generator type")
ErrUnknownOutput = errors.New("unknown output from generator")
)
// TestGenerator tests thumb generator by getting lib version
func TestGenerator(ctx context.Context, name, executable string) (string, error) {
switch name {
case "vips":
return testVipsGenerator(ctx, executable)
case "ffmpeg":
return testFfmpegGenerator(ctx, executable)
case "libreOffice":
return testLibreOfficeGenerator(ctx, executable)
default:
return "", ErrUnknownGenerator
}
}
func testVipsGenerator(ctx context.Context, executable string) (string, error) {
cmd := exec.CommandContext(ctx, executable, "--version")
var output bytes.Buffer
cmd.Stdout = &output
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to invoke vips executable: %w", err)
}
if !strings.Contains(output.String(), "vips") {
return "", ErrUnknownOutput
}
return output.String(), nil
}
func testFfmpegGenerator(ctx context.Context, executable string) (string, error) {
cmd := exec.CommandContext(ctx, executable, "-version")
var output bytes.Buffer
cmd.Stdout = &output
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to invoke ffmpeg executable: %w", err)
}
if !strings.Contains(output.String(), "ffmpeg") {
return "", ErrUnknownOutput
}
return output.String(), nil
}
func testLibreOfficeGenerator(ctx context.Context, executable string) (string, error) {
cmd := exec.CommandContext(ctx, executable, "--version")
var output bytes.Buffer
cmd.Stdout = &output
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("failed to invoke libreoffice executable: %w", err)
}
if !strings.Contains(output.String(), "LibreOffice") {
return "", ErrUnknownOutput
}
return output.String(), nil
}

78
pkg/thumb/vips.go Normal file
View File

@ -0,0 +1,78 @@
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/exec"
"path/filepath"
"strings"
)
func init() {
RegisterGenerator(&VipsGenerator{})
}
type VipsGenerator struct {
exts []string
lastRawExts string
}
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"] {
v.exts = strings.Split(vipsOpts["thumb_vips_exts"], ",")
}
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"])
}
cmd := exec.CommandContext(ctx,
vipsOpts["thumb_vips_path"], "thumbnail_source", "[descriptor=0]", outputOpt, options["thumb_width"],
"--height", options["thumb_height"])
tempPath := filepath.Join(
util.RelativePath(vipsOpts["temp_path"]),
"thumb",
fmt.Sprintf("thumb_%s", uuid.Must(uuid.NewV4()).String()),
)
thumbFile, err := util.CreatNestedFile(tempPath)
if err != nil {
return nil, fmt.Errorf("failed to create temp file: %w", err)
}
defer thumbFile.Close()
// Redirect IO
var vipsErr bytes.Buffer
cmd.Stdin = file
cmd.Stdout = thumbFile
cmd.Stderr = &vipsErr
if err := cmd.Run(); err != nil {
util.Log().Warning("Failed to invoke vips: %s", vipsErr.String())
return nil, fmt.Errorf("failed to invoke vips: %w", err)
}
return &Result{Path: tempPath}, nil
}
func (v *VipsGenerator) Priority() int {
return 100
}
func (v *VipsGenerator) EnableFlag() string {
return "thumb_vips_enabled"
}