Skip to content

Commit

Permalink
chore: move some types to model
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 15, 2022
1 parent 2cddd3c commit 979f838
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 37 deletions.
6 changes: 3 additions & 3 deletions drivers/local/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func (d *Driver) GetAddition() driver.Additional {
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
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
panic("implement me")
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func (d *Driver) Remove(ctx context.Context, path string) error {
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
panic("implement me")
}
Expand Down
7 changes: 4 additions & 3 deletions internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package driver

import (
"context"

"github.com/alist-org/alist/v3/internal/model"
)

Expand All @@ -28,8 +29,8 @@ type Other interface {
}

type Reader interface {
List(ctx context.Context, path string) ([]FileInfo, error)
Link(ctx context.Context, path string, args LinkArgs) (*Link, error)
List(ctx context.Context, path string) ([]model.FileInfo, error)
Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error)
//Get(ctx context.Context, path string) (FileInfo, error) // maybe not need
}

Expand All @@ -39,5 +40,5 @@ type Writer interface {
Rename(ctx context.Context, srcPath, dstName string) error
Copy(ctx context.Context, srcPath, dstPath 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
}
12 changes: 6 additions & 6 deletions internal/fs/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package fs

import (
"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/operations"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
stdpath "path"
"time"
)

// List files
// TODO: hide
// 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)
virtualFiles := operations.GetAccountVirtualFilesByPath(path)
if err != nil {
Expand All @@ -40,7 +40,7 @@ func List(ctx context.Context, path string) ([]driver.FileInfo, error) {
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)
// maybe a virtual file
if path != "/" {
Expand All @@ -67,7 +67,7 @@ func Get(ctx context.Context, path string) (driver.FileInfo, error) {
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)
if err != nil {
return nil, errors.WithMessage(err, "failed get account")
Expand Down
13 changes: 11 additions & 2 deletions internal/fs/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package fs

import (
"context"

"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/pkg/errors"
)
Expand Down Expand Up @@ -39,7 +41,7 @@ func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string)
}

// 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 {
srcAccount, srcActualPath, err := operations.GetAccountAndActualPath(srcPath)
if err != nil {
Expand All @@ -49,9 +51,16 @@ func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) e
if err != nil {
return errors.WithMessage(err, "failed get dst account")
}
// copy if in an account, just call driver.Copy
if srcAccount.GetAccount() == dstAccount.GetAccount() {
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:
// operations.Link(ctx,srcAccount,srcActualPath,args)
// get a Reader from link
Expand All @@ -68,7 +77,7 @@ func Remove(ctx context.Context, account driver.Driver, path string) error {
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)
if err != nil {
return errors.WithMessage(err, "failed get account")
Expand Down
4 changes: 2 additions & 2 deletions internal/driver/file.go → internal/model/ifile.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package driver
package model

import (
"io"
Expand All @@ -12,7 +12,7 @@ type FileInfo interface {
IsDir() bool
}

type FileStream interface {
type FileStreamer interface {
io.ReadCloser
FileInfo
GetMimetype() string
Expand Down
4 changes: 2 additions & 2 deletions internal/driver/link.go → internal/model/link.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package driver
package model

import (
"io"
Expand All @@ -16,6 +16,6 @@ type Link struct {
Header http.Header // needed header
Data io.ReadCloser // return file reader directly
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
}
15 changes: 15 additions & 0 deletions internal/model/stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model

import (
"io"
)

type FileStream struct {
FileInfo
io.ReadCloser
Mimetype string
}

func (f FileStream) GetMimetype() string {
return f.Mimetype
}
4 changes: 2 additions & 2 deletions internal/operations/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ func getAccountsByPath(path string) []driver.Driver {
// 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
// GetAccountVirtualFilesByPath(/a) => b,c,d
func GetAccountVirtualFilesByPath(prefix string) []driver.FileInfo {
files := make([]driver.FileInfo, 0)
func GetAccountVirtualFilesByPath(prefix string) []model.FileInfo {
files := make([]model.FileInfo, 0)
accounts := accountsMap.Values()
sort.Slice(accounts, func(i, j int) bool {
if accounts[i].GetAccount().Index == accounts[j].GetAccount().Index {
Expand Down
24 changes: 12 additions & 12 deletions internal/operations/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ import (

// In order to facilitate adding some other things before and after file operations

var filesCache = cache.NewMemCache(cache.WithShards[[]driver.FileInfo](64))
var filesG singleflight.Group[[]driver.FileInfo]
var filesCache = cache.NewMemCache(cache.WithShards[[]model.FileInfo](64))
var filesG singleflight.Group[[]model.FileInfo]

// 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 {
return account.List(ctx, path)
}
key := stdpath.Join(account.GetAccount().VirtualPath, path)
if files, ok := filesCache.Get(key); ok {
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)
if err != nil {
return nil, errors.WithMessage(err, "failed to list files")
}
// 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, 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, "/") {
return model.FileWithId{
Id: r.GetRootFolderId(),
Expand Down Expand Up @@ -72,22 +72,22 @@ func Get(ctx context.Context, account driver.Driver, path string) (driver.FileIn
return nil, errors.WithStack(driver.ErrorObjectNotFound)
}

var linkCache = cache.NewMemCache(cache.WithShards[*driver.Link](16))
var linkG singleflight.Group[*driver.Link]
var linkCache = cache.NewMemCache(cache.WithShards[*model.Link](16))
var linkG singleflight.Group[*model.Link]

// 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)
if link, ok := linkCache.Get(key); ok {
return link, nil
}
fn := func() (*driver.Link, error) {
fn := func() (*model.Link, error) {
link, err := account.Link(ctx, path, args)
if err != nil {
return nil, errors.WithMessage(err, "failed get link")
}
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
}
Expand Down Expand Up @@ -116,6 +116,6 @@ func Remove(ctx context.Context, account driver.Driver, path string) error {
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)
}
10 changes: 5 additions & 5 deletions internal/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package task
import "context"

type Task struct {
Name string
Func func(context.Context) error
Status string
Error error
Progress int
Name string
Func func(context.Context) error
Status string
Error error
Finish bool
}

0 comments on commit 979f838

Please sign in to comment.