chore: move some types to model

refactor/fs
Noah Hsu 2022-06-15 18:06:42 +08:00
parent 2cddd3cf2b
commit 979f8383d8
10 changed files with 62 additions and 37 deletions

View File

@ -36,12 +36,12 @@ func (d *Driver) GetAddition() driver.Additional {
return d.Addition return d.Addition
} }
func (d *Driver) List(ctx context.Context, path string) ([]driver.FileInfo, error) { func (d *Driver) List(ctx context.Context, path string) ([]model.FileInfo, error) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (d *Driver) Link(ctx context.Context, path string, args driver.LinkArgs) (*driver.Link, error) { func (d *Driver) Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
@ -76,7 +76,7 @@ func (d *Driver) Remove(ctx context.Context, path string) error {
panic("implement me") panic("implement me")
} }
func (d *Driver) Put(ctx context.Context, parentPath string, stream driver.FileStream) error { func (d *Driver) Put(ctx context.Context, parentPath string, stream model.FileStreamer) error {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }

View File

@ -2,6 +2,7 @@ package driver
import ( import (
"context" "context"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
) )
@ -28,8 +29,8 @@ type Other interface {
} }
type Reader interface { type Reader interface {
List(ctx context.Context, path string) ([]FileInfo, error) List(ctx context.Context, path string) ([]model.FileInfo, error)
Link(ctx context.Context, path string, args LinkArgs) (*Link, error) Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error)
//Get(ctx context.Context, path string) (FileInfo, error) // maybe not need //Get(ctx context.Context, path string) (FileInfo, error) // maybe not need
} }
@ -39,5 +40,5 @@ type Writer interface {
Rename(ctx context.Context, srcPath, dstName string) error Rename(ctx context.Context, srcPath, dstName string) error
Copy(ctx context.Context, srcPath, dstPath string) error Copy(ctx context.Context, srcPath, dstPath string) error
Remove(ctx context.Context, path string) error Remove(ctx context.Context, path string) error
Put(ctx context.Context, parentPath string, stream FileStream) error Put(ctx context.Context, parentPath string, stream model.FileStreamer) error
} }

View File

@ -2,20 +2,20 @@ package fs
import ( import (
"context" "context"
"github.com/alist-org/alist/v3/internal/driver" stdpath "path"
"time"
"github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations" "github.com/alist-org/alist/v3/internal/operations"
"github.com/alist-org/alist/v3/pkg/utils" "github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
stdpath "path"
"time"
) )
// List files // List files
// TODO: hide // TODO: hide
// TODO: sort // TODO: sort
func List(ctx context.Context, path string) ([]driver.FileInfo, error) { func List(ctx context.Context, path string) ([]model.FileInfo, error) {
account, actualPath, err := operations.GetAccountAndActualPath(path) account, actualPath, err := operations.GetAccountAndActualPath(path)
virtualFiles := operations.GetAccountVirtualFilesByPath(path) virtualFiles := operations.GetAccountVirtualFilesByPath(path)
if err != nil { if err != nil {
@ -40,7 +40,7 @@ func List(ctx context.Context, path string) ([]driver.FileInfo, error) {
return files, nil return files, nil
} }
func Get(ctx context.Context, path string) (driver.FileInfo, error) { func Get(ctx context.Context, path string) (model.FileInfo, error) {
path = utils.StandardizationPath(path) path = utils.StandardizationPath(path)
// maybe a virtual file // maybe a virtual file
if path != "/" { if path != "/" {
@ -67,7 +67,7 @@ func Get(ctx context.Context, path string) (driver.FileInfo, error) {
return operations.Get(ctx, account, actualPath) return operations.Get(ctx, account, actualPath)
} }
func Link(ctx context.Context, path string, args driver.LinkArgs) (*driver.Link, error) { func Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) {
account, actualPath, err := operations.GetAccountAndActualPath(path) account, actualPath, err := operations.GetAccountAndActualPath(path)
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "failed get account") return nil, errors.WithMessage(err, "failed get account")

View File

@ -2,7 +2,9 @@ package fs
import ( import (
"context" "context"
"github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/operations" "github.com/alist-org/alist/v3/internal/operations"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -39,7 +41,7 @@ func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string)
} }
// Copy if in an account, call move method // Copy if in an account, call move method
// TODO: if not, add copy task // if not, add copy task
func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) error { func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) error {
srcAccount, srcActualPath, err := operations.GetAccountAndActualPath(srcPath) srcAccount, srcActualPath, err := operations.GetAccountAndActualPath(srcPath)
if err != nil { if err != nil {
@ -49,9 +51,16 @@ func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) e
if err != nil { if err != nil {
return errors.WithMessage(err, "failed get dst account") return errors.WithMessage(err, "failed get dst account")
} }
// copy if in an account, just call driver.Copy
if srcAccount.GetAccount() == dstAccount.GetAccount() { if srcAccount.GetAccount() == dstAccount.GetAccount() {
return operations.Copy(ctx, account, srcActualPath, dstActualPath) return operations.Copy(ctx, account, srcActualPath, dstActualPath)
} }
// not in an account, add copy task
srcFile, err := operations.Get(ctx, srcAccount, srcActualPath)
if srcFile.IsDir() {
// TODO: recursive copy
return nil
}
// TODO: add copy task, maybe like this: // TODO: add copy task, maybe like this:
// operations.Link(ctx,srcAccount,srcActualPath,args) // operations.Link(ctx,srcAccount,srcActualPath,args)
// get a Reader from link // get a Reader from link
@ -68,7 +77,7 @@ func Remove(ctx context.Context, account driver.Driver, path string) error {
return operations.Remove(ctx, account, actualPath) return operations.Remove(ctx, account, actualPath)
} }
func Put(ctx context.Context, account driver.Driver, parentPath string, file driver.FileStream) error { func Put(ctx context.Context, account driver.Driver, parentPath string, file model.FileStreamer) error {
account, actualParentPath, err := operations.GetAccountAndActualPath(parentPath) account, actualParentPath, err := operations.GetAccountAndActualPath(parentPath)
if err != nil { if err != nil {
return errors.WithMessage(err, "failed get account") return errors.WithMessage(err, "failed get account")

View File

@ -1,4 +1,4 @@
package driver package model
import ( import (
"io" "io"
@ -12,7 +12,7 @@ type FileInfo interface {
IsDir() bool IsDir() bool
} }
type FileStream interface { type FileStreamer interface {
io.ReadCloser io.ReadCloser
FileInfo FileInfo
GetMimetype() string GetMimetype() string

View File

@ -1,4 +1,4 @@
package driver package model
import ( import (
"io" "io"
@ -16,6 +16,6 @@ type Link struct {
Header http.Header // needed header Header http.Header // needed header
Data io.ReadCloser // return file reader directly Data io.ReadCloser // return file reader directly
Status int // status maybe 200 or 206, etc Status int // status maybe 200 or 206, etc
FilePath string // local file, return the filepath FilePath *string // local file, return the filepath
Expiration *time.Duration // url expiration time Expiration *time.Duration // url expiration time
} }

15
internal/model/stream.go Normal file
View File

@ -0,0 +1,15 @@
package model
import (
"io"
)
type FileStream struct {
FileInfo
io.ReadCloser
Mimetype string
}
func (f FileStream) GetMimetype() string {
return f.Mimetype
}

View File

@ -136,8 +136,8 @@ func getAccountsByPath(path string) []driver.Driver {
// GetAccountVirtualFilesByPath Obtain the virtual file generated by the account according to the path // GetAccountVirtualFilesByPath Obtain the virtual file generated by the account according to the path
// for example, there are: /a/b,/a/c,/a/d/e,/a/b.balance1,/av // for example, there are: /a/b,/a/c,/a/d/e,/a/b.balance1,/av
// GetAccountVirtualFilesByPath(/a) => b,c,d // GetAccountVirtualFilesByPath(/a) => b,c,d
func GetAccountVirtualFilesByPath(prefix string) []driver.FileInfo { func GetAccountVirtualFilesByPath(prefix string) []model.FileInfo {
files := make([]driver.FileInfo, 0) files := make([]model.FileInfo, 0)
accounts := accountsMap.Values() accounts := accountsMap.Values()
sort.Slice(accounts, func(i, j int) bool { sort.Slice(accounts, func(i, j int) bool {
if accounts[i].GetAccount().Index == accounts[j].GetAccount().Index { if accounts[i].GetAccount().Index == accounts[j].GetAccount().Index {

View File

@ -15,11 +15,11 @@ import (
// In order to facilitate adding some other things before and after file operations // In order to facilitate adding some other things before and after file operations
var filesCache = cache.NewMemCache(cache.WithShards[[]driver.FileInfo](64)) var filesCache = cache.NewMemCache(cache.WithShards[[]model.FileInfo](64))
var filesG singleflight.Group[[]driver.FileInfo] var filesG singleflight.Group[[]model.FileInfo]
// List files in storage, not contains virtual file // List files in storage, not contains virtual file
func List(ctx context.Context, account driver.Driver, path string) ([]driver.FileInfo, error) { func List(ctx context.Context, account driver.Driver, path string) ([]model.FileInfo, error) {
if account.Config().NoCache { if account.Config().NoCache {
return account.List(ctx, path) return account.List(ctx, path)
} }
@ -27,19 +27,19 @@ func List(ctx context.Context, account driver.Driver, path string) ([]driver.Fil
if files, ok := filesCache.Get(key); ok { if files, ok := filesCache.Get(key); ok {
return files, nil return files, nil
} }
files, err, _ := filesG.Do(key, func() ([]driver.FileInfo, error) { files, err, _ := filesG.Do(key, func() ([]model.FileInfo, error) {
files, err := account.List(ctx, path) files, err := account.List(ctx, path)
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "failed to list files") return nil, errors.WithMessage(err, "failed to list files")
} }
// TODO: get duration from global config or account's config // TODO: get duration from global config or account's config
filesCache.Set(key, files, cache.WithEx[[]driver.FileInfo](time.Minute*30)) filesCache.Set(key, files, cache.WithEx[[]model.FileInfo](time.Minute*30))
return files, nil return files, nil
}) })
return files, err return files, err
} }
func Get(ctx context.Context, account driver.Driver, path string) (driver.FileInfo, error) { func Get(ctx context.Context, account driver.Driver, path string) (model.FileInfo, error) {
if r, ok := account.GetAddition().(driver.RootFolderId); ok && utils.PathEqual(path, "/") { if r, ok := account.GetAddition().(driver.RootFolderId); ok && utils.PathEqual(path, "/") {
return model.FileWithId{ return model.FileWithId{
Id: r.GetRootFolderId(), Id: r.GetRootFolderId(),
@ -72,22 +72,22 @@ func Get(ctx context.Context, account driver.Driver, path string) (driver.FileIn
return nil, errors.WithStack(driver.ErrorObjectNotFound) return nil, errors.WithStack(driver.ErrorObjectNotFound)
} }
var linkCache = cache.NewMemCache(cache.WithShards[*driver.Link](16)) var linkCache = cache.NewMemCache(cache.WithShards[*model.Link](16))
var linkG singleflight.Group[*driver.Link] var linkG singleflight.Group[*model.Link]
// Link get link, if is an url. should have an expiry time // Link get link, if is an url. should have an expiry time
func Link(ctx context.Context, account driver.Driver, path string, args driver.LinkArgs) (*driver.Link, error) { func Link(ctx context.Context, account driver.Driver, path string, args model.LinkArgs) (*model.Link, error) {
key := stdpath.Join(account.GetAccount().VirtualPath, path) key := stdpath.Join(account.GetAccount().VirtualPath, path)
if link, ok := linkCache.Get(key); ok { if link, ok := linkCache.Get(key); ok {
return link, nil return link, nil
} }
fn := func() (*driver.Link, error) { fn := func() (*model.Link, error) {
link, err := account.Link(ctx, path, args) link, err := account.Link(ctx, path, args)
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "failed get link") return nil, errors.WithMessage(err, "failed get link")
} }
if link.Expiration != nil { if link.Expiration != nil {
linkCache.Set(key, link, cache.WithEx[*driver.Link](*link.Expiration)) linkCache.Set(key, link, cache.WithEx[*model.Link](*link.Expiration))
} }
return link, nil return link, nil
} }
@ -116,6 +116,6 @@ func Remove(ctx context.Context, account driver.Driver, path string) error {
return account.Remove(ctx, path) return account.Remove(ctx, path)
} }
func Put(ctx context.Context, account driver.Driver, parentPath string, file driver.FileStream) error { func Put(ctx context.Context, account driver.Driver, parentPath string, file model.FileStreamer) error {
return account.Put(ctx, parentPath, file) return account.Put(ctx, parentPath, file)
} }

View File

@ -4,9 +4,9 @@ package task
import "context" import "context"
type Task struct { type Task struct {
Name string Name string
Func func(context.Context) error Func func(context.Context) error
Status string Status string
Error error Error error
Progress int Finish bool
} }