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-04-14 13:34:14 +08:00
ContentHash string ` json:"content_hash" `
2021-03-04 23:50:51 +08:00
}
func ( file * File ) Create ( ) error {
return conf . DB . Create ( file ) . Error
}
2021-03-16 21:25:16 +08:00
func Clear ( drive * conf . Drive ) error {
2021-03-16 23:52:41 +08:00
if err := conf . DB . Where ( "dir = '' AND name = ?" , drive . Name ) . Delete ( & File { } ) . Error ; err != nil {
return err
}
2021-03-16 21:25:16 +08:00
return conf . DB . Where ( "dir like ?" , fmt . Sprintf ( "%s%%" , drive . Name ) ) . Delete ( & File { } ) . Error
2021-03-05 21:07:45 +08:00
}
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
}
2021-04-14 13:34:14 +08:00
func DeleteWithDir ( dir string ) error {
return conf . DB . Where ( "dir like ?" , fmt . Sprintf ( "%s%%" , dir ) ) . Delete ( & File { } ) . Error
}