i18n: file operation

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

@ -73,11 +73,11 @@ const (
// CodeMasterNotFound 主机节点未注册
CodeMasterNotFound = 40009
// CodeUploadSessionExpired 上传会话已过期
CodeUploadSessionExpired = 400011
CodeUploadSessionExpired = 40011
// CodeInvalidChunkIndex 无效的分片序号
CodeInvalidChunkIndex = 400012
CodeInvalidChunkIndex = 40012
// CodeInvalidContentLength 无效的正文长度
CodeInvalidContentLength = 400013
CodeInvalidContentLength = 40013
// CodeBatchSourceSize 超出批量获取外链限制
CodeBatchSourceSize = 40014
// CodeBatchAria2Size 超出最大 Aria2 任务数量限制
@ -160,6 +160,10 @@ const (
CodeConflictUploadOngoing = 40054
// 文件信息不一致
CodeMetaMismatch = 40055
// 不支持该格式的压缩文件
CodeUnsupportedArchiveType = 40056
// 可用存储策略发生变化
CodePolicyChanged = 40057
// CodeDBError 数据库操作失败
CodeDBError = 50001
// CodeEncryptError 加密失败

@ -132,14 +132,14 @@ func Thumb(c *gin.Context) {
// 获取文件ID
fileID, ok := c.Get("object_id")
if !ok {
c.JSON(200, serializer.ParamErr("文件不存在", err))
c.JSON(200, serializer.Err(serializer.CodeFileNotFound, "", err))
return
}
// 获取缩略图
resp, err := fs.GetThumb(ctx, fileID.(uint))
if err != nil {
c.JSON(200, serializer.Err(serializer.CodeNotSet, "无法获取缩略图", err))
c.JSON(200, serializer.Err(serializer.CodeNotSet, "Failed to get thumbnail", err))
return
}

@ -51,7 +51,7 @@ func (service *SingleFileService) Create(c *gin.Context) serializer.Response {
// 创建文件系统
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()
@ -101,21 +101,21 @@ func (service *SlaveListService) List(c *gin.Context) serializer.Response {
func (service *ArchiveService) DownloadArchived(ctx context.Context, c *gin.Context) serializer.Response {
userRaw, exist := cache.Get("archive_user_" + service.ID)
if !exist {
return serializer.Err(404, "归档会话不存在", nil)
return serializer.Err(serializer.CodeNotFound, "Archive session not exist", nil)
}
user := userRaw.(model.User)
// 创建文件系统
fs, err := filesystem.NewFileSystem(&user)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
// 查找打包的临时文件
archiveSession, exist := cache.Get("archive_" + service.ID)
if !exist {
return serializer.Err(404, "归档会话不存在", nil)
return serializer.Err(serializer.CodeNotFound, "Archive session not exist", nil)
}
// 开始打包
@ -126,7 +126,7 @@ func (service *ArchiveService) DownloadArchived(ctx context.Context, c *gin.Cont
ctx = context.WithValue(ctx, fsctx.GinCtx, c)
err = fs.Compress(ctx, c.Writer, items.Dirs, items.Items, true)
if err != nil {
return serializer.Err(serializer.CodeNotSet, "无法创建压缩文件", err)
return serializer.Err(serializer.CodeNotSet, "Failed to compress file", err)
}
return serializer.Response{
@ -138,7 +138,7 @@ func (service *ArchiveService) DownloadArchived(ctx context.Context, c *gin.Cont
func (service *FileAnonymousGetService) Download(ctx context.Context, c *gin.Context) serializer.Response {
fs, err := filesystem.NewAnonymousFileSystem()
if err != nil {
return serializer.Err(serializer.CodeGroupNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
@ -167,7 +167,7 @@ func (service *FileAnonymousGetService) Download(ctx context.Context, c *gin.Con
func (service *FileAnonymousGetService) Source(ctx context.Context, c *gin.Context) serializer.Response {
fs, err := filesystem.NewAnonymousFileSystem()
if err != nil {
return serializer.Err(serializer.CodeGroupNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
@ -243,7 +243,7 @@ func (service *FileIDService) CreateDownloadSession(ctx context.Context, c *gin.
// 创建文件系统
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()
@ -274,7 +274,7 @@ func (service *DownloadService) Download(ctx context.Context, c *gin.Context) se
// 查找打包的临时文件
file, exist := cache.Get("download_" + service.ID)
if !exist {
return serializer.Err(404, "文件下载会话不存在", nil)
return serializer.Err(serializer.CodeNotFound, "Download session not exist", nil)
}
fs.FileTarget = []model.File{file.(model.File)}
@ -308,7 +308,7 @@ func (service *FileIDService) PreviewContent(ctx context.Context, c *gin.Context
// 创建文件系统
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()
@ -327,7 +327,7 @@ func (service *FileIDService) PreviewContent(ctx context.Context, c *gin.Context
path := ctx.Value(fsctx.PathCtx).(string)
err := fs.ResetFileIfNotExist(ctx, path)
if err != nil {
return serializer.Err(serializer.CodeNotFound, err.Error(), err)
return serializer.Err(serializer.CodeFileNotFound, err.Error(), err)
}
objectID = uint(0)
}
@ -371,7 +371,7 @@ func (service *FileIDService) PutContent(ctx context.Context, c *gin.Context) se
fileSize, err := strconv.ParseUint(c.Request.Header.Get("Content-Length"), 10, 64)
if err != nil {
return serializer.ParamErr("无法解析文件尺寸", err)
return serializer.ParamErr("Invalid content-length value", err)
}
fileData := fsctx.FileStream{
@ -384,7 +384,7 @@ func (service *FileIDService) PutContent(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)
}
uploadCtx := context.WithValue(ctx, fsctx.GinCtx, c)
@ -392,7 +392,7 @@ func (service *FileIDService) PutContent(ctx context.Context, c *gin.Context) se
fileID, _ := c.Get("object_id")
originFile, _ := model.GetFilesByIDs([]uint{fileID.(uint)}, fs.User.ID)
if len(originFile) == 0 {
return serializer.Err(404, "文件不存在", nil)
return serializer.Err(serializer.CodeFileNotFound, "", nil)
}
fileData.Name = originFile[0].Name
@ -433,12 +433,12 @@ func (service *FileIDService) PutContent(ctx context.Context, c *gin.Context) se
func (s *ItemIDService) Sources(ctx context.Context, c *gin.Context) serializer.Response {
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, "无法初始化文件系统", err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
defer fs.Recycle()
if len(s.Raw().Items) > fs.User.Group.OptionsSerialized.SourceBatchSize {
return serializer.Err(serializer.CodeBatchSourceSize, "超出批量获取外链的最大数量限制", err)
return serializer.Err(serializer.CodeBatchSourceSize, "", err)
}
res := make([]serializer.Sources, 0, len(s.Raw().Items))

@ -102,30 +102,30 @@ func (service *ItemDecompressService) CreateDecompressTask(c *gin.Context) seria
// 创建文件系统
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.ArchiveTask {
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)
}
// 压缩包是否存在
exist, file := fs.IsFileExist(service.Src)
if !exist {
return serializer.Err(serializer.CodeNotFound, "文件不存在", nil)
return serializer.Err(serializer.CodeFileNotFound, "", nil)
}
// 文件尺寸限制
if fs.User.Group.OptionsSerialized.DecompressSize != 0 && file.Size > fs.User.Group.
OptionsSerialized.DecompressSize {
return serializer.Err(serializer.CodeParamErr, "文件太大", nil)
return serializer.Err(serializer.CodeFileTooLarge, "", nil)
}
// 支持的压缩格式后缀
@ -140,13 +140,13 @@ func (service *ItemDecompressService) CreateDecompressTask(c *gin.Context) seria
}
}
if !matched {
return serializer.Err(serializer.CodeParamErr, "不支持该格式的压缩文件", nil)
return serializer.Err(serializer.CodeUnsupportedArchiveType, "", nil)
}
// 创建任务
job, err := task.NewDecompressTask(fs.User, service.Src, service.Dst, service.Encoding)
if err != nil {
return serializer.Err(serializer.CodeNotSet, "任务创建失败", err)
return serializer.Err(serializer.CodeCreateTaskError, "", err)
}
task.TaskPoll.Submit(job)
@ -159,13 +159,13 @@ func (service *ItemCompressService) CreateCompressTask(c *gin.Context) serialize
// 创建文件系统
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.ArchiveTask {
return serializer.Err(serializer.CodeGroupNotAllowed, "当前用户组无法进行此操作", nil)
return serializer.Err(serializer.CodeGroupNotAllowed, "", nil)
}
// 补齐压缩文件扩展名(如果没有)
@ -175,30 +175,30 @@ func (service *ItemCompressService) CreateCompressTask(c *gin.Context) serialize
// 存放目录是否存在,是否重名
if exist, _ := fs.IsPathExist(service.Dst); !exist {
return serializer.Err(serializer.CodeNotFound, "存放路径不存在", nil)
return serializer.Err(serializer.CodeParentNotExist, "", nil)
}
if exist, _ := fs.IsFileExist(path.Join(service.Dst, service.Name)); exist {
return serializer.ParamErr("名为 "+service.Name+" 的文件已存在", nil)
return serializer.ParamErr("File "+service.Name+" already exist", nil)
}
// 检查文件名合法性
if !fs.ValidateLegalName(context.Background(), service.Name) {
return serializer.ParamErr("文件名非法", nil)
return serializer.Err(serializer.CodeIllegalObjectName, "", nil)
}
if !fs.ValidateExtension(context.Background(), service.Name) {
return serializer.ParamErr("不允许存储此扩展名的文件", nil)
return serializer.Err(serializer.CodeFileTypeNotAllowed, "", nil)
}
// 递归列出待压缩子目录
folders, err := model.GetRecursiveChildFolder(service.Src.Raw().Dirs, fs.User.ID, true)
if err != nil {
return serializer.Err(serializer.CodeDBError, "无法列出子目录", err)
return serializer.DBErr("Failed to list folders", err)
}
// 列出所有待压缩文件
files, err := model.GetChildFilesOfFolders(&folders)
if err != nil {
return serializer.Err(serializer.CodeDBError, "无法列出子文件", err)
return serializer.DBErr("Failed to list files", err)
}
// 计算待压缩文件大小
@ -210,21 +210,21 @@ func (service *ItemCompressService) CreateCompressTask(c *gin.Context) serialize
// 文件尺寸限制
if fs.User.Group.OptionsSerialized.CompressSize != 0 && totalSize > fs.User.Group.
OptionsSerialized.CompressSize {
return serializer.Err(serializer.CodeParamErr, "文件太大", nil)
return serializer.Err(serializer.CodeFileTooLarge, "", nil)
}
// 按照平均压缩率计算用户空间是否足够
compressRatio := 0.4
spaceNeeded := uint64(math.Round(float64(totalSize) * compressRatio))
if fs.User.GetRemainingCapacity() < spaceNeeded {
return serializer.Err(serializer.CodeParamErr, "剩余空间不足", err)
return serializer.Err(serializer.CodeInsufficientCapacity, "", err)
}
// 创建任务
job, err := task.NewCompressTask(fs.User, path.Join(service.Dst, service.Name), service.Src.Raw().Dirs,
service.Src.Raw().Items)
if err != nil {
return serializer.Err(serializer.CodeNotSet, "任务创建失败", err)
return serializer.Err(serializer.CodeCreateTaskError, "", err)
}
task.TaskPoll.Submit(job)
@ -237,13 +237,13 @@ func (service *ItemIDService) Archive(ctx context.Context, c *gin.Context) seria
// 创建文件系统
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.ArchiveDownload {
return serializer.Err(serializer.CodeGroupNotAllowed, "当前用户组无法进行此操作", nil)
return serializer.Err(serializer.CodeGroupNotAllowed, "", nil)
}
// 创建打包下载会话

@ -23,14 +23,14 @@ func (service *ItemSearchService) Search(c *gin.Context) serializer.Response {
// 创建文件系统
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 service.Path != "" {
ok, parent := fs.IsPathExist(service.Path)
if !ok {
return serializer.Err(serializer.CodeParentNotExist, "Cannot find parent folder", nil)
return serializer.Err(serializer.CodeParentNotExist, "", nil)
}
fs.Root = parent
@ -60,9 +60,9 @@ func (service *ItemSearchService) Search(c *gin.Context) serializer.Response {
}
}
}
return serializer.Err(serializer.CodeNotFound, "标签不存在", nil)
return serializer.Err(serializer.CodeNotFound, "", nil)
default:
return serializer.ParamErr("未知搜索类型", nil)
return serializer.ParamErr("Unknown search type", nil)
}
}

@ -33,13 +33,13 @@ func (service *CreateUploadSessionService) Create(ctx context.Context, c *gin.Co
// 创建文件系统
fs, err := filesystem.NewFileSystemFromContext(c)
if err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
return serializer.Err(serializer.CodeCreateFSError, "", err)
}
// 取得存储策略的ID
rawID, err := hashid.DecodeHashID(service.PolicyID, hashid.PolicyID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "存储策略不存在", err)
return serializer.Err(serializer.CodePolicyNotExist, "", err)
}
if fs.Policy.ID != rawID {
@ -77,7 +77,7 @@ type UploadService struct {
func (service *UploadService) LocalUpload(ctx context.Context, c *gin.Context) serializer.Response {
uploadSessionRaw, ok := cache.Get(filesystem.UploadSessionCachePrefix + service.ID)
if !ok {
return serializer.Err(serializer.CodeUploadSessionExpired, "LocalUpload session expired or not exist", nil)
return serializer.Err(serializer.CodeUploadSessionExpired, "", nil)
}
uploadSession := uploadSessionRaw.(serializer.UploadSession)
@ -88,23 +88,23 @@ func (service *UploadService) LocalUpload(ctx context.Context, c *gin.Context) s
}
if uploadSession.UID != fs.User.ID {
return serializer.Err(serializer.CodeUploadSessionExpired, "Local upload session expired or not exist", nil)
return serializer.Err(serializer.CodeUploadSessionExpired, "", nil)
}
// 查找上传会话创建的占位文件
file, err := model.GetFilesByUploadSession(service.ID, fs.User.ID)
if err != nil {
return serializer.Err(serializer.CodeUploadSessionExpired, "Local upload session file placeholder not exist", err)
return serializer.Err(serializer.CodeUploadSessionExpired, "", err)
}
// 重设 fs 存储策略
if !uploadSession.Policy.IsTransitUpload(uploadSession.Size) {
return serializer.Err(serializer.CodePolicyNotAllowed, "Storage policy not supported", err)
return serializer.Err(serializer.CodePolicyNotAllowed, "", err)
}
fs.Policy = &uploadSession.Policy
if err := fs.DispatchHandler(); err != nil {
return serializer.Err(serializer.CodePolicyNotAllowed, "Unknown storage policy", err)
return serializer.Err(serializer.CodePolicyNotExist, "", err)
}
expectedSizeStart := file.Size
@ -118,7 +118,7 @@ func (service *UploadService) LocalUpload(ctx context.Context, c *gin.Context) s
}
if expectedSizeStart > actualSizeStart {
util.Log().Info("尝试上传覆盖分片[%d] Start=%d", service.Index, actualSizeStart)
util.Log().Info("Trying to overwrite chunk[%d] Start=%d", service.Index, actualSizeStart)
}
return processChunkUpload(ctx, c, fs, &uploadSession, service.Index, file, fsctx.Append)
@ -128,7 +128,7 @@ func (service *UploadService) LocalUpload(ctx context.Context, c *gin.Context) s
func (service *UploadService) SlaveUpload(ctx context.Context, c *gin.Context) serializer.Response {
uploadSessionRaw, ok := cache.Get(filesystem.UploadSessionCachePrefix + service.ID)
if !ok {
return serializer.Err(serializer.CodeUploadSessionExpired, "Slave upload session expired or not exist", nil)
return serializer.Err(serializer.CodeUploadSessionExpired, "", nil)
}
uploadSession := uploadSessionRaw.(serializer.UploadSession)
@ -226,14 +226,14 @@ func (service *UploadSessionService) Delete(ctx context.Context, c *gin.Context)
// 创建文件系统
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()
// 查找需要删除的上传会话的占位文件
file, err := model.GetFilesByUploadSession(service.ID, fs.User.ID)
if err != nil {
return serializer.Err(serializer.CodeUploadSessionExpired, "Local Upload session file placeholder not exist", err)
return serializer.Err(serializer.CodeUploadSessionExpired, "", err)
}
// 删除文件
@ -271,7 +271,7 @@ func DeleteAllUploadSession(ctx context.Context, 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()

Loading…
Cancel
Save