From d764157d8725e6e5f8885a5fde63be58a6df5d18 Mon Sep 17 00:00:00 2001 From: Cian Date: Mon, 25 Jan 2021 14:30:18 +0800 Subject: [PATCH] =?UTF-8?q?Feat:=20OneDrive=20=E7=AD=96=E7=95=A5=E5=A4=96?= =?UTF-8?q?=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/filesystem/driver/onedrive/handler.go | 21 +++++++++++++++++++++ service/explorer/file.go | 13 +++++++++++++ 2 files changed, 34 insertions(+) diff --git a/pkg/filesystem/driver/onedrive/handler.go b/pkg/filesystem/driver/onedrive/handler.go index 2bedb16..7c42b08 100644 --- a/pkg/filesystem/driver/onedrive/handler.go +++ b/pkg/filesystem/driver/onedrive/handler.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/cloudreve/Cloudreve/v3/pkg/auth" "io" "net/url" "path" @@ -152,6 +153,26 @@ func (handler Driver) Source( isDownload bool, speed int, ) (string, error) { + file, ok := ctx.Value(fsctx.FileModelCtx).(model.File) + if !ok { + return "", errors.New("无法获取文件记录上下文") + } + + if !isDownload { + // 签名生成文件记录 + signedURI, err := auth.SignURI( + auth.General, + fmt.Sprintf("/api/v3/file/get/%d/%s", file.ID, file.Name), + ttl, + ) + if err != nil { + return "", serializer.NewError(serializer.CodeEncryptError, "无法对URL进行签名", err) + } + + finalURL := baseURL.ResolveReference(signedURI).String() + return finalURL, nil + } + // 尝试从缓存中查找 if cachedURL, ok := cache.Get(fmt.Sprintf("onedrive_source_%d_%s", handler.Policy.ID, path)); ok { return handler.replaceSourceHost(cachedURL.(string)) diff --git a/service/explorer/file.go b/service/explorer/file.go index 1015b98..1055a5a 100644 --- a/service/explorer/file.go +++ b/service/explorer/file.go @@ -168,6 +168,19 @@ func (service *FileAnonymousGetService) Download(ctx context.Context, c *gin.Con return serializer.Err(serializer.CodeNotSet, err.Error(), err) } + if fs.Policy.Type == "onedrive" { + // 获取文件临时下载地址 + downloadURL, err := fs.GetDownloadURL(ctx, 0, "download_timeout") + if err != nil { + return serializer.Err(serializer.CodeNotSet, err.Error(), err) + } + c.Redirect(302, downloadURL) + + return serializer.Response{ + Code: 0, + } + } + // 获取文件流 rs, err := fs.GetDownloadContent(ctx, 0) defer rs.Close()