mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: upyun callback & authentication
This commit is contained in:
@@ -140,7 +140,7 @@ func (fs *FileSystem) CancelUpload(ctx context.Context, path string, file FileHe
|
||||
}
|
||||
|
||||
// GetUploadToken 生成新的上传凭证
|
||||
func (fs *FileSystem) GetUploadToken(ctx context.Context, path string, size uint64) (*serializer.UploadCredential, error) {
|
||||
func (fs *FileSystem) GetUploadToken(ctx context.Context, path string, size uint64, name string) (*serializer.UploadCredential, error) {
|
||||
// 获取相关有效期设置
|
||||
credentialTTL := model.GetIntSetting("upload_credential_timeout", 3600)
|
||||
callBackSessionTTL := model.GetIntSetting("upload_session_timeout", 86400)
|
||||
@@ -167,6 +167,7 @@ func (fs *FileSystem) GetUploadToken(ctx context.Context, path string, size uint
|
||||
UID: fs.User.ID,
|
||||
PolicyID: fs.User.GetPolicyID(),
|
||||
VirtualPath: path,
|
||||
Name: name,
|
||||
},
|
||||
callBackSessionTTL,
|
||||
)
|
||||
|
||||
@@ -180,7 +180,7 @@ func TestFileSystem_GetUploadToken(t *testing.T) {
|
||||
testHandller := new(FileHeaderMock)
|
||||
testHandller.On("Token", testMock.Anything, int64(10), testMock.Anything).Return(serializer.UploadCredential{Token: "test"}, nil)
|
||||
fs.Handler = testHandller
|
||||
res, err := fs.GetUploadToken(ctx, "/", 10)
|
||||
res, err := fs.GetUploadToken(ctx, "/", 10, "123")
|
||||
testHandller.AssertExpectations(t)
|
||||
asserts.NoError(err)
|
||||
asserts.Equal("test", res.Token)
|
||||
@@ -195,7 +195,7 @@ func TestFileSystem_GetUploadToken(t *testing.T) {
|
||||
testHandller := new(FileHeaderMock)
|
||||
testHandller.On("Token", testMock.Anything, int64(10), testMock.Anything).Return(serializer.UploadCredential{}, errors.New("error"))
|
||||
fs.Handler = testHandller
|
||||
_, err := fs.GetUploadToken(ctx, "/", 10)
|
||||
_, err := fs.GetUploadToken(ctx, "/", 10, "123")
|
||||
testHandller.AssertExpectations(t)
|
||||
asserts.Error(err)
|
||||
}
|
||||
|
||||
@@ -110,15 +110,21 @@ func (handler Driver) getUploadCredential(ctx context.Context, policy UploadPoli
|
||||
policyEncoded := base64.StdEncoding.EncodeToString(policyJSON)
|
||||
|
||||
// 生成签名
|
||||
password := fmt.Sprintf("%x", md5.Sum([]byte(handler.Policy.SecretKey)))
|
||||
mac := hmac.New(sha1.New, []byte(password))
|
||||
elements := []string{"POST", "/" + handler.Policy.BucketName, policyEncoded}
|
||||
value := strings.Join(elements, "&")
|
||||
mac.Write([]byte(value))
|
||||
signStr := base64.StdEncoding.EncodeToString((mac.Sum(nil)))
|
||||
signStr := handler.Sign(ctx, elements)
|
||||
|
||||
return serializer.UploadCredential{
|
||||
Policy: policyEncoded,
|
||||
Token: fmt.Sprintf("UPYUN %s:%s", handler.Policy.AccessKey, signStr),
|
||||
Token: signStr,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Sign 计算又拍云的签名头
|
||||
func (handler Driver) Sign(ctx context.Context, elements []string) string {
|
||||
password := fmt.Sprintf("%x", md5.Sum([]byte(handler.Policy.SecretKey)))
|
||||
mac := hmac.New(sha1.New, []byte(password))
|
||||
value := strings.Join(elements, "&")
|
||||
mac.Write([]byte(value))
|
||||
signStr := base64.StdEncoding.EncodeToString((mac.Sum(nil)))
|
||||
return fmt.Sprintf("UPYUN %s:%s", handler.Policy.AccessKey, signStr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user