feat(thumb): set size limit for original file

pull/1690/head
Aaron Liu 2 years ago
parent b910254cc5
commit f5a21a7e6f

@ -114,6 +114,7 @@ Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; verti
{Name: "thumb_ffmpeg_path", Value: "ffmpeg", Type: "thumb"}, {Name: "thumb_ffmpeg_path", Value: "ffmpeg", Type: "thumb"},
{Name: "thumb_proxy_enabled", Value: "0", Type: "thumb"}, {Name: "thumb_proxy_enabled", Value: "0", Type: "thumb"},
{Name: "thumb_proxy_policy", Value: "[]", Type: "thumb"}, {Name: "thumb_proxy_policy", Value: "[]", Type: "thumb"},
{Name: "thumb_max_src_size", Value: "31457280", Type: "thumb"},
{Name: "pwa_small_icon", Value: "/static/img/favicon.ico", Type: "pwa"}, {Name: "pwa_small_icon", Value: "/static/img/favicon.ico", Type: "pwa"},
{Name: "pwa_medium_icon", Value: "/static/img/logo192.png", Type: "pwa"}, {Name: "pwa_medium_icon", Value: "/static/img/logo192.png", Type: "pwa"},
{Name: "pwa_large_icon", Value: "/static/img/logo512.png", Type: "pwa"}, {Name: "pwa_large_icon", Value: "/static/img/logo512.png", Type: "pwa"},

@ -145,7 +145,6 @@ func (handler Driver) Thumb(ctx context.Context, file *model.File) (*response.Co
return nil, errors.New("failed to get thumbnail size") return nil, errors.New("failed to get thumbnail size")
} }
return nil, driver.ErrorThumbNotSupported
res, err := handler.Client.GetThumbURL(ctx, file.SourceName, thumbSize[0], thumbSize[1]) res, err := handler.Client.GetThumbURL(ctx, file.SourceName, thumbSize[0], thumbSize[1])
if err != nil { if err != nil {
var apiErr *RespError var apiErr *RespError

@ -214,6 +214,8 @@ func (fs *FileSystem) deleteGroupedFile(ctx context.Context, files map[uint][]*m
// 执行删除 // 执行删除
toBeDeletedSrcs := append(sourceNamesAll, thumbs...) toBeDeletedSrcs := append(sourceNamesAll, thumbs...)
failedFile, _ := fs.Handler.Delete(ctx, toBeDeletedSrcs) failedFile, _ := fs.Handler.Delete(ctx, toBeDeletedSrcs)
// Exclude failed results related to thumb file
failed[policyID] = util.SliceDifference(failedFile, thumbs) failed[policyID] = util.SliceDifference(failedFile, thumbs)
} }

@ -3,6 +3,7 @@ package filesystem
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"os" "os"
"sync" "sync"
@ -39,8 +40,11 @@ func (fs *FileSystem) GetThumb(ctx context.Context, id uint) (*response.ContentR
res, err := fs.Handler.Thumb(ctx, &file) res, err := fs.Handler.Thumb(ctx, &file)
if errors.Is(err, driver.ErrorThumbNotExist) { if errors.Is(err, driver.ErrorThumbNotExist) {
// Regenerate thumb if the thumb is not initialized yet // Regenerate thumb if the thumb is not initialized yet
fs.GenerateThumbnail(ctx, &file) if generateErr := fs.GenerateThumbnail(ctx, &file); generateErr == nil {
res, err = fs.Handler.Thumb(ctx, &file) res, err = fs.Handler.Thumb(ctx, &file)
} else {
err = generateErr
}
} else if errors.Is(err, driver.ErrorThumbNotSupported) { } else if errors.Is(err, driver.ErrorThumbNotSupported) {
// Policy handler explicitly indicates thumb not available, check if proxy is enabled // Policy handler explicitly indicates thumb not available, check if proxy is enabled
if fs.Policy.CouldProxyThumb() { if fs.Policy.CouldProxyThumb() {
@ -61,9 +65,10 @@ func (fs *FileSystem) GetThumb(ctx context.Context, id uint) (*response.ContentR
) )
} else { } else {
// if not exist, generate and upload the sidecar thumb. // if not exist, generate and upload the sidecar thumb.
fs.GenerateThumbnail(ctx, &file) if err = fs.GenerateThumbnail(ctx, &file); err == nil {
return fs.GetThumb(ctx, id) return fs.GetThumb(ctx, id)
} }
}
} else { } else {
// thumb not supported and proxy is disabled, mark as not available // thumb not supported and proxy is disabled, mark as not available
_ = updateThumbStatus(&file, model.ThumbStatusNotAvailable) _ = updateThumbStatus(&file, model.ThumbStatusNotAvailable)
@ -111,19 +116,24 @@ func (pool *Pool) releaseWorker() {
} }
// GenerateThumbnail generates thumb for given file, upload the thumb file back with given suffix // GenerateThumbnail generates thumb for given file, upload the thumb file back with given suffix
func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) { func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) error {
// 新建上下文 // 新建上下文
newCtx, cancel := context.WithCancel(context.Background()) newCtx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
// TODO: check file size // TODO: check file size
if file.Size > uint64(model.GetIntSetting("thumb_max_src_size", 31457280)) {
_ = updateThumbStatus(file, model.ThumbStatusNotAvailable)
return errors.New("file too large")
}
getThumbWorker().addWorker() getThumbWorker().addWorker()
defer getThumbWorker().releaseWorker() defer getThumbWorker().releaseWorker()
// 获取文件数据 // 获取文件数据
source, err := fs.Handler.Get(newCtx, file.SourceName) source, err := fs.Handler.Get(newCtx, file.SourceName)
if err != nil { if err != nil {
return return fmt.Errorf("faield to fetch original file %q: %w", file.SourceName, err)
} }
defer source.Close() defer source.Close()
@ -137,22 +147,19 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
"thumb_ffmpeg_path", "thumb_ffmpeg_path",
)) ))
if err != nil { if err != nil {
util.Log().Warning("Failed to generate thumb for %s: %s", file.Name, err)
_ = updateThumbStatus(file, model.ThumbStatusNotAvailable) _ = updateThumbStatus(file, model.ThumbStatusNotAvailable)
return return fmt.Errorf("failed to generate thumb for %q: %w", file.Name, err)
} }
thumbFile, err := os.Open(thumbPath) thumbFile, err := os.Open(thumbPath)
if err != nil { if err != nil {
util.Log().Warning("Failed to open temp thumb %q: %s", thumbFile, err) return fmt.Errorf("failed to open temp thumb %q: %w", thumbFile, err)
return
} }
defer thumbFile.Close() defer thumbFile.Close()
fileInfo, err := thumbFile.Stat() fileInfo, err := thumbFile.Stat()
if err != nil { if err != nil {
util.Log().Warning("Failed to stat temp thumb %q: %s", thumbFile, err) return fmt.Errorf("failed to stat temp thumb %q: %w", thumbFile, err)
return
} }
if err = fs.Handler.Put(newCtx, &fsctx.FileStream{ if err = fs.Handler.Put(newCtx, &fsctx.FileStream{
@ -162,8 +169,7 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
Size: uint64(fileInfo.Size()), Size: uint64(fileInfo.Size()),
SavePath: file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb"), SavePath: file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb"),
}); err != nil { }); err != nil {
util.Log().Warning("Failed to save thumb for %s: %s", file.Name, err) return fmt.Errorf("failed to save thumb for %q: %w", file.Name, err)
return
} }
if model.IsTrueVal(model.GetSettingByName("thumb_gc_after_gen")) { if model.IsTrueVal(model.GetSettingByName("thumb_gc_after_gen")) {
@ -178,6 +184,8 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
if err != nil { if err != nil {
_, _ = fs.Handler.Delete(newCtx, []string{file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb")}) _, _ = fs.Handler.Delete(newCtx, []string{file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb")})
} }
return nil
} }
// GenerateThumbnailSize 获取要生成的缩略图的尺寸 // GenerateThumbnailSize 获取要生成的缩略图的尺寸

Loading…
Cancel
Save