From d28e5632d5105cf07d7b6bac2450bc0588cdd361 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Sun, 1 Dec 2019 19:45:01 +0800 Subject: [PATCH] Fix: unique index check failed due to soft delete --- models/file.go | 2 +- models/folder.go | 10 ++-------- pkg/filesystem/path.go | 37 +++++++++++++++++++------------------ pkg/serializer/user.go | 18 ++++++++++-------- 4 files changed, 32 insertions(+), 35 deletions(-) diff --git a/models/file.go b/models/file.go index 08442a1..5bd2121 100644 --- a/models/file.go +++ b/models/file.go @@ -123,7 +123,7 @@ func RemoveFilesWithSoftLinks(files []File) ([]File, error) { // DeleteFileByIDs 根据给定ID批量删除文件记录 func DeleteFileByIDs(ids []uint) error { - result := DB.Where("id in (?)", ids).Delete(&File{}) + result := DB.Where("id in (?)", ids).Unscoped().Delete(&File{}) return result.Error } diff --git a/models/folder.go b/models/folder.go index b74037a..73d2c6d 100644 --- a/models/folder.go +++ b/models/folder.go @@ -52,7 +52,7 @@ func GetRecursiveChildFolder(dirs []string, uid uint) ([]Folder, error) { // DeleteFolderByIDs 根据给定ID批量删除目录记录 func DeleteFolderByIDs(ids []uint) error { - result := DB.Where("id in (?)", ids).Delete(&Folder{}) + result := DB.Where("id in (?)", ids).Unscoped().Delete(&Folder{}) return result.Error } @@ -62,14 +62,8 @@ func DeleteFolderByIDs(ids []uint) error { // MoveFileTo 将此目录下的文件递归移动至dstFolder func (folder *Folder) MoveFileTo(files []string, dstFolder *Folder) error { - // 生成绝对路径 - fullFilePath := make([]string, len(files)) - for i := 0; i < len(files); i++ { - fullFilePath[i] = path.Join(folder.PositionAbsolute, files[i]) - } - // 更改顶级要移动文件的父目录指向 - err := DB.Model(File{}).Where("dir in (?) and user_id = ?", fullFilePath, folder.OwnerID). + err := DB.Model(File{}).Where("dir in (?) and user_id = ?", folder.PositionAbsolute, folder.OwnerID). Update(map[string]interface{}{ "folder_id": dstFolder.ID, "dir": dstFolder.PositionAbsolute, diff --git a/pkg/filesystem/path.go b/pkg/filesystem/path.go index dd98a4d..eef312b 100644 --- a/pkg/filesystem/path.go +++ b/pkg/filesystem/path.go @@ -77,31 +77,32 @@ func (fs *FileSystem) Delete(ctx context.Context, dirs, files []string) error { if err != nil { return err } - // 去除待删除文件中包含软连接的部分 - filesToBeDelete, err := model.RemoveFilesWithSoftLinks(fs.FileTarget) - if err != nil { - return ErrDBListObjects.WithError(err) - } + } - // 根据存储策略将文件分组 - policyGroup := fs.GroupFileByPolicy(ctx, filesToBeDelete) + // 去除待删除文件中包含软连接的部分 + filesToBeDelete, err := model.RemoveFilesWithSoftLinks(fs.FileTarget) + if err != nil { + return ErrDBListObjects.WithError(err) + } - // 按照存储策略分组删除对象 - failed := fs.deleteGroupedFile(ctx, policyGroup) + // 根据存储策略将文件分组 + policyGroup := fs.GroupFileByPolicy(ctx, filesToBeDelete) - for i := 0; i < len(fs.FileTarget); i++ { - if util.ContainsString(failed[fs.FileTarget[i].PolicyID], fs.FileTarget[i].SourceName) { - // TODO 删除失败时不删除文件记录及父目录 - } else { - deletedFileIDs = append(deletedFileIDs, fs.FileTarget[i].ID) - } - deletedStorage[fs.FileTarget[i].ID] = fs.FileTarget[i].Size - allFileIDs = append(allFileIDs, fs.FileTarget[i].ID) + // 按照存储策略分组删除对象 + failed := fs.deleteGroupedFile(ctx, policyGroup) + + for i := 0; i < len(fs.FileTarget); i++ { + if util.ContainsString(failed[fs.FileTarget[i].PolicyID], fs.FileTarget[i].SourceName) { + // TODO 删除失败时不删除文件记录及父目录 + } else { + deletedFileIDs = append(deletedFileIDs, fs.FileTarget[i].ID) } + deletedStorage[fs.FileTarget[i].ID] = fs.FileTarget[i].Size + allFileIDs = append(allFileIDs, fs.FileTarget[i].ID) } // 删除文件记录 - err := model.DeleteFileByIDs(allFileIDs) + err = model.DeleteFileByIDs(allFileIDs) if err != nil { return ErrDBDeleteObjects.WithError(err) } diff --git a/pkg/serializer/user.go b/pkg/serializer/user.go index a021205..b61dfa3 100644 --- a/pkg/serializer/user.go +++ b/pkg/serializer/user.go @@ -27,10 +27,11 @@ type User struct { } type Policy struct { - SaveType string `json:"saveType"` - MaxSize string `json:"maxSize"` - AllowedType []string `json:"allowedType"` - UploadURL string `json:"upUrl"` + SaveType string `json:"saveType"` + MaxSize string `json:"maxSize"` + AllowedType []string `json:"allowedType"` + UploadURL string `json:"upUrl"` + AllowGetSource bool `json:"allowSource"` } type Group struct { @@ -51,10 +52,11 @@ func BuildUser(user model.User) User { CreatedAt: user.CreatedAt.Unix(), PreferredTheme: user.OptionsSerialized.PreferredTheme, Policy: Policy{ - SaveType: user.Policy.Type, - MaxSize: fmt.Sprintf("%.2fmb", float64(user.Policy.MaxSize)/1024*1024), - AllowedType: user.Policy.OptionsSerialized.FileType, - UploadURL: user.Policy.Server, + SaveType: user.Policy.Type, + MaxSize: fmt.Sprintf("%.2fmb", float64(user.Policy.MaxSize)/1024*1024), + AllowedType: user.Policy.OptionsSerialized.FileType, + UploadURL: user.Policy.Server, + AllowGetSource: user.Policy.IsOriginLinkEnable, }, Group: Group{ AllowShare: user.Group.ShareEnabled,