mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: filter and search file
This commit is contained in:
@@ -81,15 +81,26 @@ func GetFilesByIDs(ids []uint, uid uint) ([]File, error) {
|
||||
// GetFilesByKeywords 根据关键字搜索文件,
|
||||
// UID为0表示忽略用户,只根据文件ID检索
|
||||
// TODO 测试
|
||||
func GetFilesByKeywords(keywords string, uid uint) ([]File, error) {
|
||||
var files []File
|
||||
var result *gorm.DB
|
||||
func GetFilesByKeywords(uid uint, keywords ...interface{}) ([]File, error) {
|
||||
var (
|
||||
files []File
|
||||
result = DB
|
||||
conditions string
|
||||
)
|
||||
|
||||
if uid == 0 {
|
||||
result = DB.Where("name like ?", keywords).Find(&files)
|
||||
} else {
|
||||
result = DB.Where("name like ? AND user_id = ?", keywords, uid).Find(&files)
|
||||
// 生成查询条件
|
||||
for i := 0; i < len(keywords); i++ {
|
||||
conditions += "LOWER(name) like ?"
|
||||
if i != len(keywords)-1 {
|
||||
conditions += " or "
|
||||
}
|
||||
}
|
||||
|
||||
if uid != 0 {
|
||||
result = result.Where("user_id = ?", uid)
|
||||
}
|
||||
result = result.Where("("+conditions+")", keywords...).Find(&files)
|
||||
|
||||
return files, result.Error
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ func Init() {
|
||||
|
||||
// Debug模式下,输出所有 SQL 日志
|
||||
if conf.SystemConfig.Debug {
|
||||
db.LogMode(false)
|
||||
db.LogMode(true)
|
||||
}
|
||||
|
||||
//db.SetLogger(util.Log())
|
||||
|
||||
Reference in New Issue
Block a user