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.
cloudreve/pkg/serializer/upload.go

93 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package serializer
import (
"encoding/base64"
"encoding/gob"
"encoding/json"
"time"
)
// UploadPolicy slave模式下传递的上传策略
type UploadPolicy struct {
SavePath string `json:"save_path"`
FileName string `json:"file_name"`
AutoRename bool `json:"auto_rename"`
MaxSize uint64 `json:"max_size"`
AllowedExtension []string `json:"allowed_extension"`
CallbackURL string `json:"callback_url"`
}
// UploadCredential 返回给客户端的上传凭证
type UploadCredential struct {
SessionID string `json:"sessionID"`
ChunkSize uint64 `json:"chunkSize"` // 分块大小0 为部分快
Expires int64 `json:"expires"` // 上传凭证过期时间, Unix 时间戳
Token string `json:"token"`
Policy string `json:"policy"`
Path string `json:"path"` // 存储路径
AccessKey string `json:"ak"`
KeyTime string `json:"key_time,omitempty"` // COS用有效期
Callback string `json:"callback,omitempty"` // 回调地址
Key string `json:"key,omitempty"` // 文件标识符通常为回调key
}
// UploadSession 上传会话
type UploadSession struct {
Key string // 上传会话 GUID
UID uint // 发起者
PolicyID uint
VirtualPath string // 用户文件路径,不含文件名
Name string // 文件名
Size uint64 // 文件大小
SavePath string // 物理存储路径,包含物理文件名
ChunkSize uint64 // 分块大小0 为不分快
LastModified *time.Time // 可选的文件最后修改日期
}
// UploadCallback 上传回调正文
type UploadCallback struct {
Name string `json:"name"`
SourceName string `json:"source_name"`
PicInfo string `json:"pic_info"`
Size uint64 `json:"size"`
}
// GeneralUploadCallbackFailed 存储策略上传回调失败响应
type GeneralUploadCallbackFailed struct {
Error string `json:"error"`
}
func init() {
gob.Register(UploadSession{})
}
// 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
}
// 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
}