From e115497dfe8f6aebb8d85cca3d323fd318d73291 Mon Sep 17 00:00:00 2001 From: Aaron Liu <912394456@qq.com> Date: Fri, 7 Apr 2023 19:27:31 +0800 Subject: [PATCH] feat(thumb): generate thumb for OneDrive files --- pkg/filesystem/driver/onedrive/api.go | 7 ++++--- pkg/filesystem/driver/onedrive/client.go | 2 ++ pkg/filesystem/driver/onedrive/handler.go | 8 +++++--- pkg/filesystem/image.go | 1 + 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/filesystem/driver/onedrive/api.go b/pkg/filesystem/driver/onedrive/api.go index 2ec1663..56abbaa 100644 --- a/pkg/filesystem/driver/onedrive/api.go +++ b/pkg/filesystem/driver/onedrive/api.go @@ -3,7 +3,6 @@ package onedrive import ( "context" "encoding/json" - "errors" "fmt" "github.com/cloudreve/Cloudreve/v3/pkg/conf" "io" @@ -32,6 +31,8 @@ const ( // ListRetry 列取请求重试次数 ListRetry = 1 chunkRetrySleep = time.Second * 5 + + notFoundError = "itemNotFound" ) // GetSourcePath 获取文件的绝对路径 @@ -438,7 +439,7 @@ func (client *Client) GetThumbURL(ctx context.Context, dst string, w, h uint) (s } } - return "", errors.New("failed to generate thumb") + return "", ErrThumbSizeNotFound } // MonitorUpload 监控客户端分片上传进度 @@ -469,7 +470,7 @@ func (client *Client) MonitorUpload(uploadURL, callbackKey, path string, size ui if err != nil { if resErr, ok := err.(*RespError); ok { - if resErr.APIError.Code == "itemNotFound" { + if resErr.APIError.Code == notFoundError { util.Log().Debug("Upload completed, will check upload callback later.") select { case <-time.After(time.Duration(interval) * time.Second): diff --git a/pkg/filesystem/driver/onedrive/client.go b/pkg/filesystem/driver/onedrive/client.go index 149fcf0..39beff3 100644 --- a/pkg/filesystem/driver/onedrive/client.go +++ b/pkg/filesystem/driver/onedrive/client.go @@ -17,6 +17,8 @@ var ( ErrDeleteFile = errors.New("cannot delete file") // ErrClientCanceled 客户端取消操作 ErrClientCanceled = errors.New("client canceled") + // Desired thumb size not available + ErrThumbSizeNotFound = errors.New("thumb size not found") ) // Client OneDrive客户端 diff --git a/pkg/filesystem/driver/onedrive/handler.go b/pkg/filesystem/driver/onedrive/handler.go index 3b8eea7..be47d99 100644 --- a/pkg/filesystem/driver/onedrive/handler.go +++ b/pkg/filesystem/driver/onedrive/handler.go @@ -147,11 +147,13 @@ func (handler Driver) Thumb(ctx context.Context, file *model.File) (*response.Co res, err := handler.Client.GetThumbURL(ctx, file.SourceName, thumbSize[0], thumbSize[1]) if err != nil { - // 如果出现异常,就清空文件的pic_info - if file, ok := ctx.Value(fsctx.FileModelCtx).(model.File); ok { - file.UpdatePicInfo("") + var apiErr *RespError + if errors.As(err, &apiErr); err == ErrThumbSizeNotFound || (apiErr != nil && apiErr.APIError.Code == notFoundError) { + // OneDrive cannot generate thumbnail for this file + return nil, driver.ErrorThumbNotSupported } } + return &response.ContentResponse{ Redirect: true, URL: res, diff --git a/pkg/filesystem/image.go b/pkg/filesystem/image.go index 1e81169..bcf3b1e 100644 --- a/pkg/filesystem/image.go +++ b/pkg/filesystem/image.go @@ -115,6 +115,7 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) { // 新建上下文 newCtx, cancel := context.WithCancel(context.Background()) defer cancel() + // TODO: check file size // 获取文件数据 source, err := fs.Handler.Get(newCtx, file.SourceName)