feat(thumb): generate and return sidecar thumb

This commit is contained in:
Aaron Liu
2023-04-07 19:26:39 +08:00
parent 7cb5e68b78
commit 62b73b577b
12 changed files with 130 additions and 52 deletions

View File

@@ -12,7 +12,7 @@ import (
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"
)
@@ -156,14 +156,30 @@ func (image *Thumb) CreateAvatar(uid uint) error {
type Builtin struct{}
func (b Builtin) Generate(file io.Reader, w io.Writer, name string, options map[string]string) error {
func (b Builtin) Generate(file io.Reader, name string, options map[string]string) (string, error) {
img, err := NewThumbFromFile(file, name)
if err != nil {
return err
return "", err
}
img.GetThumb(thumbSize(options))
return img.Save(w)
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 "", fmt.Errorf("failed to create temp file: %w", err)
}
defer thumbFile.Close()
if err := img.Save(thumbFile); err != nil {
return "", err
}
return tempPath, nil
}
func (b Builtin) Priority() int {

View File

@@ -12,7 +12,7 @@ import (
// Generator generates a thumbnail for a given reader.
type Generator interface {
Generate(file io.Reader, w io.Writer, name string, options map[string]string) error
Generate(file io.Reader, name string, options map[string]string) (string, error)
// Priority of execution order, smaller value means higher priority.
Priority() int
@@ -51,19 +51,19 @@ func RegisterGenerator(generator Generator) {
sort.Sort(Generators)
}
func (p GeneratorList) Generate(file io.Reader, w io.Writer, name string, options map[string]string) error {
func (p GeneratorList) Generate(file io.Reader, name string, options map[string]string) (string, error) {
for _, generator := range p {
if model.IsTrueVal(options[generator.EnableFlag()]) {
err := generator.Generate(file, w, name, options)
res, err := generator.Generate(file, name, options)
if errors.Is(err, ErrPassThrough) {
util.Log().Debug("Failed to generate thumbnail for %s: %s, passing through to next generator.", name, err)
continue
}
return err
return res, err
}
}
return ErrNotAvailable
return "", ErrNotAvailable
}
func (p GeneratorList) Priority() int {