foxxorcat 6024e8d832
refactor: split the db package hook and cache to the op package (#2747)
* refactor:separate the setting method from the db package to the op package and add the cache

* refactor:separate the meta method from the db package to the op package

* fix:setting not load database data

* refactor:separate the user method from the db package to the op package

* refactor:remove user JoinPath error

* fix:op package user cache

* refactor:fs package list method

* fix:tile virtual paths (close #2743)

* Revert "refactor:remove user JoinPath error"

This reverts commit 4e20daaf9e700da047000d4fd4900abbe05c3848.

* clean path directly may lead to unknown behavior

* fix: The path of the meta passed in must be prefix of reqPath

* chore: rename all virtualPath to mountPath

* fix: `getStoragesByPath` and `GetStorageVirtualFilesByPath`

is_sub_path:

/a/b isn't subpath of /a/bc

* fix: don't save setting if hook error

Co-authored-by: Noah Hsu <i@nn.ci>
2022-12-18 19:51:20 +08:00

98 lines
2.6 KiB
Go

package search
import (
"strings"
"github.com/alist-org/alist/v3/drivers/alist_v3"
"github.com/alist-org/alist/v3/drivers/base"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/pkg/utils"
log "github.com/sirupsen/logrus"
)
func Progress() (*model.IndexProgress, error) {
p := setting.GetStr(conf.IndexProgress)
var progress model.IndexProgress
err := utils.Json.UnmarshalFromString(p, &progress)
return &progress, err
}
func WriteProgress(progress *model.IndexProgress) {
p, err := utils.Json.MarshalToString(progress)
if err != nil {
log.Errorf("marshal progress error: %+v", err)
}
err = op.SaveSettingItem(&model.SettingItem{
Key: conf.IndexProgress,
Value: p,
Type: conf.TypeText,
Group: model.SINGLE,
Flag: model.PRIVATE,
})
if err != nil {
log.Errorf("save progress error: %+v", err)
}
}
func GetIndexPaths() []string {
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()
ignorePaths := make([]string, 0)
var skipDrivers = []string{"AList V2", "AList V3", "Virtual"}
v3Visited := make(map[string]bool)
for _, storage := range storages {
if utils.SliceContains(skipDrivers, storage.Config().Name) {
if storage.Config().Name == "AList V3" {
addition := storage.GetAddition().(*alist_v3.Addition)
allowIndexed, visited := v3Visited[addition.Address]
if !visited {
url := addition.Address + "/api/public/settings"
res, err := base.RestyClient.R().Get(url)
if err == nil {
allowIndexed = utils.Json.Get(res.Body(), "data", conf.AllowIndexed).ToBool()
}
}
if allowIndexed {
ignorePaths = append(ignorePaths, storage.GetStorage().MountPath)
}
} else {
ignorePaths = append(ignorePaths, storage.GetStorage().MountPath)
}
}
}
customIgnorePaths := setting.GetStr(conf.IgnorePaths)
if customIgnorePaths != "" {
ignorePaths = append(ignorePaths, strings.Split(customIgnorePaths, "\n")...)
}
return ignorePaths, nil
}
func isIgnorePath(path string, ignorePaths []string) bool {
for _, ignorePath := range ignorePaths {
if strings.HasPrefix(path, ignorePath) {
return true
}
}
return false
}