feat(thumb): generate thumb for OneDrive files
This commit is contained in:
parent
62b73b577b
commit
e115497dfe
4 changed files with 12 additions and 6 deletions
|
@ -3,7 +3,6 @@ package onedrive
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
|
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
|
||||||
"io"
|
"io"
|
||||||
|
@ -32,6 +31,8 @@ const (
|
||||||
// ListRetry 列取请求重试次数
|
// ListRetry 列取请求重试次数
|
||||||
ListRetry = 1
|
ListRetry = 1
|
||||||
chunkRetrySleep = time.Second * 5
|
chunkRetrySleep = time.Second * 5
|
||||||
|
|
||||||
|
notFoundError = "itemNotFound"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetSourcePath 获取文件的绝对路径
|
// 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 监控客户端分片上传进度
|
// MonitorUpload 监控客户端分片上传进度
|
||||||
|
@ -469,7 +470,7 @@ func (client *Client) MonitorUpload(uploadURL, callbackKey, path string, size ui
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if resErr, ok := err.(*RespError); ok {
|
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.")
|
util.Log().Debug("Upload completed, will check upload callback later.")
|
||||||
select {
|
select {
|
||||||
case <-time.After(time.Duration(interval) * time.Second):
|
case <-time.After(time.Duration(interval) * time.Second):
|
||||||
|
|
|
@ -17,6 +17,8 @@ var (
|
||||||
ErrDeleteFile = errors.New("cannot delete file")
|
ErrDeleteFile = errors.New("cannot delete file")
|
||||||
// ErrClientCanceled 客户端取消操作
|
// ErrClientCanceled 客户端取消操作
|
||||||
ErrClientCanceled = errors.New("client canceled")
|
ErrClientCanceled = errors.New("client canceled")
|
||||||
|
// Desired thumb size not available
|
||||||
|
ErrThumbSizeNotFound = errors.New("thumb size not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client OneDrive客户端
|
// Client OneDrive客户端
|
||||||
|
|
|
@ -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])
|
res, err := handler.Client.GetThumbURL(ctx, file.SourceName, thumbSize[0], thumbSize[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// 如果出现异常,就清空文件的pic_info
|
var apiErr *RespError
|
||||||
if file, ok := ctx.Value(fsctx.FileModelCtx).(model.File); ok {
|
if errors.As(err, &apiErr); err == ErrThumbSizeNotFound || (apiErr != nil && apiErr.APIError.Code == notFoundError) {
|
||||||
file.UpdatePicInfo("")
|
// OneDrive cannot generate thumbnail for this file
|
||||||
|
return nil, driver.ErrorThumbNotSupported
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &response.ContentResponse{
|
return &response.ContentResponse{
|
||||||
Redirect: true,
|
Redirect: true,
|
||||||
URL: res,
|
URL: res,
|
||||||
|
|
|
@ -115,6 +115,7 @@ func (fs *FileSystem) GenerateThumbnail(ctx context.Context, file *model.File) {
|
||||||
// 新建上下文
|
// 新建上下文
|
||||||
newCtx, cancel := context.WithCancel(context.Background())
|
newCtx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
// TODO: check file size
|
||||||
|
|
||||||
// 获取文件数据
|
// 获取文件数据
|
||||||
source, err := fs.Handler.Get(newCtx, file.SourceName)
|
source, err := fs.Handler.Get(newCtx, file.SourceName)
|
||||||
|
|
Loading…
Add table
Reference in a new issue