Feat: upyun callback & authentication

This commit is contained in:
HFO4
2020-01-18 10:40:03 +08:00
parent 21ec3fc710
commit 5befbc21d0
9 changed files with 138 additions and 16 deletions

View File

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

View File

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

View File

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

View File

@@ -29,6 +29,7 @@ type UploadSession struct {
UID uint
PolicyID uint
VirtualPath string
Name string
}
// UploadCallback 上传回调正文