Add: upload controller in slave mode

This commit is contained in:
HFO4
2019-12-27 21:15:05 +08:00
parent 4f8558d1e8
commit 6470340104
8 changed files with 272 additions and 16 deletions

View File

@@ -1,10 +1,33 @@
package serializer
import (
"encoding/base64"
"encoding/json"
)
// UploadPolicy slave模式下传递的上传策略
type UploadPolicy struct {
SavePath string `json:"save_path"`
MaxSize uint64 `json:"save_path"`
MaxSize uint64 `json:"max_size"`
AllowedExtension []string `json:"allowed_extension"`
CallbackURL string `json:"callback_url"`
CallbackKey string `json:"callback_key"`
}
// DecodeUploadPolicy 反序列化Header中携带的上传策略
// TODO 测试
func DecodeUploadPolicy(raw string) (*UploadPolicy, error) {
var res UploadPolicy
rawJSON, err := base64.StdEncoding.DecodeString(raw)
if err != nil {
return nil, err
}
err = json.Unmarshal(rawJSON, &res)
if err != nil {
return nil, err
}
return &res, err
}

View File

@@ -0,0 +1,55 @@
package serializer
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestDecodeUploadPolicy(t *testing.T) {
asserts := assert.New(t)
testCases := []struct {
input string
expectError bool
expectNil bool
expectRes *UploadPolicy
}{
{
"错误的base64字符",
true,
true,
&UploadPolicy{},
},
{
"6ZSZ6K+v55qESlNPTuWtl+espg==",
true,
true,
&UploadPolicy{},
},
{
"e30=",
false,
false,
&UploadPolicy{},
},
{
"eyJjYWxsYmFja19rZXkiOiJ0ZXN0In0=",
false,
false,
&UploadPolicy{CallbackKey: "test"},
},
}
for _, testCase := range testCases {
res, err := DecodeUploadPolicy(testCase.input)
if testCase.expectError {
asserts.Error(err)
}
if testCase.expectNil {
asserts.Nil(res)
}
if !testCase.expectNil {
asserts.Equal(testCase.expectRes, res)
}
}
}