feat: update index by req.Paths
This commit is contained in:
parent
5e28d0f96a
commit
e118f4a3b9
@ -143,7 +143,6 @@ func InitialSettings() []model.SettingItem {
|
|||||||
// single settings
|
// single settings
|
||||||
{Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE},
|
{Key: conf.Token, Value: token, Type: conf.TypeString, Group: model.SINGLE, Flag: model.PRIVATE},
|
||||||
{Key: conf.SearchIndex, Value: "none", Type: conf.TypeSelect, Options: "database,bleve,none", Group: model.INDEX},
|
{Key: conf.SearchIndex, Value: "none", Type: conf.TypeSelect, Options: "database,bleve,none", Group: model.INDEX},
|
||||||
{Key: conf.IndexPaths, Value: "/", Type: conf.TypeText, Group: model.INDEX, Flag: model.PRIVATE, Help: `one path per line`},
|
|
||||||
{Key: conf.IgnorePaths, Value: "", Type: conf.TypeText, Group: model.INDEX, Flag: model.PRIVATE, Help: `one path per line`},
|
{Key: conf.IgnorePaths, Value: "", Type: conf.TypeText, Group: model.INDEX, Flag: model.PRIVATE, Help: `one path per line`},
|
||||||
{Key: conf.IndexProgress, Value: "{}", Type: conf.TypeText, Group: model.SINGLE, Flag: model.PRIVATE},
|
{Key: conf.IndexProgress, Value: "{}", Type: conf.TypeText, Group: model.SINGLE, Flag: model.PRIVATE},
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/internal/conf"
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
|
"github.com/alist-org/alist/v3/internal/driver"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -90,12 +91,17 @@ func HandleSettingItemHook(item *model.SettingItem) (hasHook bool, err error) {
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//func HandleSettingItemsHook(items []model.SettingItem) (err error) {
|
// Storage
|
||||||
// for i := 0; i < len(items); i++ {
|
type StorageHook func(typ string, storage driver.Driver)
|
||||||
// _, err = HandleSettingItemHook(&items[i])
|
|
||||||
// if err != nil {
|
var storageHooks = make([]StorageHook, 0)
|
||||||
// return err
|
|
||||||
// }
|
func callStorageHooks(typ string, storage driver.Driver) {
|
||||||
// }
|
for _, hook := range storageHooks {
|
||||||
// return nil
|
hook(typ, storage)
|
||||||
//}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterStorageHook(hook StorageHook) {
|
||||||
|
storageHooks = append(storageHooks, hook)
|
||||||
|
}
|
||||||
|
@ -29,11 +29,11 @@ func HasStorage(mountPath string) bool {
|
|||||||
return storagesMap.Has(utils.FixAndCleanPath(mountPath))
|
return storagesMap.Has(utils.FixAndCleanPath(mountPath))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetStorageByMountPath(virtualPath string) (driver.Driver, error) {
|
func GetStorageByMountPath(mountPath string) (driver.Driver, error) {
|
||||||
virtualPath = utils.FixAndCleanPath(virtualPath)
|
mountPath = utils.FixAndCleanPath(mountPath)
|
||||||
storageDriver, ok := storagesMap.Load(virtualPath)
|
storageDriver, ok := storagesMap.Load(mountPath)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.Errorf("no mount path for an storage is: %s", virtualPath)
|
return nil, errors.Errorf("no mount path for an storage is: %s", mountPath)
|
||||||
}
|
}
|
||||||
return storageDriver, nil
|
return storageDriver, nil
|
||||||
}
|
}
|
||||||
@ -58,6 +58,7 @@ func CreateStorage(ctx context.Context, storage model.Storage) (uint, error) {
|
|||||||
}
|
}
|
||||||
// already has an id
|
// already has an id
|
||||||
err = initStorage(ctx, storage, storageDriver)
|
err = initStorage(ctx, storage, storageDriver)
|
||||||
|
go callStorageHooks("add", storageDriver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return storage.ID, errors.Wrap(err, "failed init storage but storage is already created")
|
return storage.ID, errors.Wrap(err, "failed init storage but storage is already created")
|
||||||
}
|
}
|
||||||
@ -77,6 +78,7 @@ func LoadStorage(ctx context.Context, storage model.Storage) error {
|
|||||||
storageDriver := driverNew()
|
storageDriver := driverNew()
|
||||||
|
|
||||||
err = initStorage(ctx, storage, storageDriver)
|
err = initStorage(ctx, storage, storageDriver)
|
||||||
|
go callStorageHooks("add", storageDriver)
|
||||||
log.Debugf("storage %+v is created", storageDriver)
|
log.Debugf("storage %+v is created", storageDriver)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -145,6 +147,7 @@ func DisableStorage(ctx context.Context, id uint) error {
|
|||||||
return errors.WithMessage(err, "failed update storage in db")
|
return errors.WithMessage(err, "failed update storage in db")
|
||||||
}
|
}
|
||||||
storagesMap.Delete(storage.MountPath)
|
storagesMap.Delete(storage.MountPath)
|
||||||
|
go callStorageHooks("del", storageDriver)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,6 +185,7 @@ func UpdateStorage(ctx context.Context, storage model.Storage) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = initStorage(ctx, storage, storageDriver)
|
err = initStorage(ctx, storage, storageDriver)
|
||||||
|
go callStorageHooks("update", storageDriver)
|
||||||
log.Debugf("storage %+v is update", storageDriver)
|
log.Debugf("storage %+v is update", storageDriver)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -200,13 +204,14 @@ func DeleteStorageById(ctx context.Context, id uint) error {
|
|||||||
if err := storageDriver.Drop(ctx); err != nil {
|
if err := storageDriver.Drop(ctx); err != nil {
|
||||||
return errors.Wrapf(err, "failed drop storage")
|
return errors.Wrapf(err, "failed drop storage")
|
||||||
}
|
}
|
||||||
|
// delete the storage in the memory
|
||||||
|
storagesMap.Delete(storage.MountPath)
|
||||||
|
go callStorageHooks("del", storageDriver)
|
||||||
}
|
}
|
||||||
// delete the storage in the database
|
// delete the storage in the database
|
||||||
if err := db.DeleteStorageById(id); err != nil {
|
if err := db.DeleteStorageById(id); err != nil {
|
||||||
return errors.WithMessage(err, "failed delete storage in database")
|
return errors.WithMessage(err, "failed delete storage in database")
|
||||||
}
|
}
|
||||||
// delete the storage in the memory
|
|
||||||
storagesMap.Delete(storage.MountPath)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,9 +8,11 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
"github.com/alist-org/alist/v3/internal/fs"
|
"github.com/alist-org/alist/v3/internal/fs"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/internal/op"
|
"github.com/alist-org/alist/v3/internal/op"
|
||||||
|
"github.com/alist-org/alist/v3/internal/search/searcher"
|
||||||
"github.com/alist-org/alist/v3/pkg/mq"
|
"github.com/alist-org/alist/v3/pkg/mq"
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
mapset "github.com/deckarep/golang-set/v2"
|
mapset "github.com/deckarep/golang-set/v2"
|
||||||
@ -157,20 +159,15 @@ func Clear(ctx context.Context) error {
|
|||||||
return instance.Clear(ctx)
|
return instance.Clear(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Config(ctx context.Context) searcher.Config {
|
||||||
|
return instance.Config()
|
||||||
|
}
|
||||||
|
|
||||||
func Update(parent string, objs []model.Obj) {
|
func Update(parent string, objs []model.Obj) {
|
||||||
if instance == nil || !instance.Config().AutoUpdate || Running.Load() {
|
if instance == nil || !instance.Config().AutoUpdate || Running.Load() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
indexPaths := GetIndexPaths()
|
if isIgnorePath(parent) {
|
||||||
if !isIndexPath(parent, indexPaths) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ignorePaths, err := GetIgnorePaths()
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("update search index error while get ignore paths: %+v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if isIgnorePath(parent, ignorePaths) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@ -219,7 +216,10 @@ func Update(parent string, objs []model.Obj) {
|
|||||||
}
|
}
|
||||||
// build index if it's a folder
|
// build index if it's a folder
|
||||||
if objs[i].IsDir() {
|
if objs[i].IsDir() {
|
||||||
err = BuildIndex(ctx, []string{path.Join(parent, objs[i].GetName())}, ignorePaths, -1, false)
|
err = BuildIndex(ctx,
|
||||||
|
[]string{path.Join(parent, objs[i].GetName())},
|
||||||
|
conf.SlicesMap[conf.IgnorePaths],
|
||||||
|
-1, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("update search index error while build index: %+v", err)
|
log.Errorf("update search index error while build index: %+v", err)
|
||||||
return
|
return
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/alist-org/alist/v3/drivers/alist_v3"
|
"github.com/alist-org/alist/v3/drivers/alist_v3"
|
||||||
"github.com/alist-org/alist/v3/drivers/base"
|
"github.com/alist-org/alist/v3/drivers/base"
|
||||||
"github.com/alist-org/alist/v3/internal/conf"
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
|
"github.com/alist-org/alist/v3/internal/driver"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/internal/op"
|
"github.com/alist-org/alist/v3/internal/op"
|
||||||
"github.com/alist-org/alist/v3/internal/setting"
|
"github.com/alist-org/alist/v3/internal/setting"
|
||||||
@ -37,25 +38,7 @@ func WriteProgress(progress *model.IndexProgress) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetIndexPaths() []string {
|
func updateIgnorePaths() {
|
||||||
indexPaths := make([]string, 0)
|
|
||||||
customIndexPaths := setting.GetStr(conf.IndexPaths)
|
|
||||||
if customIndexPaths != "" {
|
|
||||||
indexPaths = append(indexPaths, strings.Split(customIndexPaths, "\n")...)
|
|
||||||
}
|
|
||||||
return indexPaths
|
|
||||||
}
|
|
||||||
|
|
||||||
func isIndexPath(path string, indexPaths []string) bool {
|
|
||||||
for _, indexPaths := range indexPaths {
|
|
||||||
if strings.HasPrefix(path, indexPaths) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetIgnorePaths() ([]string, error) {
|
|
||||||
storages := op.GetAllStorages()
|
storages := op.GetAllStorages()
|
||||||
ignorePaths := make([]string, 0)
|
ignorePaths := make([]string, 0)
|
||||||
var skipDrivers = []string{"AList V2", "AList V3", "Virtual"}
|
var skipDrivers = []string{"AList V2", "AList V3", "Virtual"}
|
||||||
@ -84,14 +67,27 @@ func GetIgnorePaths() ([]string, error) {
|
|||||||
if customIgnorePaths != "" {
|
if customIgnorePaths != "" {
|
||||||
ignorePaths = append(ignorePaths, strings.Split(customIgnorePaths, "\n")...)
|
ignorePaths = append(ignorePaths, strings.Split(customIgnorePaths, "\n")...)
|
||||||
}
|
}
|
||||||
return ignorePaths, nil
|
conf.SlicesMap[conf.IgnorePaths] = ignorePaths
|
||||||
}
|
}
|
||||||
|
|
||||||
func isIgnorePath(path string, ignorePaths []string) bool {
|
func isIgnorePath(path string) bool {
|
||||||
for _, ignorePath := range ignorePaths {
|
for _, ignorePath := range conf.SlicesMap[conf.IgnorePaths] {
|
||||||
if strings.HasPrefix(path, ignorePath) {
|
if strings.HasPrefix(path, ignorePath) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
op.RegisterSettingItemHook(conf.IgnorePaths, func(item *model.SettingItem) error {
|
||||||
|
updateIgnorePaths()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
op.RegisterStorageHook(func(typ string, storage driver.Driver) {
|
||||||
|
var skipDrivers = []string{"AList V2", "AList V3", "Virtual"}
|
||||||
|
if utils.SliceContains(skipDrivers, storage.Config().Name) {
|
||||||
|
updateIgnorePaths()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -3,23 +3,22 @@ package handles
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/internal/search"
|
"github.com/alist-org/alist/v3/internal/search"
|
||||||
"github.com/alist-org/alist/v3/server/common"
|
"github.com/alist-org/alist/v3/server/common"
|
||||||
mapset "github.com/deckarep/golang-set/v2"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BuildIndexReq struct {
|
type BuildOrUpdateIndexReq struct {
|
||||||
Paths []string `json:"paths"`
|
Paths []string `json:"paths"`
|
||||||
MaxDepth int `json:"max_depth"`
|
MaxDepth int `json:"max_depth"`
|
||||||
IgnorePaths []string `json:"ignore_paths"`
|
//IgnorePaths []string `json:"ignore_paths"`
|
||||||
Clear bool `json:"clear"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildIndex(c *gin.Context) {
|
func BuildIndex(c *gin.Context) {
|
||||||
var req BuildIndexReq
|
var req BuildOrUpdateIndexReq
|
||||||
if err := c.ShouldBind(&req); err != nil {
|
if err := c.ShouldBind(&req); err != nil {
|
||||||
common.ErrorResp(c, err, 400)
|
common.ErrorResp(c, err, 400)
|
||||||
return
|
return
|
||||||
@ -28,38 +27,15 @@ func BuildIndex(c *gin.Context) {
|
|||||||
common.ErrorStrResp(c, "index is running", 400)
|
common.ErrorStrResp(c, "index is running", 400)
|
||||||
return
|
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() {
|
go func() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
var err error
|
err := search.Clear(ctx)
|
||||||
if req.Clear {
|
if err != nil {
|
||||||
err = search.Clear(ctx)
|
log.Errorf("clear index error: %+v", err)
|
||||||
if err != nil {
|
return
|
||||||
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.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 {
|
if err != nil {
|
||||||
log.Errorf("build index error: %+v", err)
|
log.Errorf("build index error: %+v", err)
|
||||||
}
|
}
|
||||||
@ -67,6 +43,37 @@ func BuildIndex(c *gin.Context) {
|
|||||||
common.SuccessResp(c)
|
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) {
|
func StopIndex(c *gin.Context) {
|
||||||
if !search.Running.Load() {
|
if !search.Running.Load() {
|
||||||
common.ErrorStrResp(c, "index is not running", 400)
|
common.ErrorStrResp(c, "index is not running", 400)
|
||||||
|
@ -114,6 +114,7 @@ func admin(g *gin.RouterGroup) {
|
|||||||
|
|
||||||
index := g.Group("/index")
|
index := g.Group("/index")
|
||||||
index.POST("/build", middlewares.SearchIndex, handles.BuildIndex)
|
index.POST("/build", middlewares.SearchIndex, handles.BuildIndex)
|
||||||
|
index.POST("/update", middlewares.SearchIndex, handles.UpdateIndex)
|
||||||
index.POST("/stop", middlewares.SearchIndex, handles.StopIndex)
|
index.POST("/stop", middlewares.SearchIndex, handles.StopIndex)
|
||||||
index.POST("/clear", middlewares.SearchIndex, handles.ClearIndex)
|
index.POST("/clear", middlewares.SearchIndex, handles.ClearIndex)
|
||||||
index.GET("/progress", middlewares.SearchIndex, handles.GetProgress)
|
index.GET("/progress", middlewares.SearchIndex, handles.GetProgress)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user