feat: update index by req.Paths

This commit is contained in:
Noah Hsu
2022-12-24 20:23:04 +08:00
parent 5e28d0f96a
commit e118f4a3b9
7 changed files with 99 additions and 85 deletions

View File

@ -3,23 +3,22 @@ package handles
import (
"context"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/search"
"github.com/alist-org/alist/v3/server/common"
mapset "github.com/deckarep/golang-set/v2"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
type BuildIndexReq struct {
Paths []string `json:"paths"`
MaxDepth int `json:"max_depth"`
IgnorePaths []string `json:"ignore_paths"`
Clear bool `json:"clear"`
type BuildOrUpdateIndexReq struct {
Paths []string `json:"paths"`
MaxDepth int `json:"max_depth"`
//IgnorePaths []string `json:"ignore_paths"`
}
func BuildIndex(c *gin.Context) {
var req BuildIndexReq
var req BuildOrUpdateIndexReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
@ -28,38 +27,15 @@ func BuildIndex(c *gin.Context) {
common.ErrorStrResp(c, "index is running", 400)
return
}
indexPaths := search.GetIndexPaths()
indexPaths = append(indexPaths, req.Paths...)
indexPathsSet := mapset.NewSet[string]()
for _, indexPath := range indexPaths {
indexPathsSet.Add(indexPath)
}
indexPaths = indexPathsSet.ToSlice()
ignorePaths, err := search.GetIgnorePaths()
if err != nil {
common.ErrorResp(c, err, 500)
return
}
ignorePaths = append(ignorePaths, req.IgnorePaths...)
go func() {
ctx := context.Background()
var err error
if req.Clear {
err = search.Clear(ctx)
if err != nil {
log.Errorf("clear index error: %+v", err)
return
}
} else {
for _, path := range req.Paths {
err = search.Del(ctx, path)
if err != nil {
log.Errorf("delete index on %s error: %+v", path, err)
return
}
}
err := search.Clear(ctx)
if err != nil {
log.Errorf("clear index error: %+v", err)
return
}
err = search.BuildIndex(context.Background(), indexPaths, ignorePaths, req.MaxDepth, true)
err = search.BuildIndex(context.Background(), req.Paths,
conf.SlicesMap[conf.IgnorePaths], req.MaxDepth, true)
if err != nil {
log.Errorf("build index error: %+v", err)
}
@ -67,6 +43,37 @@ func BuildIndex(c *gin.Context) {
common.SuccessResp(c)
}
func UpdateIndex(c *gin.Context) {
var req BuildOrUpdateIndexReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
if search.Running.Load() {
common.ErrorStrResp(c, "index is running", 400)
return
}
if !search.Config(c).AutoUpdate {
common.ErrorStrResp(c, "update is not supported for current index", 400)
}
go func() {
ctx := context.Background()
for _, path := range req.Paths {
err := search.Del(ctx, path)
if err != nil {
log.Errorf("delete index on %s error: %+v", path, err)
return
}
}
err := search.BuildIndex(context.Background(), req.Paths,
conf.SlicesMap[conf.IgnorePaths], req.MaxDepth, false)
if err != nil {
log.Errorf("update index error: %+v", err)
}
}()
common.SuccessResp(c)
}
func StopIndex(c *gin.Context) {
if !search.Running.Load() {
common.ErrorStrResp(c, "index is not running", 400)