Cloudreve/models/folder.go

203 lines
5.7 KiB
Go
Raw Normal View History

2019-11-15 16:32:43 +08:00
package model
import (
2019-12-01 18:31:29 +08:00
"github.com/HFO4/cloudreve/pkg/conf"
"github.com/HFO4/cloudreve/pkg/util"
"github.com/jinzhu/gorm"
2019-12-01 18:31:29 +08:00
"path"
"strings"
)
2019-11-15 16:32:43 +08:00
// Folder 目录
type Folder struct {
// 表字段
gorm.Model
Name string `gorm:"unique_index:idx_only_one_name"`
ParentID uint `gorm:"index:parent_id;unique_index:idx_only_one_name"`
Position string `gorm:"size:65536"`
OwnerID uint `gorm:"index:owner_id"`
PositionAbsolute string `gorm:"size:65536"`
}
2019-11-15 16:32:43 +08:00
// Create 创建目录
func (folder *Folder) Create() (uint, error) {
if err := DB.Create(folder).Error; err != nil {
util.Log().Warning("无法插入目录记录, %s", err)
return 0, err
}
return folder.ID, nil
2019-11-15 16:32:43 +08:00
}
2019-11-18 19:09:56 +08:00
// GetFolderByPath 根据绝对路径和UID查找目录
func GetFolderByPath(path string, uid uint) (Folder, error) {
var folder Folder
2019-11-24 13:06:15 +08:00
result := DB.Where("owner_id = ? AND position_absolute = ?", uid, path).First(&folder)
2019-11-18 19:09:56 +08:00
return folder, result.Error
}
2019-11-24 13:06:15 +08:00
// GetChildFolder 查找子目录
2019-11-24 13:06:15 +08:00
func (folder *Folder) GetChildFolder() ([]Folder, error) {
var folders []Folder
result := DB.Where("parent_id = ?", folder.ID).Find(&folders)
return folders, result.Error
}
2019-11-30 15:09:56 +08:00
2019-12-02 14:47:55 +08:00
// GetRecursiveChildFolder 查找所有递归子目录,包括自身
func GetRecursiveChildFolder(dirs []string, uid uint, includeSelf bool) ([]Folder, error) {
2019-11-30 15:09:56 +08:00
folders := make([]Folder, 0, len(dirs))
2019-12-02 14:47:55 +08:00
var err error
if conf.DatabaseConfig.Type == "mysql" {
// MySQL 下使用正则查询
search := util.BuildRegexp(dirs, "^", "/", "|")
result := DB.Where("(owner_id = ? and position_absolute REGEXP ?) or (owner_id = ? and position_absolute in (?))", uid, search, uid, dirs).Find(&folders)
err = result.Error
} else {
// SQLite 下使用递归查询
var parFolders []Folder
result := DB.Where("owner_id = ? and position_absolute in (?)", uid, dirs).Find(&parFolders)
if result.Error != nil {
return folders, err
}
// 整理父目录的ID
var parentIDs = make([]uint, 0, len(parFolders))
for _, folder := range parFolders {
parentIDs = append(parentIDs, folder.ID)
}
if includeSelf {
// 合并至最终结果
folders = append(folders, parFolders...)
}
parFolders = []Folder{}
// 递归查询子目录,最大递归65535次
for i := 0; i < 65535; i++ {
result = DB.Where("owner_id = ? and parent_id in (?)", uid, parentIDs).Find(&parFolders)
// 查询结束条件
if len(parFolders) == 0 {
break
}
// 整理父目录的ID
parentIDs = make([]uint, 0, len(parFolders))
for _, folder := range parFolders {
parentIDs = append(parentIDs, folder.ID)
}
// 合并至最终结果
folders = append(folders, parFolders...)
parFolders = []Folder{}
}
}
return folders, err
2019-11-30 15:09:56 +08:00
}
// DeleteFolderByIDs 根据给定ID批量删除目录记录
func DeleteFolderByIDs(ids []uint) error {
result := DB.Where("id in (?)", ids).Unscoped().Delete(&Folder{})
2019-11-30 15:09:56 +08:00
return result.Error
}
2019-12-01 18:31:29 +08:00
//func (folder *Folder)GetPositionAbsolute()string{
// return path.Join(folder.Position,folder.Name)
//}
2019-12-02 14:47:55 +08:00
// MoveFileTo 将此目录下的文件移动至dstFolder
2019-12-01 18:31:29 +08:00
func (folder *Folder) MoveFileTo(files []string, dstFolder *Folder) error {
// 更改顶级要移动文件的父目录指向
2019-12-02 14:47:55 +08:00
err := DB.Model(File{}).Where("name in (?) and user_id = ? and dir = ?", files, folder.OwnerID, folder.PositionAbsolute).
2019-12-01 18:31:29 +08:00
Update(map[string]interface{}{
"folder_id": dstFolder.ID,
"dir": dstFolder.PositionAbsolute,
}).
Error
if err != nil {
return err
}
return nil
}
// MoveFolderTo 将此目录下的目录移动至dstFolder
func (folder *Folder) MoveFolderTo(dirs []string, dstFolder *Folder) error {
2019-12-02 14:47:55 +08:00
// 生成绝对路径
2019-12-01 18:31:29 +08:00
fullDirs := make([]string, len(dirs))
for i := 0; i < len(dirs); i++ {
2019-12-02 14:47:55 +08:00
fullDirs[i] = path.Join(
folder.PositionAbsolute,
path.Base(dirs[i]),
)
}
var subFolders = make([][]Folder, len(fullDirs))
// 更新被移动的目录递归的子目录和文件
for key, parentDir := range fullDirs {
// 检索被移动的目录的所有子目录
toBeMoved, err := GetRecursiveChildFolder([]string{parentDir}, folder.OwnerID, true)
if err != nil {
return err
}
subFolders[key] = toBeMoved
2019-12-01 18:31:29 +08:00
}
// 更改顶级要移动目录的父目录指向
err := DB.Model(Folder{}).Where("position_absolute in (?) and owner_id = ?", fullDirs, folder.OwnerID).
Update(map[string]interface{}{
"parent_id": dstFolder.ID,
"position": dstFolder.PositionAbsolute,
"position_absolute": gorm.Expr(util.BuildConcat("?", "name", conf.DatabaseConfig.Type), util.FillSlash(dstFolder.PositionAbsolute)),
2019-12-02 14:47:55 +08:00
}).Error
2019-12-01 18:31:29 +08:00
if err != nil {
return err
}
2019-12-02 14:47:55 +08:00
// 更新被移动的目录递归的子目录和文件
for _, toBeMoved := range subFolders {
2019-12-01 18:31:29 +08:00
// TODO 找到更好的修改办法
2019-12-02 14:47:55 +08:00
for key, subFolder := range toBeMoved {
// 每个分组的第一个目录已经变更指向,直接跳过
if key > 0 {
newPosition := path.Join(dstFolder.PositionAbsolute, strings.Replace(subFolder.Position, folder.Position, "", 1))
DB.Model(&subFolder).Updates(map[string]interface{}{
"position": newPosition,
"position_absolute": path.Join(newPosition, subFolder.Name),
})
}
2019-12-01 18:31:29 +08:00
}
2019-12-02 14:47:55 +08:00
// 抽离所有子目录的ID
var subFolderIDs = make([]uint, len(toBeMoved))
for key, subFolder := range toBeMoved {
subFolderIDs[key] = subFolder.ID
}
// 获取子目录下的所有子文件
toBeMovedFile, err := GetFilesByParentIDs(subFolderIDs, folder.OwnerID)
2019-12-01 18:31:29 +08:00
if err != nil {
return err
}
2019-12-02 14:47:55 +08:00
for _, subFile := range toBeMovedFile {
newPosition := path.Join(dstFolder.PositionAbsolute, strings.Replace(subFile.Dir, folder.PositionAbsolute, "", 1))
2019-12-01 18:31:29 +08:00
DB.Model(&subFile).Updates(map[string]interface{}{
"dir": newPosition,
})
}
2019-12-02 14:47:55 +08:00
2019-12-01 18:31:29 +08:00
}
return nil
}