mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Refactor: use universal FileHeader when handling file upload, remove usage of global ctx with FileHeader, SavePath, DisableOverwrite
This commit is contained in:
@@ -257,13 +257,16 @@ func (client *Client) UploadChunk(ctx context.Context, uploadURL string, chunk *
|
||||
}
|
||||
|
||||
// Upload 上传文件
|
||||
func (client *Client) Upload(ctx context.Context, dst string, size int, file io.Reader) error {
|
||||
func (client *Client) Upload(ctx context.Context, file fsctx.FileHeader) error {
|
||||
// 决定是否覆盖文件
|
||||
overwrite := "replace"
|
||||
if ctx.Value(fsctx.DisableOverwrite) != nil {
|
||||
if file.GetMode() != fsctx.Overwrite {
|
||||
overwrite = "fail"
|
||||
}
|
||||
|
||||
size := int(file.GetSize())
|
||||
dst := file.GetSavePath()
|
||||
|
||||
// 小文件,使用简单上传接口上传
|
||||
if size <= int(SmallFileSize) {
|
||||
_, err := client.SimpleUpload(ctx, dst, file, int64(size), WithConflictBehavior(overwrite))
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@@ -121,9 +120,9 @@ func (handler Driver) Get(ctx context.Context, path string) (response.RSCloser,
|
||||
}
|
||||
|
||||
// Put 将文件流保存到指定目录
|
||||
func (handler Driver) Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error {
|
||||
func (handler Driver) Put(ctx context.Context, file fsctx.FileHeader) error {
|
||||
defer file.Close()
|
||||
return handler.Client.Upload(ctx, dst, int(size), file)
|
||||
return handler.Client.Upload(ctx, file)
|
||||
}
|
||||
|
||||
// Delete 删除一个或多个文件,
|
||||
@@ -223,20 +222,10 @@ func (handler Driver) replaceSourceHost(origin string) (string, error) {
|
||||
}
|
||||
|
||||
// Token 获取上传会话URL
|
||||
func (handler Driver) Token(ctx context.Context, ttl int64, uploadSession *serializer.UploadSession) (serializer.UploadCredential, error) {
|
||||
|
||||
// 读取上下文中生成的存储路径和文件大小
|
||||
savePath, ok := ctx.Value(fsctx.SavePathCtx).(string)
|
||||
if !ok {
|
||||
return serializer.UploadCredential{}, errors.New("无法获取存储路径")
|
||||
}
|
||||
fileSize, ok := ctx.Value(fsctx.FileSizeCtx).(uint64)
|
||||
if !ok {
|
||||
return serializer.UploadCredential{}, errors.New("无法获取文件大小")
|
||||
}
|
||||
func (handler Driver) Token(ctx context.Context, ttl int64, uploadSession *serializer.UploadSession, file fsctx.FileHeader) (serializer.UploadCredential, error) {
|
||||
|
||||
// 如果小于4MB,则由服务端中转
|
||||
if fileSize <= SmallFileSize {
|
||||
if file.GetSize() <= SmallFileSize {
|
||||
return serializer.UploadCredential{}, nil
|
||||
}
|
||||
|
||||
@@ -245,13 +234,13 @@ func (handler Driver) Token(ctx context.Context, ttl int64, uploadSession *seria
|
||||
apiBaseURI, _ := url.Parse("/api/v3/callback/onedrive/finish/" + uploadSession.Key)
|
||||
apiURL := siteURL.ResolveReference(apiBaseURI)
|
||||
|
||||
uploadURL, err := handler.Client.CreateUploadSession(ctx, savePath, WithConflictBehavior("fail"))
|
||||
uploadURL, err := handler.Client.CreateUploadSession(ctx, file.GetSavePath(), WithConflictBehavior("fail"))
|
||||
if err != nil {
|
||||
return serializer.UploadCredential{}, err
|
||||
}
|
||||
|
||||
// 监控回调及上传
|
||||
go handler.Client.MonitorUpload(uploadURL, uploadSession.Key, savePath, fileSize, ttl)
|
||||
go handler.Client.MonitorUpload(uploadURL, uploadSession.Key, file.GetSavePath(), file.GetSize(), ttl)
|
||||
|
||||
return serializer.UploadCredential{
|
||||
Policy: uploadURL,
|
||||
|
||||
@@ -36,7 +36,7 @@ func TestDriver_Token(t *testing.T) {
|
||||
// 无法获取文件路径
|
||||
{
|
||||
ctx := context.WithValue(context.Background(), fsctx.FileSizeCtx, uint64(10))
|
||||
res, err := handler.Token(ctx, 10, "key")
|
||||
res, err := handler.Token(ctx, 10, "key", nil)
|
||||
asserts.Error(err)
|
||||
asserts.Equal(serializer.UploadCredential{}, res)
|
||||
}
|
||||
@@ -44,7 +44,7 @@ func TestDriver_Token(t *testing.T) {
|
||||
// 无法获取文件大小
|
||||
{
|
||||
ctx := context.WithValue(context.Background(), fsctx.SavePathCtx, "/123")
|
||||
res, err := handler.Token(ctx, 10, "key")
|
||||
res, err := handler.Token(ctx, 10, "key", nil)
|
||||
asserts.Error(err)
|
||||
asserts.Equal(serializer.UploadCredential{}, res)
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func TestDriver_Token(t *testing.T) {
|
||||
{
|
||||
ctx := context.WithValue(context.Background(), fsctx.SavePathCtx, "/123")
|
||||
ctx = context.WithValue(ctx, fsctx.FileSizeCtx, uint64(10))
|
||||
res, err := handler.Token(ctx, 10, "key")
|
||||
res, err := handler.Token(ctx, 10, "key", nil)
|
||||
asserts.NoError(err)
|
||||
asserts.Equal(serializer.UploadCredential{}, res)
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func TestDriver_Token(t *testing.T) {
|
||||
handler.Client.Request = clientMock
|
||||
ctx := context.WithValue(context.Background(), fsctx.SavePathCtx, "/123")
|
||||
ctx = context.WithValue(ctx, fsctx.FileSizeCtx, uint64(20*1024*1024))
|
||||
res, err := handler.Token(ctx, 10, "key")
|
||||
res, err := handler.Token(ctx, 10, "key", nil)
|
||||
asserts.Error(err)
|
||||
asserts.Equal(serializer.UploadCredential{}, res)
|
||||
}
|
||||
@@ -114,7 +114,7 @@ func TestDriver_Token(t *testing.T) {
|
||||
time.Sleep(time.Duration(1) * time.Second)
|
||||
FinishCallback("key")
|
||||
}()
|
||||
res, err := handler.Token(ctx, 10, "key")
|
||||
res, err := handler.Token(ctx, 10, "key", nil)
|
||||
asserts.NoError(err)
|
||||
asserts.Equal("123321", res.Policy)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user