You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.3 KiB
64 lines
1.3 KiB
5 years ago
|
package serializer
|
||
|
|
||
5 years ago
|
import (
|
||
|
"encoding/base64"
|
||
5 years ago
|
"encoding/gob"
|
||
5 years ago
|
"encoding/json"
|
||
|
)
|
||
|
|
||
5 years ago
|
// UploadPolicy slave模式下传递的上传策略
|
||
|
type UploadPolicy struct {
|
||
|
SavePath string `json:"save_path"`
|
||
5 years ago
|
FileName string `json:"file_name"`
|
||
|
AutoRename bool `json:"auto_rename"`
|
||
5 years ago
|
MaxSize uint64 `json:"max_size"`
|
||
5 years ago
|
AllowedExtension []string `json:"allowed_extension"`
|
||
|
CallbackURL string `json:"callback_url"`
|
||
5 years ago
|
}
|
||
|
|
||
|
// UploadCredential 返回给客户端的上传凭证
|
||
|
type UploadCredential struct {
|
||
|
Token string `json:"token"`
|
||
|
Policy string `json:"policy"`
|
||
|
}
|
||
|
|
||
|
// UploadSession 上传会话
|
||
|
type UploadSession struct {
|
||
|
UID uint
|
||
|
VirtualPath string
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
gob.Register(UploadSession{})
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
// DecodeUploadPolicy 反序列化Header中携带的上传策略
|
||
|
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
|
||
|
}
|
||
5 years ago
|
|
||
|
// EncodeUploadPolicy 序列化Header中携带的上传策略
|
||
|
// TODO 测试
|
||
|
func (policy *UploadPolicy) EncodeUploadPolicy() (string, error) {
|
||
|
jsonRes, err := json.Marshal(policy)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
res := base64.StdEncoding.EncodeToString(jsonRes)
|
||
|
return res, nil
|
||
|
|
||
|
}
|