diff --git a/drivers/local/driver.go b/drivers/local/driver.go index dcec6291..73d25f01 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -2,10 +2,12 @@ package local import ( "context" - "errors" + "fmt" "github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/pkg/utils" + "os" + "path/filepath" ) type Driver struct { @@ -18,32 +20,42 @@ func (d Driver) Config() driver.Config { } func (d *Driver) Init(ctx context.Context, account model.Account) error { + d.Account = account addition := d.Account.Addition err := utils.Json.UnmarshalFromString(addition, d.Addition) if err != nil { - return errors.New("error") + return fmt.Errorf("error while unmarshal addition: %w", err) } return nil } func (d *Driver) Update(ctx context.Context, account model.Account) error { - //TODO implement me - panic("implement me") + return d.Init(ctx, account) } func (d *Driver) Drop(ctx context.Context) error { - //TODO implement me - panic("implement me") + return nil } func (d *Driver) GetAccount() model.Account { - //TODO implement me - panic("implement me") + return d.Account } -func (d *Driver) File(ctx context.Context, path string) (*driver.FileInfo, error) { - //TODO implement me - panic("implement me") +func (d *Driver) File(ctx context.Context, path string) (driver.FileInfo, error) { + fullPath := filepath.Join(d.RootFolder, path) + if !utils.Exists(fullPath) { + return nil, driver.ErrorObjectNotFound + } + f, err := os.Stat(fullPath) + if err != nil { + return nil, err + } + return model.File{ + Name: f.Name(), + Size: uint64(f.Size()), + Modified: f.ModTime(), + IsFolder: f.IsDir(), + }, nil } func (d *Driver) List(ctx context.Context, path string) ([]driver.FileInfo, error) { @@ -56,6 +68,11 @@ func (d *Driver) Link(ctx context.Context, args driver.LinkArgs) (*driver.Link, panic("implement me") } +func (d Driver) Other(ctx context.Context, data interface{}) (interface{}, error) { + //TODO implement me + panic("implement me") +} + func (d *Driver) MakeDir(ctx context.Context, path string) error { //TODO implement me panic("implement me") @@ -87,7 +104,3 @@ func (d *Driver) Put(ctx context.Context, stream driver.FileStream, parentPath s } var _ driver.Driver = (*Driver)(nil) - -func init() { - driver.RegisterDriver(config.Name, New) -} diff --git a/drivers/local/meta.go b/drivers/local/meta.go index d7678f5c..59533496 100644 --- a/drivers/local/meta.go +++ b/drivers/local/meta.go @@ -15,3 +15,7 @@ var config = driver.Config{ func New() driver.Driver { return &Driver{} } + +func init() { + driver.RegisterDriver(config, New) +} diff --git a/internal/driver/config.go b/internal/driver/config.go index 5d5208b6..98e4b79b 100644 --- a/internal/driver/config.go +++ b/internal/driver/config.go @@ -1,8 +1,9 @@ package driver type Config struct { - Name string - LocalSort bool - OnlyLocal bool - OnlyProxy bool + Name string + LocalSort bool + OnlyLocal bool + OnlyProxy bool + NoNeedSetLink bool } diff --git a/internal/driver/driver.go b/internal/driver/driver.go index 537b7fc5..08e738a0 100644 --- a/internal/driver/driver.go +++ b/internal/driver/driver.go @@ -6,12 +6,13 @@ import ( ) type Driver interface { - Other + Meta Reader Writer + Other } -type Other interface { +type Meta interface { Config() Config Init(ctx context.Context, account model.Account) error Update(ctx context.Context, account model.Account) error @@ -20,8 +21,12 @@ type Other interface { GetAccount() model.Account } +type Other interface { + Other(ctx context.Context, data interface{}) (interface{}, error) +} + type Reader interface { - File(ctx context.Context, path string) (*FileInfo, error) + File(ctx context.Context, path string) (FileInfo, error) List(ctx context.Context, path string) ([]FileInfo, error) Link(ctx context.Context, args LinkArgs) (*Link, error) } diff --git a/internal/driver/error.go b/internal/driver/error.go index a88f2ae3..15ffa8cb 100644 --- a/internal/driver/error.go +++ b/internal/driver/error.go @@ -5,4 +5,7 @@ import "errors" var ( ErrorDirNotFound = errors.New("directory not found") ErrorObjectNotFound = errors.New("object not found") + ErrNotImplement = errors.New("not implement") + ErrNotSupport = errors.New("not support") + ErrRelativePath = errors.New("access using relative path is not allowed") ) diff --git a/internal/driver/file.go b/internal/driver/file.go index 910beee8..92647ef0 100644 --- a/internal/driver/file.go +++ b/internal/driver/file.go @@ -6,9 +6,10 @@ import ( ) type FileInfo interface { + GetSize() uint64 GetName() string - GetModTime() time.Time - GetSize() int64 + ModTime() time.Time + IsDir() bool } type FileStream interface { @@ -16,3 +17,11 @@ type FileStream interface { FileInfo GetMimetype() string } + +type URL interface { + URL() string +} + +type Thumbnail interface { + Thumbnail() string +} diff --git a/internal/driver/manage.go b/internal/driver/manage.go index e243a5b9..20e3500f 100644 --- a/internal/driver/manage.go +++ b/internal/driver/manage.go @@ -8,7 +8,7 @@ type New func() Driver var driversMap = map[string]New{} -func RegisterDriver(name string, new New) { - log.Infof("register driver: [%s]", name) - driversMap[name] = new +func RegisterDriver(config Config, driver New) { + log.Infof("register driver: [%s]", config.Name) + driversMap[config.Name] = driver } diff --git a/internal/model/file.go b/internal/model/file.go new file mode 100644 index 00000000..f5b4d258 --- /dev/null +++ b/internal/model/file.go @@ -0,0 +1,26 @@ +package model + +import "time" + +type File struct { + Name string + Size uint64 + Modified time.Time + IsFolder bool +} + +func (f File) GetName() string { + return f.Name +} + +func (f File) GetSize() uint64 { + return f.Size +} + +func (f File) ModTime() time.Time { + return f.Modified +} + +func (f File) IsDir() bool { + return f.IsFolder +} diff --git a/internal/driver/operations/operate.go b/internal/operations/operate.go similarity index 100% rename from internal/driver/operations/operate.go rename to internal/operations/operate.go