Files
OpenList/server/models/file.go

62 lines
1.7 KiB
Go
Raw Normal View History

2021-03-04 23:50:51 +08:00
package models
import (
"fmt"
"github.com/Xhofe/alist/conf"
"time"
)
type File struct {
2021-03-08 20:26:02 +08:00
Dir string `json:"dir" gorm:"index"`
2021-03-04 23:50:51 +08:00
FileExtension string `json:"file_extension"`
FileId string `json:"file_id"`
Name string `json:"name" gorm:"index"`
Type string `json:"type"`
UpdatedAt *time.Time `json:"updated_at"`
Category string `json:"category"`
ContentType string `json:"content_type"`
Size int64 `json:"size"`
2021-03-05 21:07:45 +08:00
Password string `json:"password"`
2021-03-12 18:12:56 +08:00
Url string `json:"url" gorm:"-"`
2021-03-04 23:50:51 +08:00
}
func (file *File) Create() error {
return conf.DB.Create(file).Error
}
2021-03-05 21:07:45 +08:00
func Clear() error {
return conf.DB.Where("1 = 1").Delete(&File{}).Error
}
2021-03-08 20:26:02 +08:00
func GetFileByDirAndName(dir, name string) (*File, error) {
2021-03-05 21:07:45 +08:00
var file File
2021-03-08 20:26:02 +08:00
if err := conf.DB.Where("dir = ? AND name = ?", dir, name).First(&file).Error; err != nil {
2021-03-05 21:07:45 +08:00
return nil, err
}
return &file, nil
}
2021-03-08 20:26:02 +08:00
func GetFilesByDir(dir string) (*[]File, error) {
2021-03-04 23:50:51 +08:00
var files []File
2021-03-08 20:26:02 +08:00
if err := conf.DB.Where("dir = ?", dir).Find(&files).Error; err != nil {
2021-03-05 21:07:45 +08:00
return nil, err
2021-03-04 23:50:51 +08:00
}
return &files, nil
}
func SearchByNameGlobal(keyword string) (*[]File, error) {
var files []File
2021-03-08 20:26:02 +08:00
if err := conf.DB.Where("name LIKE ? AND password = ''", fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil {
2021-03-05 21:07:45 +08:00
return nil, err
2021-03-04 23:50:51 +08:00
}
return &files, nil
}
2021-03-08 20:26:02 +08:00
func SearchByNameInDir(keyword string, dir string) (*[]File, error) {
2021-03-04 23:50:51 +08:00
var files []File
2021-03-08 20:26:02 +08:00
if err := conf.DB.Where("dir LIKE ? AND name LIKE ? AND password = ''", fmt.Sprintf("%s%%", dir), fmt.Sprintf("%%%s%%", keyword)).Find(&files).Error; err != nil {
2021-03-05 21:07:45 +08:00
return nil, err
2021-03-04 23:50:51 +08:00
}
return &files, nil
2021-03-05 21:07:45 +08:00
}