Cloudreve/pkg/serializer/upload.go

86 lines
2.1 KiB
Go
Raw Normal View History

2019-12-23 22:50:44 -05:00
package serializer
2019-12-27 08:15:05 -05:00
import (
"encoding/base64"
2019-12-28 02:50:56 -05:00
"encoding/gob"
2019-12-27 08:15:05 -05:00
"encoding/json"
)
2019-12-23 22:50:44 -05:00
// UploadPolicy slave模式下传递的上传策略
type UploadPolicy struct {
SavePath string `json:"save_path"`
2019-12-28 00:14:00 -05:00
FileName string `json:"file_name"`
AutoRename bool `json:"auto_rename"`
2019-12-27 08:15:05 -05:00
MaxSize uint64 `json:"max_size"`
2019-12-23 22:50:44 -05:00
AllowedExtension []string `json:"allowed_extension"`
CallbackURL string `json:"callback_url"`
2019-12-28 02:50:56 -05:00
}
// UploadCredential 返回给客户端的上传凭证
type UploadCredential struct {
2020-01-16 00:36:13 -05:00
Token string `json:"token"`
Policy string `json:"policy"`
2020-01-22 23:38:32 -05:00
Path string `json:"path"` // 存储路径
2020-01-16 00:36:13 -05:00
AccessKey string `json:"ak"`
2020-01-22 23:38:32 -05:00
KeyTime string `json:"key_time,omitempty"` // COS用有效期
Callback string `json:"callback,omitempty"` // 回调地址
Key string `json:"key,omitempty"` // 文件标识符通常为回调key
2019-12-28 02:50:56 -05:00
}
// UploadSession 上传会话
type UploadSession struct {
2020-01-22 23:38:32 -05:00
Key string
2019-12-28 02:50:56 -05:00
UID uint
PolicyID uint
2019-12-28 02:50:56 -05:00
VirtualPath string
2020-01-17 21:40:03 -05:00
Name string
Size uint64
SavePath string
2019-12-28 02:50:56 -05:00
}
2020-01-14 21:14:15 -05:00
// UploadCallback 上传回调正文
type UploadCallback struct {
2019-12-29 04:04:08 -05:00
Name string `json:"name"`
SourceName string `json:"source_name"`
PicInfo string `json:"pic_info"`
Size uint64 `json:"size"`
2019-12-29 04:04:08 -05:00
}
2020-01-14 21:14:15 -05:00
// QiniuCallbackFailed 七牛存储策略上传回调失败响应
type QiniuCallbackFailed struct {
Error string `json:"error"`
}
2019-12-28 02:50:56 -05:00
func init() {
gob.Register(UploadSession{})
2019-12-23 22:50:44 -05:00
}
2019-12-27 08:15:05 -05:00
// 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
}
2019-12-28 02:50:56 -05:00
// EncodeUploadPolicy 序列化Header中携带的上传策略
func (policy *UploadPolicy) EncodeUploadPolicy() (string, error) {
jsonRes, err := json.Marshal(policy)
if err != nil {
return "", err
}
res := base64.StdEncoding.EncodeToString(jsonRes)
return res, nil
}