Skip to content

Commit

Permalink
feat(thumb): set size limit for original file
Browse files Browse the repository at this point in the history
  • Loading branch information
HFO4 committed Apr 7, 2023
1 parent b910254 commit f5a21a7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
1 change: 1 addition & 0 deletions models/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_proxy_enabled", Value: "0", 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_medium_icon", Value: "/static/img/logo192.png", Type: "pwa"},
{Name: "pwa_large_icon", Value: "/static/img/logo512.png", Type: "pwa"},
Expand Down
1 change: 0 additions & 1 deletion pkg/filesystem/driver/onedrive/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, driver.ErrorThumbNotSupported
res, err := handler.Client.GetThumbURL(ctx, file.SourceName, thumbSize[0], thumbSize[1])
if err != nil {
var apiErr *RespError
Expand Down
2 changes: 2 additions & 0 deletions pkg/filesystem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ func (fs *FileSystem) deleteGroupedFile(ctx context.Context, files map[uint][]*m
// 执行删除
toBeDeletedSrcs := append(sourceNamesAll, thumbs...)
failedFile, _ := fs.Handler.Delete(ctx, toBeDeletedSrcs)

// Exclude failed results related to thumb file
failed[policyID] = util.SliceDifference(failedFile, thumbs)
}

Expand Down
36 changes: 22 additions & 14 deletions pkg/filesystem/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package filesystem
import (
"context"
"errors"
"fmt"
"os"
"sync"

Expand Down Expand Up @@ -39,8 +40,11 @@ func (fs *FileSystem) GetThumb(ctx context.Context, id uint) (*response.ContentR
res, err := fs.Handler.Thumb(ctx, &file)
if errors.Is(err, driver.ErrorThumbNotExist) {
// Regenerate thumb if the thumb is not initialized yet
fs.GenerateThumbnail(ctx, &file)
res, err = fs.Handler.Thumb(ctx, &file)
if generateErr := fs.GenerateThumbnail(ctx, &file); generateErr == nil {
res, err = fs.Handler.Thumb(ctx, &file)
} else {
err = generateErr
}
} else if errors.Is(err, driver.ErrorThumbNotSupported) {
// Policy handler explicitly indicates thumb not available, check if proxy is enabled
if fs.Policy.CouldProxyThumb() {
Expand All @@ -61,8 +65,9 @@ func (fs *FileSystem) GetThumb(ctx context.Context, id uint) (*response.ContentR
)
} else {
// if not exist, generate and upload the sidecar thumb.
fs.GenerateThumbnail(ctx, &file)
return fs.GetThumb(ctx, id)
if err = fs.GenerateThumbnail(ctx, &file); err == nil {
return fs.GetThumb(ctx, id)
}
}
} else {
// thumb not supported and proxy is disabled, mark as not available
Expand Down Expand Up @@ -111,19 +116,24 @@ func (pool *Pool) releaseWorker() {
}

// 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())
defer cancel()
// 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()
defer getThumbWorker().releaseWorker()

// 获取文件数据
source, err := fs.Handler.Get(newCtx, file.SourceName)
if err != nil {
return
return fmt.Errorf("faield to fetch original file %q: %w", file.SourceName, err)
}
defer source.Close()

Expand All @@ -137,22 +147,19 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
"thumb_ffmpeg_path",
))
if err != nil {
util.Log().Warning("Failed to generate thumb for %s: %s", file.Name, err)
_ = updateThumbStatus(file, model.ThumbStatusNotAvailable)
return
return fmt.Errorf("failed to generate thumb for %q: %w", file.Name, err)
}

thumbFile, err := os.Open(thumbPath)
if err != nil {
util.Log().Warning("Failed to open temp thumb %q: %s", thumbFile, err)
return
return fmt.Errorf("failed to open temp thumb %q: %w", thumbFile, err)
}

defer thumbFile.Close()
fileInfo, err := thumbFile.Stat()
if err != nil {
util.Log().Warning("Failed to stat temp thumb %q: %s", thumbFile, err)
return
return fmt.Errorf("failed to stat temp thumb %q: %w", thumbFile, err)
}

if err = fs.Handler.Put(newCtx, &fsctx.FileStream{
Expand All @@ -162,8 +169,7 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
Size: uint64(fileInfo.Size()),
SavePath: file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb"),
}); err != nil {
util.Log().Warning("Failed to save thumb for %s: %s", file.Name, err)
return
return fmt.Errorf("failed to save thumb for %q: %w", file.Name, err)
}

if model.IsTrueVal(model.GetSettingByName("thumb_gc_after_gen")) {
Expand All @@ -178,6 +184,8 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
if err != nil {
_, _ = fs.Handler.Delete(newCtx, []string{file.SourceName + model.GetSettingByNameWithDefault("thumb_file_suffix", "._thumb")})
}

return nil
}

// GenerateThumbnailSize 获取要生成的缩略图的尺寸
Expand Down

0 comments on commit f5a21a7

Please sign in to comment.