Skip to content

Commit a73a401

Browse files
committed
feat: search api
1 parent 6591af5 commit a73a401

5 files changed

Lines changed: 44 additions & 1 deletion

File tree

bootstrap/setting.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ func InitSettings() {
258258
Access: model.PRIVATE,
259259
Group: model.BACK,
260260
},
261+
{
262+
Key: "enable search",
263+
Value: "false",
264+
Type: "bool",
265+
Access: model.PUBLIC,
266+
Group: model.BACK,
267+
},
261268
}
262269
for i, _ := range settings {
263270
v := settings[i]

conf/var.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ var (
8585
"Visitor WebDAV username", "Visitor WebDAV password",
8686
"default page size", "load type",
8787
"ocr api", "favicon",
88+
"enable search",
8889
}
8990
)
9091

drivers/base/cache.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ func SaveSearchFiles[T model.ISearchFile](key string, obj []T) {
3737

3838
func SetCache[T model.ISearchFile](path string, obj []T, account *model.Account) error {
3939
key := KeyCache(path, account)
40-
go SaveSearchFiles(key, obj)
40+
if conf.GetBool("enable search") {
41+
go SaveSearchFiles(key, obj)
42+
}
4143
return conf.Cache.Set(conf.Ctx, key, obj, nil)
4244
}
4345

server/controllers/search.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package controllers
2+
3+
import (
4+
"github.com/Xhofe/alist/conf"
5+
"github.com/Xhofe/alist/model"
6+
"github.com/Xhofe/alist/server/common"
7+
"github.com/gin-gonic/gin"
8+
)
9+
10+
type SearchReq struct {
11+
Path string `json:"path"`
12+
Keyword string `json:"keyword"`
13+
}
14+
15+
func Search(c *gin.Context) {
16+
if conf.GetBool("enable search") {
17+
common.ErrorStrResp(c, "Not allowed search", 403)
18+
return
19+
}
20+
var req SearchReq
21+
if err := c.ShouldBind(&req); err != nil {
22+
common.ErrorResp(c, err, 400)
23+
return
24+
}
25+
files, err := model.SearchByNameAndPath(req.Path, req.Keyword)
26+
if err != nil {
27+
common.ErrorResp(c, err, 500)
28+
return
29+
}
30+
common.SuccessResp(c, files)
31+
}

server/router.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func InitApiRouter(r *gin.Engine) {
2525
path.POST("/path", controllers.Path)
2626
path.POST("/preview", controllers.Preview)
2727

28+
public.POST("/search", controllers.Search)
29+
2830
//path.POST("/link",middlewares.Auth, controllers.Link)
2931
public.POST("/upload", file.UploadFiles)
3032

0 commit comments

Comments
 (0)