Modify: decoupling getSignedURL modules
This commit is contained in:
parent
6ada69f637
commit
9a942f8b48
7 changed files with 98 additions and 85 deletions
|
@ -175,37 +175,36 @@ func (fs *FileSystem) GroupFileByPolicy(ctx context.Context, files []model.File)
|
||||||
return policyGroup
|
return policyGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDownloadURL 创建文件下载链接
|
// GetDownloadURL 创建文件下载链接, timeout 为数据库中存储过期时间的字段
|
||||||
func (fs *FileSystem) GetDownloadURL(ctx context.Context, path string) (string, error) {
|
func (fs *FileSystem) GetDownloadURL(ctx context.Context, path string, timeout string) (string, error) {
|
||||||
|
var fileTarget *model.File
|
||||||
// 找到文件
|
// 找到文件
|
||||||
if len(fs.FileTarget) == 0 {
|
if len(fs.FileTarget) == 0 {
|
||||||
exist, file := fs.IsFileExist(path)
|
exist, file := fs.IsFileExist(path)
|
||||||
if !exist {
|
if !exist {
|
||||||
return "", ErrObjectNotExist
|
return "", ErrObjectNotExist
|
||||||
}
|
}
|
||||||
fs.FileTarget = []model.File{*file}
|
fileTarget = file
|
||||||
}
|
} else {
|
||||||
|
fileTarget = &fs.FileTarget[0]
|
||||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, fs.FileTarget[0])
|
|
||||||
|
|
||||||
// 将当前存储策略重设为文件使用的
|
|
||||||
fs.Policy = fs.FileTarget[0].GetPolicy()
|
|
||||||
err := fs.dispatchHandler()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成下載地址
|
// 生成下載地址
|
||||||
siteURL := model.GetSiteURL()
|
ttl, err := strconv.ParseInt(model.GetSettingByName(timeout), 10, 64)
|
||||||
ttl, err := strconv.ParseInt(model.GetSettingByName("download_timeout"), 10, 64)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", serializer.NewError(serializer.CodeInternalSetting, "无法获取下载地址有效期", err)
|
return "",
|
||||||
|
serializer.NewError(
|
||||||
|
serializer.CodeInternalSetting,
|
||||||
|
"无法获取下载地址有效期",
|
||||||
|
err,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
source, err := fs.Handler.GetDownloadURL(
|
|
||||||
|
source, err := fs.signURL(
|
||||||
ctx,
|
ctx,
|
||||||
fs.FileTarget[0].SourceName,
|
fileTarget,
|
||||||
*siteURL,
|
|
||||||
ttl,
|
ttl,
|
||||||
|
true,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -222,18 +221,8 @@ func (fs *FileSystem) GetSource(ctx context.Context, fileID uint) (string, error
|
||||||
return "", ErrObjectNotExist.WithError(err)
|
return "", ErrObjectNotExist.WithError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.FileTarget = []model.File{fileObject[0]}
|
|
||||||
ctx = context.WithValue(ctx, fsctx.FileModelCtx, fileObject[0])
|
|
||||||
|
|
||||||
// 将当前存储策略重设为文件使用的
|
|
||||||
fs.Policy = fileObject[0].GetPolicy()
|
|
||||||
err = fs.dispatchHandler()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查存储策略是否可以获得外链
|
// 检查存储策略是否可以获得外链
|
||||||
if !fs.Policy.IsOriginLinkEnable {
|
if !fileObject[0].GetPolicy().IsOriginLinkEnable {
|
||||||
return "", serializer.NewError(
|
return "", serializer.NewError(
|
||||||
serializer.CodePolicyNotAllowed,
|
serializer.CodePolicyNotAllowed,
|
||||||
"当前存储策略无法获得外链",
|
"当前存储策略无法获得外链",
|
||||||
|
@ -241,9 +230,29 @@ func (fs *FileSystem) GetSource(ctx context.Context, fileID uint) (string, error
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成外链地址
|
source, err := fs.signURL(ctx, &fileObject[0], 0, false)
|
||||||
siteURL := model.GetSiteURL()
|
if err != nil {
|
||||||
source, err := fs.Handler.Source(ctx, fileObject[0].SourceName, *siteURL, 0)
|
return "", serializer.NewError(serializer.CodeNotSet, "无法获取外链", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return source, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fs *FileSystem) signURL(ctx context.Context, file *model.File, ttl int64, isDownload bool) (string, error) {
|
||||||
|
fs.FileTarget = []model.File{*file}
|
||||||
|
ctx = context.WithValue(ctx, fsctx.FileModelCtx, *file)
|
||||||
|
|
||||||
|
// 将当前存储策略重设为文件使用的
|
||||||
|
fs.Policy = file.GetPolicy()
|
||||||
|
err := fs.dispatchHandler()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 签名最终URL
|
||||||
|
// 生成外链地址
|
||||||
|
siteURL := model.GetSiteURL()
|
||||||
|
source, err := fs.Handler.Source(ctx, fs.FileTarget[0].SourceName, *siteURL, ttl, isDownload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", serializer.NewError(serializer.CodeNotSet, "无法获取外链", err)
|
return "", serializer.NewError(serializer.CodeNotSet, "无法获取外链", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -403,17 +403,16 @@ func TestFileSystem_GetDownloadURL(t *testing.T) {
|
||||||
WithArgs(1).
|
WithArgs(1).
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id"}).AddRow(1, "1.txt", 1))
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id"}).AddRow(1, "1.txt", 1))
|
||||||
|
// 相关设置
|
||||||
|
mock.ExpectQuery("SELECT(.+)").WithArgs("download_timeout").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, "20"))
|
||||||
// 查找上传策略
|
// 查找上传策略
|
||||||
mock.ExpectQuery("SELECT(.+)").
|
mock.ExpectQuery("SELECT(.+)").
|
||||||
WillReturnRows(
|
WillReturnRows(
|
||||||
sqlmock.NewRows([]string{"id", "type", "is_origin_link_enable"}).
|
sqlmock.NewRows([]string{"id", "type", "is_origin_link_enable"}).
|
||||||
AddRow(35, "local", true),
|
AddRow(35, "local", true),
|
||||||
)
|
)
|
||||||
// 相关设置
|
|
||||||
mock.ExpectQuery("SELECT(.+)").WithArgs("siteURL").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, "https://cloudreve.org"))
|
mock.ExpectQuery("SELECT(.+)").WithArgs("siteURL").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, "https://cloudreve.org"))
|
||||||
mock.ExpectQuery("SELECT(.+)").WithArgs("download_timeout").WillReturnRows(sqlmock.NewRows([]string{"id", "value"}).AddRow(1, "20"))
|
downloadURL, err := fs.GetDownloadURL(ctx, "/1.txt", "download_timeout")
|
||||||
|
|
||||||
downloadURL, err := fs.GetDownloadURL(ctx, "/1.txt")
|
|
||||||
asserts.NoError(mock.ExpectationsWereMet())
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
asserts.NoError(err)
|
asserts.NoError(err)
|
||||||
asserts.NotEmpty(downloadURL)
|
asserts.NotEmpty(downloadURL)
|
||||||
|
@ -432,7 +431,7 @@ func TestFileSystem_GetDownloadURL(t *testing.T) {
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
|
||||||
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id"}))
|
mock.ExpectQuery("SELECT(.+)").WillReturnRows(sqlmock.NewRows([]string{"id", "name", "policy_id"}))
|
||||||
|
|
||||||
downloadURL, err := fs.GetDownloadURL(ctx, "/1.txt")
|
downloadURL, err := fs.GetDownloadURL(ctx, "/1.txt", "download_timeout")
|
||||||
asserts.NoError(mock.ExpectationsWereMet())
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
asserts.Error(err)
|
asserts.Error(err)
|
||||||
asserts.Empty(downloadURL)
|
asserts.Empty(downloadURL)
|
||||||
|
@ -457,7 +456,7 @@ func TestFileSystem_GetDownloadURL(t *testing.T) {
|
||||||
AddRow(35, "unknown", true),
|
AddRow(35, "unknown", true),
|
||||||
)
|
)
|
||||||
|
|
||||||
downloadURL, err := fs.GetDownloadURL(ctx, "/1.txt")
|
downloadURL, err := fs.GetDownloadURL(ctx, "/1.txt", "download_timeout")
|
||||||
asserts.NoError(mock.ExpectationsWereMet())
|
asserts.NoError(mock.ExpectationsWereMet())
|
||||||
asserts.Error(err)
|
asserts.Error(err)
|
||||||
asserts.Empty(downloadURL)
|
asserts.Empty(downloadURL)
|
||||||
|
|
|
@ -24,16 +24,20 @@ type FileHeader interface {
|
||||||
type Handler interface {
|
type Handler interface {
|
||||||
// 上传文件
|
// 上传文件
|
||||||
Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error
|
Put(ctx context.Context, file io.ReadCloser, dst string, size uint64) error
|
||||||
|
|
||||||
// 删除一个或多个文件
|
// 删除一个或多个文件
|
||||||
Delete(ctx context.Context, files []string) ([]string, error)
|
Delete(ctx context.Context, files []string) ([]string, error)
|
||||||
|
|
||||||
// 获取文件
|
// 获取文件
|
||||||
Get(ctx context.Context, path string) (response.RSCloser, error)
|
Get(ctx context.Context, path string) (response.RSCloser, error)
|
||||||
|
|
||||||
// 获取缩略图
|
// 获取缩略图
|
||||||
Thumb(ctx context.Context, path string) (*response.ContentResponse, error)
|
Thumb(ctx context.Context, path string) (*response.ContentResponse, error)
|
||||||
// 获取外链地址,url
|
|
||||||
Source(ctx context.Context, path string, url url.URL, expires int64) (string, error)
|
// 获取外链/下载地址,
|
||||||
//获取下载地址
|
// url - 站点本身地址,
|
||||||
GetDownloadURL(ctx context.Context, path string, url url.URL, expires int64) (string, error)
|
// isDownload - 是否直接下载
|
||||||
|
Source(ctx context.Context, path string, url url.URL, ttl int64, isDownload bool) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileSystem 管理文件的文件系统
|
// FileSystem 管理文件的文件系统
|
||||||
|
|
|
@ -111,47 +111,52 @@ func (handler Handler) Thumb(ctx context.Context, path string) (*response.Conten
|
||||||
}
|
}
|
||||||
|
|
||||||
// Source 获取外链URL
|
// Source 获取外链URL
|
||||||
func (handler Handler) Source(ctx context.Context, path string, url url.URL, expires int64) (string, error) {
|
func (handler Handler) Source(
|
||||||
|
ctx context.Context,
|
||||||
|
path string,
|
||||||
|
baseURL url.URL,
|
||||||
|
ttl int64,
|
||||||
|
isDownload bool,
|
||||||
|
) (string, error) {
|
||||||
file, ok := ctx.Value(fsctx.FileModelCtx).(model.File)
|
file, ok := ctx.Value(fsctx.FileModelCtx).(model.File)
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", errors.New("无法获取文件记录上下文")
|
return "", errors.New("无法获取文件记录上下文")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 签名生成文件记录
|
var expires int64
|
||||||
signedURI, err := auth.SignURI(
|
if ttl > 0 {
|
||||||
fmt.Sprintf("/api/v3/file/get/%d/%s", file.ID, file.Name),
|
expires = time.Now().Unix() + ttl
|
||||||
0,
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
signedURI *url.URL
|
||||||
|
err error
|
||||||
)
|
)
|
||||||
if err != nil {
|
if isDownload {
|
||||||
return "", serializer.NewError(serializer.CodeEncryptError, "无法对URL进行签名", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
finalURL := url.ResolveReference(signedURI).String()
|
|
||||||
return finalURL, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (handler Handler) GetDownloadURL(ctx context.Context, path string, url url.URL, ttl int64) (string, error) {
|
|
||||||
file, ok := ctx.Value(fsctx.FileModelCtx).(model.File)
|
|
||||||
if !ok {
|
|
||||||
return "", errors.New("无法获取文件记录上下文")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建下载会话,将文件信息写入缓存
|
// 创建下载会话,将文件信息写入缓存
|
||||||
downloadSessionID := util.RandStringRunes(16)
|
downloadSessionID := util.RandStringRunes(16)
|
||||||
err := cache.Set("download_"+downloadSessionID, file, int(ttl))
|
err = cache.Set("download_"+downloadSessionID, file, int(ttl))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", serializer.NewError(serializer.CodeCacheOperation, "无法创建下載会话", err)
|
return "", serializer.NewError(serializer.CodeCacheOperation, "无法创建下載会话", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 签名生成文件记录
|
// 签名生成文件记录
|
||||||
signedURI, err := auth.SignURI(
|
signedURI, err = auth.SignURI(
|
||||||
fmt.Sprintf("/api/v3/file/download/%s", downloadSessionID),
|
fmt.Sprintf("/api/v3/file/download/%s", downloadSessionID),
|
||||||
time.Now().Unix()+ttl,
|
expires,
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
// 签名生成文件记录
|
||||||
|
signedURI, err = auth.SignURI(
|
||||||
|
fmt.Sprintf("/api/v3/file/get/%d/%s", file.ID, file.Name),
|
||||||
|
expires,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", serializer.NewError(serializer.CodeEncryptError, "无法对URL进行签名", err)
|
return "", serializer.NewError(serializer.CodeEncryptError, "无法对URL进行签名", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
finalURL := url.ResolveReference(signedURI).String()
|
finalURL := baseURL.ResolveReference(signedURI).String()
|
||||||
return finalURL, nil
|
return finalURL, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -135,7 +135,7 @@ func TestHandler_Source(t *testing.T) {
|
||||||
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
|
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
|
||||||
baseURL, err := url.Parse("https://cloudreve.org")
|
baseURL, err := url.Parse("https://cloudreve.org")
|
||||||
asserts.NoError(err)
|
asserts.NoError(err)
|
||||||
sourceURL, err := handler.Source(ctx, "", *baseURL, 0)
|
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false)
|
||||||
asserts.NoError(err)
|
asserts.NoError(err)
|
||||||
asserts.NotEmpty(sourceURL)
|
asserts.NotEmpty(sourceURL)
|
||||||
asserts.Contains(sourceURL, "sign=")
|
asserts.Contains(sourceURL, "sign=")
|
||||||
|
@ -146,7 +146,7 @@ func TestHandler_Source(t *testing.T) {
|
||||||
{
|
{
|
||||||
baseURL, err := url.Parse("https://cloudreve.org")
|
baseURL, err := url.Parse("https://cloudreve.org")
|
||||||
asserts.NoError(err)
|
asserts.NoError(err)
|
||||||
sourceURL, err := handler.Source(ctx, "", *baseURL, 0)
|
sourceURL, err := handler.Source(ctx, "", *baseURL, 0, false)
|
||||||
asserts.Error(err)
|
asserts.Error(err)
|
||||||
asserts.Empty(sourceURL)
|
asserts.Empty(sourceURL)
|
||||||
}
|
}
|
||||||
|
@ -169,7 +169,7 @@ func TestHandler_GetDownloadURL(t *testing.T) {
|
||||||
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
|
ctx := context.WithValue(ctx, fsctx.FileModelCtx, file)
|
||||||
baseURL, err := url.Parse("https://cloudreve.org")
|
baseURL, err := url.Parse("https://cloudreve.org")
|
||||||
asserts.NoError(err)
|
asserts.NoError(err)
|
||||||
downloadURL, err := handler.GetDownloadURL(ctx, "", *baseURL, 10)
|
downloadURL, err := handler.Source(ctx, "", *baseURL, 10, true)
|
||||||
asserts.NoError(err)
|
asserts.NoError(err)
|
||||||
asserts.Contains(downloadURL, "sign=")
|
asserts.Contains(downloadURL, "sign=")
|
||||||
asserts.Contains(downloadURL, "https://cloudreve.org")
|
asserts.Contains(downloadURL, "https://cloudreve.org")
|
||||||
|
@ -179,7 +179,7 @@ func TestHandler_GetDownloadURL(t *testing.T) {
|
||||||
{
|
{
|
||||||
baseURL, err := url.Parse("https://cloudreve.org")
|
baseURL, err := url.Parse("https://cloudreve.org")
|
||||||
asserts.NoError(err)
|
asserts.NoError(err)
|
||||||
downloadURL, err := handler.GetDownloadURL(ctx, "", *baseURL, 10)
|
downloadURL, err := handler.Source(ctx, "", *baseURL, 10, true)
|
||||||
asserts.Error(err)
|
asserts.Error(err)
|
||||||
asserts.Empty(downloadURL)
|
asserts.Empty(downloadURL)
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,12 +42,8 @@ func (m FileHeaderMock) Thumb(ctx context.Context, files string) (*response.Cont
|
||||||
return args.Get(0).(*response.ContentResponse), args.Error(1)
|
return args.Get(0).(*response.ContentResponse), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m FileHeaderMock) Source(ctx context.Context, path string, url url.URL, expires int64) (string, error) {
|
func (m FileHeaderMock) Source(ctx context.Context, path string, url url.URL, expires int64, isDownload bool) (string, error) {
|
||||||
args := m.Called(ctx, path, url, expires)
|
args := m.Called(ctx, path, url, expires, isDownload)
|
||||||
return args.Get(0).(string), args.Error(1)
|
|
||||||
}
|
|
||||||
func (m FileHeaderMock) GetDownloadURL(ctx context.Context, path string, url url.URL, expires int64) (string, error) {
|
|
||||||
args := m.Called(ctx, path, url, expires)
|
|
||||||
return args.Get(0).(string), args.Error(1)
|
return args.Get(0).(string), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -104,7 +104,7 @@ func (service *SingleFileService) CreateDownloadSession(ctx context.Context, c *
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取下载地址
|
// 获取下载地址
|
||||||
downloadURL, err := fs.GetDownloadURL(ctx, service.Path)
|
downloadURL, err := fs.GetDownloadURL(ctx, service.Path, "download_timeout")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue