2022-11-24 03:46:47 +00:00
|
|
|
package handles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-11-28 05:45:25 +00:00
|
|
|
"github.com/alist-org/alist/v3/internal/search"
|
2022-11-24 03:46:47 +00:00
|
|
|
"github.com/alist-org/alist/v3/server/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-11-28 05:45:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-11-24 03:46:47 +00:00
|
|
|
)
|
|
|
|
|
2022-11-28 05:45:25 +00:00
|
|
|
type BuildIndexReq struct {
|
|
|
|
Paths []string `json:"paths"`
|
|
|
|
MaxDepth int `json:"max_depth"`
|
|
|
|
IgnorePaths []string `json:"ignore_paths"`
|
|
|
|
}
|
|
|
|
|
2022-11-24 03:46:47 +00:00
|
|
|
func BuildIndex(c *gin.Context) {
|
2022-11-28 05:45:25 +00:00
|
|
|
var req BuildIndexReq
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
|
|
common.ErrorResp(c, err, 400)
|
|
|
|
return
|
|
|
|
}
|
2022-12-05 07:46:34 +00:00
|
|
|
if search.Running.Load() {
|
2022-11-28 05:45:25 +00:00
|
|
|
common.ErrorStrResp(c, "index is running", 400)
|
|
|
|
return
|
|
|
|
}
|
2022-11-24 03:46:47 +00:00
|
|
|
go func() {
|
2022-11-28 05:45:25 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
err := search.Clear(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("clear index error: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = search.BuildIndex(context.Background(), req.Paths, req.IgnorePaths, req.MaxDepth, true)
|
2022-11-24 03:46:47 +00:00
|
|
|
if err != nil {
|
2022-11-28 05:45:25 +00:00
|
|
|
log.Errorf("build index error: %+v", err)
|
2022-11-24 03:46:47 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
|
2022-12-05 05:28:39 +00:00
|
|
|
func StopIndex(c *gin.Context) {
|
2022-12-05 07:46:34 +00:00
|
|
|
if !search.Running.Load() {
|
2022-12-05 05:28:39 +00:00
|
|
|
common.ErrorStrResp(c, "index is not running", 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
search.Quit <- struct{}{}
|
|
|
|
common.SuccessResp(c)
|
|
|
|
}
|
|
|
|
|
2022-11-24 03:46:47 +00:00
|
|
|
func GetProgress(c *gin.Context) {
|
2022-12-05 05:28:39 +00:00
|
|
|
progress, err := search.Progress()
|
2022-11-24 03:46:47 +00:00
|
|
|
if err != nil {
|
|
|
|
common.ErrorResp(c, err, 500)
|
|
|
|
return
|
|
|
|
}
|
2022-11-28 05:45:25 +00:00
|
|
|
common.SuccessResp(c, progress)
|
2022-11-24 03:46:47 +00:00
|
|
|
}
|