alist/internal/fs/read.go

77 lines
2.0 KiB
Go
Raw Normal View History

2022-06-10 09:26:43 +00:00
package fs
2022-06-10 09:18:27 +00:00
import (
"context"
2022-06-15 10:06:42 +00:00
stdpath "path"
"time"
2022-06-13 06:53:44 +00:00
"github.com/alist-org/alist/v3/internal/model"
2022-06-10 09:26:43 +00:00
"github.com/alist-org/alist/v3/internal/operations"
2022-06-13 06:53:44 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
2022-06-10 09:18:27 +00:00
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
2022-06-10 09:26:43 +00:00
// List files
// TODO: hide
// TODO: sort
2022-06-15 12:41:17 +00:00
func List(ctx context.Context, path string) ([]model.Obj, error) {
2022-06-10 09:26:43 +00:00
account, actualPath, err := operations.GetAccountAndActualPath(path)
virtualFiles := operations.GetAccountVirtualFilesByPath(path)
2022-06-10 09:18:27 +00:00
if err != nil {
if len(virtualFiles) != 0 {
return virtualFiles, nil
}
return nil, errors.WithMessage(err, "failed get account")
}
2022-06-10 12:20:45 +00:00
files, err := operations.List(ctx, account, actualPath)
2022-06-10 09:18:27 +00:00
if err != nil {
log.Errorf("%+v", err)
if len(virtualFiles) != 0 {
return virtualFiles, nil
}
return nil, errors.WithMessage(err, "failed get files")
}
for _, accountFile := range virtualFiles {
if !containsByName(files, accountFile) {
files = append(files, accountFile)
}
}
return files, nil
}
2022-06-10 13:00:51 +00:00
2022-06-15 12:41:17 +00:00
func Get(ctx context.Context, path string) (model.Obj, error) {
2022-06-13 06:53:44 +00:00
path = utils.StandardizationPath(path)
// maybe a virtual file
if path != "/" {
virtualFiles := operations.GetAccountVirtualFilesByPath(stdpath.Dir(path))
for _, f := range virtualFiles {
if f.GetName() == stdpath.Base(path) {
return f, nil
}
2022-06-11 06:43:03 +00:00
}
}
2022-06-10 13:00:51 +00:00
account, actualPath, err := operations.GetAccountAndActualPath(path)
if err != nil {
2022-06-13 06:53:44 +00:00
// if there are no account prefix with path, maybe root folder
if path == "/" {
2022-06-15 12:41:17 +00:00
return model.Object{
2022-06-13 06:53:44 +00:00
Name: "root",
Size: 0,
Modified: time.Time{},
IsFolder: true,
}, nil
}
2022-06-10 13:00:51 +00:00
return nil, errors.WithMessage(err, "failed get account")
}
return operations.Get(ctx, account, actualPath)
}
2022-06-15 10:06:42 +00:00
func Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) {
2022-06-10 13:00:51 +00:00
account, actualPath, err := operations.GetAccountAndActualPath(path)
if err != nil {
return nil, errors.WithMessage(err, "failed get account")
}
return operations.Link(ctx, account, actualPath, args)
}