From a73a40133d1979caeba54dcb227b91d2d70364f8 Mon Sep 17 00:00:00 2001 From: Xhofe Date: Fri, 8 Apr 2022 22:03:26 +0800 Subject: [PATCH] feat: search api --- bootstrap/setting.go | 7 +++++++ conf/var.go | 1 + drivers/base/cache.go | 4 +++- server/controllers/search.go | 31 +++++++++++++++++++++++++++++++ server/router.go | 2 ++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 server/controllers/search.go diff --git a/bootstrap/setting.go b/bootstrap/setting.go index 288498f2..a45245c5 100644 --- a/bootstrap/setting.go +++ b/bootstrap/setting.go @@ -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] diff --git a/conf/var.go b/conf/var.go index 09429ab4..cf1e9227 100644 --- a/conf/var.go +++ b/conf/var.go @@ -85,6 +85,7 @@ var ( "Visitor WebDAV username", "Visitor WebDAV password", "default page size", "load type", "ocr api", "favicon", + "enable search", } ) diff --git a/drivers/base/cache.go b/drivers/base/cache.go index 38141828..bde23a17 100644 --- a/drivers/base/cache.go +++ b/drivers/base/cache.go @@ -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) - go SaveSearchFiles(key, obj) + if conf.GetBool("enable search") { + go SaveSearchFiles(key, obj) + } return conf.Cache.Set(conf.Ctx, key, obj, nil) } diff --git a/server/controllers/search.go b/server/controllers/search.go new file mode 100644 index 00000000..1e754028 --- /dev/null +++ b/server/controllers/search.go @@ -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) +} diff --git a/server/router.go b/server/router.go index ae8fc262..174f29e2 100644 --- a/server/router.go +++ b/server/router.go @@ -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)