🚧 构建目录树
This commit is contained in:
58
server/models/create.go
Normal file
58
server/models/create.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/alidrive"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// build tree
|
||||
func BuildTree() error {
|
||||
log.Infof("开始构建目录树...")
|
||||
tx := conf.DB.Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
if err := tx.Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := BuildOne(conf.Conf.AliDrive.RootFolder, "/root/", tx); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
return tx.Commit().Error
|
||||
}
|
||||
|
||||
func BuildOne(parent string, path string, tx *gorm.DB) error {
|
||||
files, err := alidrive.GetList(parent, conf.Conf.AliDrive.MaxFilesCount, "", "", "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, file := range files.Items {
|
||||
newFile := File{
|
||||
ParentPath: path,
|
||||
FileExtension: file.FileExtension,
|
||||
FileId: file.FileId,
|
||||
Name: file.Name,
|
||||
Type: file.Type,
|
||||
UpdatedAt: file.UpdatedAt,
|
||||
Category: file.Category,
|
||||
ContentType: file.ContentType,
|
||||
Size: file.Size,
|
||||
}
|
||||
log.Debugf("插入file:%+v", newFile)
|
||||
if err := tx.Create(&newFile).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if file.Type == "folder" {
|
||||
if err := BuildOne(file.FileId, fmt.Sprintf("%s%s/", path, file.Name), tx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -16,16 +16,29 @@ type File struct {
|
||||
Category string `json:"category"`
|
||||
ContentType string `json:"content_type"`
|
||||
Size int64 `json:"size"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func (file *File) Create() error {
|
||||
return conf.DB.Create(file).Error
|
||||
}
|
||||
|
||||
func GetFilesByParentPath(parentPath string) (*[]File,error) {
|
||||
func Clear() error {
|
||||
return conf.DB.Where("1 = 1").Delete(&File{}).Error
|
||||
}
|
||||
|
||||
func GetFileByParentPathAndName(parentPath, name string) (*File, error) {
|
||||
var file File
|
||||
if err := conf.DB.Where("parent_path = ? AND name = ?", parentPath, name).First(&file).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &file, nil
|
||||
}
|
||||
|
||||
func GetFilesByParentPath(parentPath string) (*[]File, error) {
|
||||
var files []File
|
||||
if err := conf.DB.Where("parent_path = ?", parentPath).Find(&files).Error; err != nil {
|
||||
return nil,err
|
||||
return nil, err
|
||||
}
|
||||
return &files, nil
|
||||
}
|
||||
@@ -33,15 +46,15 @@ func GetFilesByParentPath(parentPath string) (*[]File,error) {
|
||||
func SearchByNameGlobal(keyword string) (*[]File, error) {
|
||||
var files []File
|
||||
if err := conf.DB.Where("name LIKE ?", fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil {
|
||||
return nil,err
|
||||
return nil, err
|
||||
}
|
||||
return &files, nil
|
||||
}
|
||||
|
||||
func SearchByNameInPath(keyword string, parentPath string) (*[]File, error) {
|
||||
var files []File
|
||||
if err := conf.DB.Where("parent_path LIKE ? AND name LIKE ?",fmt.Sprintf("%s%%", parentPath), fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil {
|
||||
return nil,err
|
||||
if err := conf.DB.Where("parent_path LIKE ? AND name LIKE ?", fmt.Sprintf("%s%%", parentPath), fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &files, nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user