Feat: OneDrive 策略外链

This commit is contained in:
Cian 2021-01-25 14:30:18 +08:00
parent 0e034687bb
commit d764157d87
2 changed files with 34 additions and 0 deletions

View file

@ -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))

View file

@ -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()