Feat: copy files and folders

pull/247/head
HFO4 5 years ago
parent 85bbb3d122
commit c15b8a047d

@ -54,3 +54,25 @@ func TestCurrentUser(t *testing.T) {
asserts.NotNil(user) asserts.NotNil(user)
asserts.NoError(mock.ExpectationsWereMet()) asserts.NoError(mock.ExpectationsWereMet())
} }
func TestAuthRequired(t *testing.T) {
asserts := assert.New(t)
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request, _ = http.NewRequest("GET", "/test", nil)
AuthRequiredFunc := AuthRequired()
// 未登录
AuthRequiredFunc(c)
asserts.NotNil(c)
// 类型错误
c.Set("user", 123)
AuthRequiredFunc(c)
asserts.NotNil(c)
// 正常
c.Set("user", &model.User{})
AuthRequiredFunc(c)
asserts.NotNil(c)
}

@ -0,0 +1,7 @@
package middleware
import "testing"
func TestMockHelper(t *testing.T) {
}

@ -108,28 +108,63 @@ func DeleteFolderByIDs(ids []uint) error {
return result.Error return result.Error
} }
//func (folder *Folder)GetPositionAbsolute()string{ // MoveOrCopyFileTo 将此目录下的files移动或复制至dstFolder
// return path.Join(folder.Position,folder.Name) // 返回此操作新增的容量
//} func (folder *Folder) MoveOrCopyFileTo(files []string, dstFolder *Folder, isCopy bool) (uint64, error) {
// 已复制文件的总大小
// MoveFileTo 将此目录下的文件移动至dstFolder var copiedSize uint64
func (folder *Folder) MoveFileTo(files []string, dstFolder *Folder) error {
// 更改顶级要移动文件的父目录指向 if isCopy {
err := DB.Model(File{}).Where("name in (?) and user_id = ? and dir = ?", files, folder.OwnerID, folder.PositionAbsolute). // 检索出要复制的文件
Update(map[string]interface{}{ var originFiles = make([]File, 0, len(files))
"folder_id": dstFolder.ID, if err := DB.Where(
"dir": dstFolder.PositionAbsolute, "name in (?) and user_id = ? and dir = ?",
}). files,
Error folder.OwnerID,
if err != nil { folder.PositionAbsolute,
return err ).Find(&originFiles).Error; err != nil {
return 0, err
}
// 复制文件记录
for _, oldFile := range originFiles {
oldFile.Model = gorm.Model{}
oldFile.FolderID = dstFolder.ID
oldFile.Dir = dstFolder.PositionAbsolute
if err := DB.Create(&oldFile).Error; err != nil {
return copiedSize, err
}
copiedSize += oldFile.Size
}
} else {
// 更改顶级要移动文件的父目录指向
err := DB.Model(File{}).Where(
"name in (?) and user_id = ? and dir = ?",
files,
folder.OwnerID,
folder.PositionAbsolute,
).
Update(map[string]interface{}{
"folder_id": dstFolder.ID,
"dir": dstFolder.PositionAbsolute,
}).
Error
if err != nil {
return 0, err
}
} }
return nil
return copiedSize, nil
} }
// RenameFolderTo 将folder目录下的dirs子目录复制或移动到dstFolder // MoveOrCopyFolderTo 将folder目录下的dirs子目录复制或移动到dstFolder
func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bool) error { // 返回此过程中增加的容量
func (folder *Folder) MoveOrCopyFolderTo(dirs []string, dstFolder *Folder, isCopy bool) (uint64, error) {
// 生成绝对路径 // 生成绝对路径
fullDirs := make([]string, len(dirs)) fullDirs := make([]string, len(dirs))
for i := 0; i < len(dirs); i++ { for i := 0; i < len(dirs); i++ {
@ -146,13 +181,15 @@ func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bo
// 检索被移动的目录的所有子目录 // 检索被移动的目录的所有子目录
toBeMoved, err := GetRecursiveChildFolder([]string{parentDir}, folder.OwnerID, true) toBeMoved, err := GetRecursiveChildFolder([]string{parentDir}, folder.OwnerID, true)
if err != nil { if err != nil {
return err return 0, err
} }
subFolders[key] = toBeMoved subFolders[key] = toBeMoved
} }
// 记录复制要用到的父目录源路径和新的ID // 记录复制要用到的父目录源路径和新的ID
var copyCache = make(map[string]uint) var copyCache = make(map[string]uint)
// 记录已复制文件的容量
var newUsedStorage uint64
var err error var err error
if isCopy { if isCopy {
@ -160,7 +197,7 @@ func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bo
// TODO:支持多目录 // TODO:支持多目录
origin := Folder{} origin := Folder{}
if DB.Where("position_absolute in (?) and owner_id = ?", fullDirs, folder.OwnerID).Find(&origin).Error != nil { if DB.Where("position_absolute in (?) and owner_id = ?", fullDirs, folder.OwnerID).Find(&origin).Error != nil {
return errors.New("找不到原始目录") return 0, errors.New("找不到原始目录")
} }
oldPosition := origin.PositionAbsolute oldPosition := origin.PositionAbsolute
@ -174,7 +211,7 @@ func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bo
origin.Model = gorm.Model{} origin.Model = gorm.Model{}
if err := DB.Create(&origin).Error; err != nil { if err := DB.Create(&origin).Error; err != nil {
return err return 0, err
} }
// 记录新的主键 // 记录新的主键
@ -185,14 +222,20 @@ func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bo
// 更改顶级要移动目录的父目录指向 // 更改顶级要移动目录的父目录指向
err = DB.Model(Folder{}).Where("position_absolute in (?) and owner_id = ?", fullDirs, folder.OwnerID). err = DB.Model(Folder{}).Where("position_absolute in (?) and owner_id = ?", fullDirs, folder.OwnerID).
Update(map[string]interface{}{ Update(map[string]interface{}{
"parent_id": dstFolder.ID, "parent_id": dstFolder.ID,
"position": dstFolder.PositionAbsolute, "position": dstFolder.PositionAbsolute,
"position_absolute": gorm.Expr(util.BuildConcat("?", "name", conf.DatabaseConfig.Type), util.FillSlash(dstFolder.PositionAbsolute)), "position_absolute": gorm.Expr(
util.BuildConcat("?",
"name",
conf.DatabaseConfig.Type,
),
util.FillSlash(dstFolder.PositionAbsolute),
),
}).Error }).Error
} }
if err != nil { if err != nil {
return err return 0, err
} }
// 更新被移动的目录递归的子目录和文件 // 更新被移动的目录递归的子目录和文件
@ -200,6 +243,12 @@ func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bo
ignorePath := fullDirs[parKey] ignorePath := fullDirs[parKey]
// TODO 找到更好的修改办法 // TODO 找到更好的修改办法
// 抽离所有子目录的ID
var subFolderIDs = make([]uint, len(toBeMoved))
for key, subFolder := range toBeMoved {
subFolderIDs[key] = subFolder.ID
}
if isCopy { if isCopy {
index := 0 index := 0
for len(toBeMoved) != 0 { for len(toBeMoved) != 0 {
@ -213,7 +262,10 @@ func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bo
// 如果缓存中存在父目录ID执行复制,并删除 // 如果缓存中存在父目录ID执行复制,并删除
if newID, ok := copyCache[toBeMoved[innerIndex].Position]; ok { if newID, ok := copyCache[toBeMoved[innerIndex].Position]; ok {
// 记录目录原来的路径
oldPosition := toBeMoved[innerIndex].PositionAbsolute oldPosition := toBeMoved[innerIndex].PositionAbsolute
// 设置目录i虚拟的路径
newPosition := path.Join( newPosition := path.Join(
dstFolder.PositionAbsolute, strings.Replace( dstFolder.PositionAbsolute, strings.Replace(
toBeMoved[innerIndex].Position, toBeMoved[innerIndex].Position,
@ -224,8 +276,10 @@ func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bo
toBeMoved[innerIndex].ParentID = newID toBeMoved[innerIndex].ParentID = newID
toBeMoved[innerIndex].Model = gorm.Model{} toBeMoved[innerIndex].Model = gorm.Model{}
if err := DB.Create(&toBeMoved[innerIndex]).Error; err != nil { if err := DB.Create(&toBeMoved[innerIndex]).Error; err != nil {
return err return 0, err
} }
// 将当前目录老路径和新ID保存以便后续待处理目录文件使用
copyCache[oldPosition] = toBeMoved[innerIndex].Model.ID copyCache[oldPosition] = toBeMoved[innerIndex].Model.ID
toBeMoved = append(toBeMoved[:innerIndex], toBeMoved[innerIndex+1:]...) toBeMoved = append(toBeMoved[:innerIndex], toBeMoved[innerIndex+1:]...)
} }
@ -235,7 +289,13 @@ func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bo
for _, subFolder := range toBeMoved { for _, subFolder := range toBeMoved {
// 每个分组的第一个目录已经变更指向,直接跳过 // 每个分组的第一个目录已经变更指向,直接跳过
if subFolder.PositionAbsolute != ignorePath { if subFolder.PositionAbsolute != ignorePath {
newPosition := path.Join(dstFolder.PositionAbsolute, strings.Replace(subFolder.Position, folder.PositionAbsolute, "", 1)) newPosition := path.Join(dstFolder.PositionAbsolute,
strings.Replace(subFolder.Position,
folder.PositionAbsolute,
"",
1,
),
)
// 移动 // 移动
DB.Model(&subFolder).Updates(map[string]interface{}{ DB.Model(&subFolder).Updates(map[string]interface{}{
"position": newPosition, "position": newPosition,
@ -245,26 +305,38 @@ func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bo
} }
} }
// 抽离所有子目录的ID
var subFolderIDs = make([]uint, len(toBeMoved))
for key, subFolder := range toBeMoved {
subFolderIDs[key] = subFolder.ID
}
// 获取子目录下的所有子文件 // 获取子目录下的所有子文件
toBeMovedFile, err := GetFilesByParentIDs(subFolderIDs, folder.OwnerID) toBeMovedFile, err := GetFilesByParentIDs(subFolderIDs, folder.OwnerID)
if err != nil { if err != nil {
return err return 0, err
} }
// 开始复制或移动子文件
for _, subFile := range toBeMovedFile { for _, subFile := range toBeMovedFile {
newPosition := path.Join(dstFolder.PositionAbsolute, strings.Replace(subFile.Dir, folder.PositionAbsolute, "", 1)) newPosition := path.Join(dstFolder.PositionAbsolute,
strings.Replace(
subFile.Dir,
folder.PositionAbsolute,
"",
1,
),
)
if isCopy { if isCopy {
// 复制 // 复制
if newID, ok := copyCache[subFile.Dir]; ok {
subFile.FolderID = newID
} else {
util.Log().Debug("无法找到文件的父目录ID原始路径%s", subFile.Dir)
}
subFile.Dir = newPosition subFile.Dir = newPosition
subFile.Model = gorm.Model{} subFile.Model = gorm.Model{}
// 复制文件记录
if err := DB.Create(&subFile).Error; err != nil { if err := DB.Create(&subFile).Error; err != nil {
util.Log().Warning("无法复制子文件:%s", err) util.Log().Warning("无法复制子文件:%s", err)
} else {
// 记录此文件容量
newUsedStorage += subFile.Size
} }
} else { } else {
DB.Model(&subFile).Updates(map[string]interface{}{ DB.Model(&subFile).Updates(map[string]interface{}{
@ -276,6 +348,6 @@ func (folder *Folder) RenameFolderTo(dirs []string, dstFolder *Folder, isCopy bo
} }
return nil return newUsedStorage, nil
} }

@ -85,6 +85,16 @@ func (user *User) IncreaseStorage(size uint64) bool {
return false return false
} }
// IncreaseStorageWithoutCheck 忽略可用容量,增加用户已用容量
func (user *User) IncreaseStorageWithoutCheck(size uint64) {
if size == 0 {
return
}
user.Storage += size
DB.Model(user).UpdateColumn("storage", gorm.Expr("storage + ?", size))
}
// GetRemainingCapacity 获取剩余配额 // GetRemainingCapacity 获取剩余配额
func (user *User) GetRemainingCapacity() uint64 { func (user *User) GetRemainingCapacity() uint64 {
if user.Group.MaxStorage <= user.Storage { if user.Group.MaxStorage <= user.Storage {

@ -35,12 +35,25 @@ func (fs *FileSystem) Copy(ctx context.Context, dirs, files []string, src, dst s
return ErrPathNotExist return ErrPathNotExist
} }
// 记录复制的文件的总容量
var newUsedStorage uint64
// 复制目录 // 复制目录
if len(dirs) > 0 { if len(dirs) > 0 {
err := srcFolder.RenameFolderTo(dirs, dstFolder, true) subFileSizes, err := srcFolder.MoveOrCopyFolderTo(dirs, dstFolder, true)
if err != nil {
return serializer.NewError(serializer.CodeDBError, "操作失败,可能有重名冲突", err)
}
newUsedStorage += subFileSizes
}
// 复制文件
if len(files) > 0 {
subFileSizes, err := srcFolder.MoveOrCopyFileTo(files, dstFolder, true)
if err != nil { if err != nil {
return serializer.NewError(serializer.CodeDBError, "操作失败,可能有重名冲突", err) return serializer.NewError(serializer.CodeDBError, "操作失败,可能有重名冲突", err)
} }
newUsedStorage += subFileSizes
} }
return nil return nil
@ -57,13 +70,13 @@ func (fs *FileSystem) Move(ctx context.Context, dirs, files []string, src, dst s
} }
// 处理目录及子文件移动 // 处理目录及子文件移动
err := srcFolder.RenameFolderTo(dirs, dstFolder, false) _, err := srcFolder.MoveOrCopyFolderTo(dirs, dstFolder, false)
if err != nil { if err != nil {
return serializer.NewError(serializer.CodeDBError, "操作失败,可能有重名冲突", err) return serializer.NewError(serializer.CodeDBError, "操作失败,可能有重名冲突", err)
} }
// 处理文件移动 // 处理文件移动
err = srcFolder.MoveFileTo(files, dstFolder) _, err = srcFolder.MoveOrCopyFileTo(files, dstFolder, false)
if err != nil { if err != nil {
return serializer.NewError(serializer.CodeDBError, "操作失败,可能有重名冲突", err) return serializer.NewError(serializer.CodeDBError, "操作失败,可能有重名冲突", err)
} }

Loading…
Cancel
Save