i18n: error codes for aria2 / callback/ directory operation

pull/1416/head
HFO4 2 years ago
parent a1880672b1
commit 08104646ba

@ -1 +1 @@
Subproject commit f47af47efeecae015dd28a76ab797665391029e0
Subproject commit a1028e7e0ae96be4bb67d8c117cf39e07c207473

@ -7,20 +7,19 @@ import (
)
var (
ErrUnknownPolicyType = errors.New("未知存储策略类型")
ErrFileSizeTooBig = errors.New("单个文件尺寸太大")
ErrFileExtensionNotAllowed = errors.New("不允许上传此类型的文件")
ErrInsufficientCapacity = errors.New("容量空间不足")
ErrIllegalObjectName = errors.New("目标名称非法")
ErrClientCanceled = errors.New("客户端取消操作")
ErrRootProtected = errors.New("无法对根目录进行操作")
ErrInsertFileRecord = serializer.NewError(serializer.CodeDBError, "无法插入文件记录", nil)
ErrFileExisted = serializer.NewError(serializer.CodeObjectExist, "同名文件或目录已存在", nil)
ErrFileUploadSessionExisted = serializer.NewError(serializer.CodeObjectExist, "当前目录下已经有同名文件正在上传中,请尝试清空上传会话", nil)
ErrFolderExisted = serializer.NewError(serializer.CodeObjectExist, "同名目录已存在", nil)
ErrPathNotExist = serializer.NewError(404, "路径不存在", nil)
ErrObjectNotExist = serializer.NewError(404, "文件不存在", nil)
ErrIO = serializer.NewError(serializer.CodeIOFailed, "无法读取文件数据", nil)
ErrDBListObjects = serializer.NewError(serializer.CodeDBError, "无法列取对象记录", nil)
ErrDBDeleteObjects = serializer.NewError(serializer.CodeDBError, "无法删除对象记录", nil)
ErrUnknownPolicyType = serializer.NewError(serializer.CodeInternalSetting, "Unknown policy type", nil)
ErrFileSizeTooBig = serializer.NewError(serializer.CodeFileTooLarge, "", nil)
ErrFileExtensionNotAllowed = serializer.NewError(serializer.CodeFileTypeNotAllowed, "", nil)
ErrInsufficientCapacity = serializer.NewError(serializer.CodeInsufficientCapacity, "", nil)
ErrIllegalObjectName = serializer.NewError(serializer.CodeIllegalObjectName, "", nil)
ErrClientCanceled = errors.New("Client canceled operation")
ErrRootProtected = serializer.NewError(serializer.CodeRootProtected, "", nil)
ErrInsertFileRecord = serializer.NewError(serializer.CodeDBError, "Failed to create file record", nil)
ErrFileExisted = serializer.NewError(serializer.CodeObjectExist, "", nil)
ErrFileUploadSessionExisted = serializer.NewError(serializer.CodeConflictUploadOngoing, "", nil)
ErrPathNotExist = serializer.NewError(serializer.CodeParentNotExist, "", nil)
ErrObjectNotExist = serializer.NewError(serializer.CodeParentNotExist, "", nil)
ErrIO = serializer.NewError(serializer.CodeIOFailed, "Failed to read file data", nil)
ErrDBListObjects = serializer.NewError(serializer.CodeDBError, "Failed to list object records", nil)
ErrDBDeleteObjects = serializer.NewError(serializer.CodeDBError, "Failed to delete object records", nil)
)

@ -142,6 +142,24 @@ const (
CodeListFilesError = 40045
// 对系统节点进行非法操作
CodeInvalidActionOnSystemNode = 40046
// 创建文件系统出错
CodeCreateFSError = 40047
// 创建任务出错
CodeCreateTaskError = 40048
// 文件尺寸太大
CodeFileTooLarge = 40049
// 文件类型不允许
CodeFileTypeNotAllowed = 40050
// 用户容量不足
CodeInsufficientCapacity = 40051
// 对象名非法
CodeIllegalObjectName = 40052
// 不支持对根目录执行此操作
CodeRootProtected = 40053
// 当前目录下已经有同名文件正在上传中
CodeConflictUploadOngoing = 40054
// 文件信息不一致
CodeMetaMismatch = 40055
// CodeDBError 数据库操作失败
CodeDBError = 50001
// CodeEncryptError 加密失败
@ -158,6 +176,10 @@ const (
CodeUpdateSetting = 50008
// 跨域策略添加失败
CodeAddCORS = 50009
// 节点不可用
CodeNodeOffline = 50010
// 文件元信息查询失败
CodeQueryMetaFailed = 50011
//CodeParamErr 各种奇奇怪怪的参数错误
CodeParamErr = 40001
// CodeNotSet 未定错误后续尝试从error中获取
@ -183,7 +205,8 @@ func ParamErr(msg string, err error) Response {
// Err 通用错误处理
func Err(errCode int, msg string, err error) Response {
// 底层错误是AppError则尝试从AppError中获取详细信息
if appError, ok := err.(AppError); ok {
var appError AppError
if errors.As(err, &appError) {
errCode = appError.Code
err = appError.RawError
msg = appError.Msg

@ -57,7 +57,7 @@ func UpyunCallback(c *gin.Context) {
if err := c.ShouldBind(&callbackBody); err == nil {
if callbackBody.Code != 200 {
util.Log().Debug(
"又拍云回调返回错误代码%d信息%s",
"Upload callback returned error code:%d, message: %s",
callbackBody.Code,
callbackBody.Message,
)

@ -42,7 +42,7 @@ func (service *ListFolderService) List(c *gin.Context) serializer.Response {
// 创建文件系统
fs, err := filesystem.NewAnonymousFileSystem()
if err != nil {
return serializer.Err(serializer.CodeInternalSetting, "Failed to create filesystem.", err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
@ -69,7 +69,7 @@ func (service *ListFolderService) List(c *gin.Context) serializer.Response {
// 创建文件系统
fs, err := filesystem.NewFileSystem(&user)
if err != nil {
return serializer.Err(serializer.CodeInternalSetting, "Failed to create filesystem.", err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()

@ -24,24 +24,24 @@ func (service *BatchAddURLService) Add(c *gin.Context, taskType int) serializer.
// 创建文件系统
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
// 检查用户组权限
if !fs.User.Group.OptionsSerialized.Aria2 {
return serializer.Err(serializer.CodeGroupNotAllowed, "当前用户组无法进行此操作", nil)
return serializer.Err(serializer.CodeGroupNotAllowed, "", nil)
}
// 存放目录是否存在
if exist, _ := fs.IsPathExist(service.Dst); !exist {
return serializer.Err(serializer.CodeNotFound, "存放路径不存在", nil)
return serializer.Err(serializer.CodeParentNotExist, "", nil)
}
// 检查批量任务数量
limit := fs.User.Group.OptionsSerialized.Aria2BatchSize
if limit > 0 && len(service.URLs) > limit {
return serializer.Err(serializer.CodeBatchAria2Size, "Exceed aria2 batch size", nil)
return serializer.Err(serializer.CodeBatchAria2Size, "", nil)
}
res := make([]serializer.Response, 0, len(service.URLs))
@ -71,25 +71,25 @@ func (service *AddURLService) Add(c *gin.Context, fs *filesystem.FileSystem, tas
// 创建文件系统
fs, err = filesystem.NewFileSystemFromContext(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
// 检查用户组权限
if !fs.User.Group.OptionsSerialized.Aria2 {
return serializer.Err(serializer.CodeGroupNotAllowed, "当前用户组无法进行此操作", nil)
return serializer.Err(serializer.CodeGroupNotAllowed, "", nil)
}
// 存放目录是否存在
if exist, _ := fs.IsPathExist(service.Dst); !exist {
return serializer.Err(serializer.CodeNotFound, "存放路径不存在", nil)
return serializer.Err(serializer.CodeParentNotExist, "", nil)
}
}
downloads := model.GetDownloadsByStatusAndUser(0, fs.User.ID, common.Downloading, common.Paused, common.Ready)
limit := fs.User.Group.OptionsSerialized.Aria2BatchSize
if limit > 0 && len(downloads)+1 > limit {
return serializer.Err(serializer.CodeBatchAria2Size, "Exceed aria2 batch size", nil)
return serializer.Err(serializer.CodeBatchAria2Size, "", nil)
}
// 创建任务
@ -107,20 +107,20 @@ func (service *AddURLService) Add(c *gin.Context, fs *filesystem.FileSystem, tas
// 获取 Aria2 实例
err, node := cluster.Default.BalanceNodeByFeature("aria2", lb)
if err != nil {
return serializer.Err(serializer.CodeInternalSetting, "Aria2 实例获取失败", err)
return serializer.Err(serializer.CodeInternalSetting, "Failed to get Aria2 instance", err)
}
// 创建任务
gid, err := node.GetAria2Instance().CreateTask(task, fs.User.Group.OptionsSerialized.Aria2Options)
if err != nil {
return serializer.Err(serializer.CodeNotSet, "任务创建失败", err)
return serializer.Err(serializer.CodeCreateTaskError, "", err)
}
task.GID = gid
task.NodeID = node.ID()
_, err = task.Create()
if err != nil {
return serializer.DBErr("任务创建失败", err)
return serializer.DBErr("Failed to create task record", err)
}
// 创建任务监控

@ -54,13 +54,13 @@ func (service *DownloadTaskService) Delete(c *gin.Context) serializer.Response {
// 查找下载记录
download, err := model.GetDownloadByGid(c.Param("gid"), user.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "下载记录不存在", err)
return serializer.Err(serializer.CodeNotFound, "Download record not found", err)
}
if download.Status >= common.Error {
// 如果任务已完成,则删除任务记录
if err := download.Delete(); err != nil {
return serializer.Err(serializer.CodeDBError, "任务记录删除失败", err)
return serializer.DBErr("Failed to delete task record", err)
}
return serializer.Response{}
}
@ -68,11 +68,11 @@ func (service *DownloadTaskService) Delete(c *gin.Context) serializer.Response {
// 取消任务
node := cluster.Default.GetNodeByID(download.GetNodeID())
if node == nil {
return serializer.Err(serializer.CodeInternalSetting, "目标节点不可用", err)
return serializer.Err(serializer.CodeNodeOffline, "", err)
}
if err := node.GetAria2Instance().Cancel(download); err != nil {
return serializer.Err(serializer.CodeNotSet, "操作失败", err)
return serializer.Err(serializer.CodeNotSet, "Operation failed", err)
}
return serializer.Response{}
@ -86,17 +86,17 @@ func (service *SelectFileService) Select(c *gin.Context) serializer.Response {
// 查找下载记录
download, err := model.GetDownloadByGid(c.Param("gid"), user.ID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "下载记录不存在", err)
return serializer.Err(serializer.CodeNotFound, "Download record not found", err)
}
if download.StatusInfo.BitTorrent.Mode != "multi" || (download.Status != common.Downloading && download.Status != common.Paused) {
return serializer.Err(serializer.CodeNoPermissionErr, "此下载任务无法选取文件", err)
return serializer.ParamErr("You cannot select files for this task", nil)
}
// 选取下载
node := cluster.Default.GetNodeByID(download.GetNodeID())
if err := node.GetAria2Instance().Select(download, service.Indexes); err != nil {
return serializer.Err(serializer.CodeNotSet, "操作失败", err)
return serializer.Err(serializer.CodeNotSet, "Operation failed", err)
}
return serializer.Response{}

@ -112,7 +112,7 @@ func ProcessCallback(service CallbackProcessService, c *gin.Context) serializer.
// 创建文件系统
fs, err := filesystem.NewFileSystemFromCallback(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, err.Error(), err)
}
defer fs.Recycle()
@ -156,7 +156,7 @@ func (service *OneDriveCallback) PreProcess(c *gin.Context) serializer.Response
// 创建文件系统
fs, err := filesystem.NewFileSystemFromCallback(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
@ -166,7 +166,7 @@ func (service *OneDriveCallback) PreProcess(c *gin.Context) serializer.Response
// 获取文件信息
info, err := fs.Handler.(onedrive.Driver).Client.Meta(context.Background(), "", uploadSession.SavePath)
if err != nil {
return serializer.Err(serializer.CodeUploadFailed, "文件元信息查询失败", err)
return serializer.Err(serializer.CodeQueryMetaFailed, "", err)
}
// 验证与回调会话中是否一致
@ -181,7 +181,7 @@ func (service *OneDriveCallback) PreProcess(c *gin.Context) serializer.Response
if isSizeCheckFailed || !strings.EqualFold(info.GetSourcePath(), actualPath) {
fs.Handler.(onedrive.Driver).Client.Delete(context.Background(), []string{info.GetSourcePath()})
return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err)
return serializer.Err(serializer.CodeMetaMismatch, "", err)
}
service.Meta = info
return ProcessCallback(service, c)
@ -192,7 +192,7 @@ func (service *COSCallback) PreProcess(c *gin.Context) serializer.Response {
// 创建文件系统
fs, err := filesystem.NewFileSystemFromCallback(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
@ -202,12 +202,12 @@ func (service *COSCallback) PreProcess(c *gin.Context) serializer.Response {
// 获取文件信息
info, err := fs.Handler.(cos.Driver).Meta(context.Background(), uploadSession.SavePath)
if err != nil {
return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err)
return serializer.Err(serializer.CodeMetaMismatch, "", err)
}
// 验证实际文件信息与回调会话中是否一致
if uploadSession.Size != info.Size || uploadSession.Key != info.CallbackKey {
return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err)
return serializer.Err(serializer.CodeMetaMismatch, "", err)
}
return ProcessCallback(service, c)
@ -218,7 +218,7 @@ func (service *S3Callback) PreProcess(c *gin.Context) serializer.Response {
// 创建文件系统
fs, err := filesystem.NewFileSystemFromCallback(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
@ -228,12 +228,12 @@ func (service *S3Callback) PreProcess(c *gin.Context) serializer.Response {
// 获取文件信息
info, err := fs.Handler.(*s3.Driver).Meta(context.Background(), uploadSession.SavePath)
if err != nil {
return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err)
return serializer.Err(serializer.CodeMetaMismatch, "", err)
}
// 验证实际文件信息与回调会话中是否一致
if uploadSession.Size != info.Size {
return serializer.Err(serializer.CodeUploadFailed, "文件信息不一致", err)
return serializer.Err(serializer.CodeMetaMismatch, "", err)
}
return ProcessCallback(service, c)
@ -244,7 +244,7 @@ func (service *UploadCallbackService) PreProcess(c *gin.Context) serializer.Resp
// 创建文件系统
fs, err := filesystem.NewFileSystemFromCallback(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
@ -254,7 +254,7 @@ func (service *UploadCallbackService) PreProcess(c *gin.Context) serializer.Resp
// 验证文件大小
if uploadSession.Size != service.Size {
fs.Handler.Delete(context.Background(), []string{uploadSession.SavePath})
return serializer.Err(serializer.CodeUploadFailed, "文件大小不一致", nil)
return serializer.Err(serializer.CodeMetaMismatch, "", err)
}
return ProcessCallback(service, c)

@ -18,7 +18,7 @@ func (service *DirectoryService) ListDirectory(c *gin.Context) serializer.Respon
// 创建文件系统
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
@ -48,7 +48,7 @@ func (service *DirectoryService) CreateDirectory(c *gin.Context) serializer.Resp
// 创建文件系统
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()

@ -267,7 +267,7 @@ func (service *DownloadService) Download(ctx context.Context, c *gin.Context) se
// 创建文件系统
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()

Loading…
Cancel
Save