Feat: filter and search file
This commit is contained in:
parent
8f28a9c346
commit
127d0236f9
5 changed files with 34 additions and 13 deletions
|
@ -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())
|
||||
|
|
|
@ -127,6 +127,7 @@ func (fs *FileSystem) Preview(ctx context.Context, id uint, isText bool) (*respo
|
|||
return &response.ContentResponse{
|
||||
Redirect: true,
|
||||
URL: previewURL,
|
||||
MaxAge: ttl,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
@ -342,8 +343,8 @@ func (fs *FileSystem) resetPolicyToFirstFile(ctx context.Context) error {
|
|||
}
|
||||
|
||||
// Search 搜索文件
|
||||
func (fs *FileSystem) Search(ctx context.Context, keywords string) ([]Object, error) {
|
||||
files, _ := model.GetFilesByKeywords(keywords, fs.User.ID)
|
||||
func (fs *FileSystem) Search(ctx context.Context, keywords ...interface{}) ([]Object, error) {
|
||||
files, _ := model.GetFilesByKeywords(fs.User.ID, keywords...)
|
||||
fs.SetTargetFile(&files)
|
||||
|
||||
return fs.listObjects(ctx, "/", files, nil, nil), nil
|
||||
|
|
|
@ -272,6 +272,7 @@ func (service *FileIDService) PreviewContent(ctx context.Context, c *gin.Context
|
|||
|
||||
// 重定向到文件源
|
||||
if resp.Redirect {
|
||||
c.Header("Cache-Control", fmt.Sprintf("max-age=%d", resp.MaxAge))
|
||||
return serializer.Response{
|
||||
Code: -301,
|
||||
Data: resp.URL,
|
||||
|
|
|
@ -24,20 +24,28 @@ func (service *ItemSearchService) Search(c *gin.Context) serializer.Response {
|
|||
|
||||
switch service.Type {
|
||||
case "keywords":
|
||||
return service.SearchKeywords(c, "%"+service.Keywords+"%", fs)
|
||||
return service.SearchKeywords(c, fs, "%"+service.Keywords+"%")
|
||||
case "image":
|
||||
return service.SearchKeywords(c, fs, "%.bmp", "%.flac", "%.iff", "%.png", "%.gif", "%.jpg", "%.jpge", "%.psd", "%.svg", "%.webp")
|
||||
case "video":
|
||||
return service.SearchKeywords(c, fs, "%.mp4", "%.flv", "%.avi", "%.wmv", "%.mkv", "%.rm", "%.rmvb", "%.mov", "%.ogv")
|
||||
case "audio":
|
||||
return service.SearchKeywords(c, fs, "%.mp3", "%.flac", "%.ape", "%.wav", "%.acc", "%.ogg", "%.midi", "%.mid")
|
||||
case "doc":
|
||||
return service.SearchKeywords(c, fs, "%.txt", "%.md", "%.pdf", "%.doc", "%.docx", "%.ppt", "%.pptx", "%.xls", "%.xlsx", "%.pub")
|
||||
default:
|
||||
return serializer.ParamErr("未知搜索类型", nil)
|
||||
}
|
||||
}
|
||||
|
||||
// SearchKeywords 根据关键字搜索文件
|
||||
func (service *ItemSearchService) SearchKeywords(c *gin.Context, keywords string, fs *filesystem.FileSystem) serializer.Response {
|
||||
func (service *ItemSearchService) SearchKeywords(c *gin.Context, fs *filesystem.FileSystem, keywords ...interface{}) serializer.Response {
|
||||
// 上下文
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// 获取子项目
|
||||
objects, err := fs.Search(ctx, keywords)
|
||||
objects, err := fs.Search(ctx, keywords...)
|
||||
if err != nil {
|
||||
return serializer.Err(serializer.CodeNotSet, err.Error(), err)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue