alist/server/common/files.go

52 lines
1.3 KiB
Go
Raw Normal View History

2022-03-31 12:43:17 +00:00
package common
import (
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/drivers/operate"
"github.com/Xhofe/alist/model"
2022-03-31 13:52:19 +00:00
log "github.com/sirupsen/logrus"
2022-03-31 12:43:17 +00:00
)
func Path(rawPath string) (*model.File, []model.File, *model.Account, base.Driver, string, error) {
account, path, driver, err := ParsePath(rawPath)
accountFiles := model.GetAccountFilesByPath(rawPath)
2022-03-31 12:43:17 +00:00
if err != nil {
if err.Error() == "path not found" {
if len(accountFiles) != 0 {
return nil, accountFiles, nil, nil, path, nil
}
}
return nil, nil, nil, nil, "", err
}
2022-03-31 13:52:19 +00:00
log.Debugln("use account: ", account.Name)
2022-03-31 12:43:17 +00:00
file, files, err := operate.Path(driver, account, path)
if err != nil {
if err.Error() == "path not found" {
if len(accountFiles) != 0 {
return nil, accountFiles, nil, nil, path, nil
}
}
2022-03-31 12:43:17 +00:00
return nil, nil, nil, nil, "", err
}
if file != nil {
return file, nil, account, driver, path, nil
} else {
2022-03-31 13:52:19 +00:00
accountFiles := model.GetAccountFilesByPath(rawPath)
2022-04-16 09:28:16 +00:00
for _, accountFile := range accountFiles {
if !containsByName(files, accountFile) {
files = append(files, accountFile)
}
}
2022-03-31 12:43:17 +00:00
return nil, files, account, driver, path, nil
}
}
2022-04-16 09:28:16 +00:00
func containsByName(files []model.File, file model.File) bool {
for _, f := range files {
if f.Name == file.Name {
return true
}
}
return false
}