1 Star 0 Fork 0

leminewx / gov2x

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
req_file.go 2.25 KB
一键复制 编辑 原始数据 按行查看 历史
leminewx 提交于 2024-03-30 17:10 . 0.0.5.1:优化OM编解码器
package om
import (
"encoding/json"
"fmt"
"strconv"
)
type fileRequest struct {
Command Command `json:"command"`
Key string `json:"key"` // 文件唯一识别码
Url string `json:"url"` // 文件地址
FileType FileType `json:"file_type"` // 文件类型
FileSize string `json:"file_size,omitempty"` // 文件大小
Username string `json:"username"` // 用户名
Password string `json:"password"` // 密码
DelayTime string `json:"delay_time"` // 超时时间
}
func NewFileRequest() *fileRequest {
return &fileRequest{
DelayTime: "60", // 默认超时时间60s
}
}
func (own *fileRequest) WithUser(username, password string) *fileRequest {
own.Username = username
own.Password = password
return own
}
func (own *fileRequest) WithDelayTime(timeout int) *fileRequest {
own.DelayTime = strconv.Itoa(timeout)
return own
}
func (own *fileRequest) UPLOAD(key, url string, typ FileType) *fileRequest {
own.Url = url
own.Key = key
own.FileType = typ
own.Command = CMD_REQ_UPLOAD
return own
}
func (own *fileRequest) DOWNLOAD(key, url string, typ FileType, size int) *fileRequest {
own.Url = url
own.Key = key
own.FileType = typ
own.FileSize = strconv.Itoa(size)
own.Command = CMD_REQ_DOWNLOAD
return own
}
func (own *fileRequest) Validate() error {
if own.Url == "" {
return fmt.Errorf("[Om.File] empty url")
} else if own.Key == "" {
return fmt.Errorf("[Om.File] empty key")
} else if own.Username == "" {
return fmt.Errorf("[Om.File] empty username")
} else if own.Password == "" {
return fmt.Errorf("[Om.File] empty password")
}
switch own.Command {
case CMD_REQ_UPLOAD:
if !own.FileType.IsUploadType() {
return fmt.Errorf("[Om.File.Upload] file type error: %v", own.FileType)
}
case CMD_REQ_DOWNLOAD:
if !own.FileType.IsDownloadType() {
return fmt.Errorf("[Om.File.Download] file type error: %v", own.FileType)
}
default:
return fmt.Errorf("[Om.File] command error: %s", own.Command)
}
return nil
}
func (own *fileRequest) String() string {
res, _ := json.Marshal(&own)
return string(res)
}
func (own *fileRequest) GetClass() Class {
return CLS_NONE
}
func (own *fileRequest) GetCommand() Command {
return own.Command
}
Go
1
https://gitee.com/leminewx/gov2x.git
git@gitee.com:leminewx/gov2x.git
leminewx
gov2x
gov2x
a88e0b321ff7

搜索帮助