feat: search api

pull/938/head
Xhofe 2022-04-08 22:03:26 +08:00
parent 6591af58ea
commit a73a40133d
5 changed files with 44 additions and 1 deletions

View File

@ -258,6 +258,13 @@ func InitSettings() {
Access: model.PRIVATE,
Group: model.BACK,
},
{
Key: "enable search",
Value: "false",
Type: "bool",
Access: model.PUBLIC,
Group: model.BACK,
},
}
for i, _ := range settings {
v := settings[i]

View File

@ -85,6 +85,7 @@ var (
"Visitor WebDAV username", "Visitor WebDAV password",
"default page size", "load type",
"ocr api", "favicon",
"enable search",
}
)

View File

@ -37,7 +37,9 @@ func SaveSearchFiles[T model.ISearchFile](key string, obj []T) {
func SetCache[T model.ISearchFile](path string, obj []T, account *model.Account) error {
key := KeyCache(path, account)
if conf.GetBool("enable search") {
go SaveSearchFiles(key, obj)
}
return conf.Cache.Set(conf.Ctx, key, obj, nil)
}

View File

@ -0,0 +1,31 @@
package controllers
import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common"
"github.com/gin-gonic/gin"
)
type SearchReq struct {
Path string `json:"path"`
Keyword string `json:"keyword"`
}
func Search(c *gin.Context) {
if conf.GetBool("enable search") {
common.ErrorStrResp(c, "Not allowed search", 403)
return
}
var req SearchReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
files, err := model.SearchByNameAndPath(req.Path, req.Keyword)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c, files)
}

View File

@ -25,6 +25,8 @@ func InitApiRouter(r *gin.Engine) {
path.POST("/path", controllers.Path)
path.POST("/preview", controllers.Preview)
public.POST("/search", controllers.Search)
//path.POST("/link",middlewares.Auth, controllers.Link)
public.POST("/upload", file.UploadFiles)