chore: rename some symbols

refactor/fs
Noah Hsu 2022-06-15 20:41:17 +08:00
parent 09ef7c7106
commit 69647f73f0
10 changed files with 95 additions and 91 deletions

View File

@ -44,42 +44,42 @@ func (d *Driver) GetAddition() driver.Additional {
return d.Addition return d.Addition
} }
func (d *Driver) List(ctx context.Context, dir model.Object) ([]model.Object, error) { func (d *Driver) List(ctx context.Context, dir model.Obj) ([]model.Obj, error) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (d *Driver) Link(ctx context.Context, file model.Object, args model.LinkArgs) (*model.Link, error) { func (d *Driver) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (d *Driver) MakeDir(ctx context.Context, parentDir model.Object, dirName string) error { func (d *Driver) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (d *Driver) Move(ctx context.Context, srcObject, dstDir model.Object) error { func (d *Driver) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (d *Driver) Rename(ctx context.Context, srcObject model.Object, newName string) error { func (d *Driver) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (d *Driver) Copy(ctx context.Context, srcObject, dstDir model.Object) error { func (d *Driver) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (d *Driver) Remove(ctx context.Context, object model.Object) error { func (d *Driver) Remove(ctx context.Context, obj model.Obj) error {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }
func (d *Driver) Put(ctx context.Context, parentDir model.Object, stream model.FileStreamer) error { func (d *Driver) Put(ctx context.Context, parentDir model.Obj, stream model.FileStreamer) error {
//TODO implement me //TODO implement me
panic("implement me") panic("implement me")
} }

View File

@ -29,22 +29,26 @@ type Other interface {
} }
type Reader interface { type Reader interface {
List(ctx context.Context, dir model.Object) ([]model.Object, error) // List list files in the path
Link(ctx context.Context, file model.Object, args model.LinkArgs) (*model.Link, error) // if identify files by path, need to set ID with path,like stdpath.Join(dir.GetID(), obj.GetName())
// if identify files by id, need to set ID with corresponding id
List(ctx context.Context, dir model.Obj) ([]model.Obj, error)
// Link get url/filepath/reader of file
Link(ctx context.Context, file model.Obj, 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
} }
type Writer interface { type Writer interface {
// MakeDir make a folder named `dirName` in `parentDir` // MakeDir make a folder named `dirName` in `parentDir`
MakeDir(ctx context.Context, parentDir model.Object, dirName string) error MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error
// Move move `srcObject` to `dstDir` // Move move `srcObject` to `dstDir`
Move(ctx context.Context, srcObject, dstDir model.Object) error Move(ctx context.Context, srcObj, dstDir model.Obj) error
// Rename rename `srcObject` to `newName` // Rename rename `srcObject` to `newName`
Rename(ctx context.Context, srcObject model.Object, newName string) error Rename(ctx context.Context, srcObj model.Obj, newName string) error
// Copy copy `srcObject` to `dstDir` // Copy copy `srcObject` to `dstDir`
Copy(ctx context.Context, srcObject, dstDir model.Object) error Copy(ctx context.Context, srcObj, dstDir model.Obj) error
// Remove remove `object` // Remove remove `object`
Remove(ctx context.Context, object model.Object) error Remove(ctx context.Context, obj model.Obj) error
// Put put `stream` to `parentDir` // Put put `stream` to `parentDir`
Put(ctx context.Context, parentDir model.Object, stream model.FileStreamer) error Put(ctx context.Context, parentDir model.Obj, stream model.FileStreamer) error
} }

View File

@ -15,7 +15,7 @@ import (
// List files // List files
// TODO: hide // TODO: hide
// TODO: sort // TODO: sort
func List(ctx context.Context, path string) ([]model.Object, error) { func List(ctx context.Context, path string) ([]model.Obj, 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) ([]model.Object, error) {
return files, nil return files, nil
} }
func Get(ctx context.Context, path string) (model.Object, error) { func Get(ctx context.Context, path string) (model.Obj, error) {
path = utils.StandardizationPath(path) path = utils.StandardizationPath(path)
// maybe a virtual file // maybe a virtual file
if path != "/" { if path != "/" {
@ -55,7 +55,7 @@ func Get(ctx context.Context, path string) (model.Object, error) {
if err != nil { if err != nil {
// if there are no account prefix with path, maybe root folder // if there are no account prefix with path, maybe root folder
if path == "/" { if path == "/" {
return model.File{ return model.Object{
Name: "root", Name: "root",
Size: 0, Size: 0,
Modified: time.Time{}, Modified: time.Time{},

View File

@ -12,7 +12,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
func containsByName(files []model.Object, file model.Object) bool { func containsByName(files []model.Obj, file model.Obj) bool {
for _, f := range files { for _, f := range files {
if f.GetName() == file.GetName() { if f.GetName() == file.GetName() {
return true return true
@ -23,7 +23,7 @@ func containsByName(files []model.Object, file model.Object) bool {
var httpClient = &http.Client{} var httpClient = &http.Client{}
func getFileStreamFromLink(file model.Object, link *model.Link) (model.FileStreamer, error) { func getFileStreamFromLink(file model.Obj, link *model.Link) (model.FileStreamer, error) {
var rc io.ReadCloser var rc io.ReadCloser
mimetype := mime.TypeByExtension(stdpath.Ext(file.GetName())) mimetype := mime.TypeByExtension(stdpath.Ext(file.GetName()))
if link.Data != nil { if link.Data != nil {
@ -57,7 +57,7 @@ func getFileStreamFromLink(file model.Object, link *model.Link) (model.FileStrea
mimetype = "application/octet-stream" mimetype = "application/octet-stream"
} }
stream := model.FileStream{ stream := model.FileStream{
Object: file, Obj: file,
ReadCloser: rc, ReadCloser: rc,
Mimetype: mimetype, Mimetype: mimetype,
} }

View File

@ -1,31 +0,0 @@
package model
import "time"
type File struct {
ID string
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
}
func (f File) GetID() string {
return f.ID
}

28
internal/model/obj.go Normal file
View File

@ -0,0 +1,28 @@
package model
import (
"io"
"time"
)
type Obj interface {
GetSize() uint64
GetName() string
ModTime() time.Time
IsDir() bool
GetID() string
}
type FileStreamer interface {
io.ReadCloser
Obj
GetMimetype() string
}
type URL interface {
URL() string
}
type Thumbnail interface {
Thumbnail() string
}

View File

@ -1,28 +1,31 @@
package model package model
import ( import "time"
"io"
"time"
)
type Object interface { type Object struct {
GetSize() uint64 ID string
GetName() string Name string
ModTime() time.Time Size uint64
IsDir() bool Modified time.Time
GetID() string IsFolder bool
} }
type FileStreamer interface { func (f Object) GetName() string {
io.ReadCloser return f.Name
Object
GetMimetype() string
} }
type URL interface { func (f Object) GetSize() uint64 {
URL() string return f.Size
} }
type Thumbnail interface { func (f Object) ModTime() time.Time {
Thumbnail() string return f.Modified
}
func (f Object) IsDir() bool {
return f.IsFolder
}
func (f Object) GetID() string {
return f.ID
} }

View File

@ -5,7 +5,7 @@ import (
) )
type FileStream struct { type FileStream struct {
Object Obj
io.ReadCloser io.ReadCloser
Mimetype string Mimetype string
} }

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) []model.Object { func GetAccountVirtualFilesByPath(prefix string) []model.Obj {
files := make([]model.Object, 0) files := make([]model.Obj, 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 {
@ -165,7 +165,7 @@ func GetAccountVirtualFilesByPath(prefix string) []model.Object {
if _, ok := set[name]; ok { if _, ok := set[name]; ok {
continue continue
} }
files = append(files, model.File{ files = append(files, model.Object{
Name: name, Name: name,
Size: 0, Size: 0,
Modified: v.GetAccount().Modified, Modified: v.GetAccount().Modified,

View File

@ -16,11 +16,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[[]model.Object](64)) var filesCache = cache.NewMemCache(cache.WithShards[[]model.Obj](64))
var filesG singleflight.Group[[]model.Object] var filesG singleflight.Group[[]model.Obj]
// 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) ([]model.Object, error) { func List(ctx context.Context, account driver.Driver, path string) ([]model.Obj, error) {
dir, err := Get(ctx, account, path) dir, err := Get(ctx, account, path)
if err != nil { if err != nil {
return nil, errors.WithMessage(err, "failed get dir") return nil, errors.WithMessage(err, "failed get dir")
@ -32,13 +32,13 @@ func List(ctx context.Context, account driver.Driver, path string) ([]model.Obje
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() ([]model.Object, error) { files, err, _ := filesG.Do(key, func() ([]model.Obj, error) {
files, err := account.List(ctx, dir) files, err := account.List(ctx, dir)
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: maybe can get duration from account's config // TODO: maybe can get duration from account's config
filesCache.Set(key, files, cache.WithEx[[]model.Object](time.Minute*time.Duration(conf.Conf.CaCheExpiration))) filesCache.Set(key, files, cache.WithEx[[]model.Obj](time.Minute*time.Duration(conf.Conf.CaCheExpiration)))
return files, nil return files, nil
}) })
return files, err return files, err
@ -46,10 +46,10 @@ func List(ctx context.Context, account driver.Driver, path string) ([]model.Obje
// Get get object from list of files // Get get object from list of files
// TODO: maybe should set object ID with path here // TODO: maybe should set object ID with path here
func Get(ctx context.Context, account driver.Driver, path string) (model.Object, error) { func Get(ctx context.Context, account driver.Driver, path string) (model.Obj, error) {
// is root folder // is root folder
if r, ok := account.GetAddition().(driver.IRootFolderId); ok && utils.PathEqual(path, "/") { if r, ok := account.GetAddition().(driver.IRootFolderId); ok && utils.PathEqual(path, "/") {
return model.File{ return model.Object{
ID: r.GetRootFolderId(), ID: r.GetRootFolderId(),
Name: "root", Name: "root",
Size: 0, Size: 0,
@ -58,7 +58,7 @@ func Get(ctx context.Context, account driver.Driver, path string) (model.Object,
}, nil }, nil
} }
if r, ok := account.GetAddition().(driver.IRootFolderPath); ok && utils.PathEqual(path, r.GetRootFolderPath()) { if r, ok := account.GetAddition().(driver.IRootFolderPath); ok && utils.PathEqual(path, r.GetRootFolderPath()) {
return model.File{ return model.Object{
ID: r.GetRootFolderPath(), ID: r.GetRootFolderPath(),
Name: "root", Name: "root",
Size: 0, Size: 0,
@ -138,34 +138,34 @@ func MakeDir(ctx context.Context, account driver.Driver, path string) error {
} }
func Move(ctx context.Context, account driver.Driver, srcPath, dstPath string) error { func Move(ctx context.Context, account driver.Driver, srcPath, dstPath string) error {
srcObject, err := Get(ctx, account, srcPath) srcObj, err := Get(ctx, account, srcPath)
if err != nil { if err != nil {
return errors.WithMessage(err, "failed to get src object") return errors.WithMessage(err, "failed to get src object")
} }
dstDir, err := Get(ctx, account, stdpath.Dir(dstPath)) dstDir, err := Get(ctx, account, stdpath.Dir(dstPath))
return account.Move(ctx, srcObject, dstDir) return account.Move(ctx, srcObj, dstDir)
} }
func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string) error { func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string) error {
srcObject, err := Get(ctx, account, srcPath) srcObj, err := Get(ctx, account, srcPath)
if err != nil { if err != nil {
return errors.WithMessage(err, "failed to get src object") return errors.WithMessage(err, "failed to get src object")
} }
return account.Rename(ctx, srcObject, dstName) return account.Rename(ctx, srcObj, dstName)
} }
// Copy Just copy file[s] in an account // Copy Just copy file[s] in an account
func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) error { func Copy(ctx context.Context, account driver.Driver, srcPath, dstPath string) error {
srcObject, err := Get(ctx, account, srcPath) srcObj, err := Get(ctx, account, srcPath)
if err != nil { if err != nil {
return errors.WithMessage(err, "failed to get src object") return errors.WithMessage(err, "failed to get src object")
} }
dstDir, err := Get(ctx, account, stdpath.Dir(dstPath)) dstDir, err := Get(ctx, account, stdpath.Dir(dstPath))
return account.Copy(ctx, srcObject, dstDir) return account.Copy(ctx, srcObj, dstDir)
} }
func Remove(ctx context.Context, account driver.Driver, path string) error { func Remove(ctx context.Context, account driver.Driver, path string) error {
object, err := Get(ctx, account, path) obj, err := Get(ctx, account, path)
if err != nil { if err != nil {
// if object not found, it's ok // if object not found, it's ok
if driver.IsErrObjectNotFound(err) { if driver.IsErrObjectNotFound(err) {
@ -173,7 +173,7 @@ func Remove(ctx context.Context, account driver.Driver, path string) error {
} }
return errors.WithMessage(err, "failed to get object") return errors.WithMessage(err, "failed to get object")
} }
return account.Remove(ctx, object) return account.Remove(ctx, obj)
} }
func Put(ctx context.Context, account driver.Driver, parentPath string, file model.FileStreamer) error { func Put(ctx context.Context, account driver.Driver, parentPath string, file model.FileStreamer) error {