From 2bff656f001b2f377cbd31c4593a77dd0487dcf8 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Tue, 12 Jul 2022 14:11:37 +0800 Subject: [PATCH] chore: rename `VirtualPath` to `MountPath` --- internal/aria2/add.go | 2 +- internal/aria2/aria2_test.go | 14 +++++++------- internal/aria2/monitor.go | 2 +- internal/bootstrap/data/dev.go | 10 +++++----- internal/fs/copy.go | 6 +++--- internal/fs/put.go | 2 +- internal/model/storage.go | 16 +++++++-------- internal/operations/fs.go | 8 ++++---- internal/operations/path.go | 4 ++-- internal/operations/storage.go | 30 ++++++++++++++--------------- internal/operations/storage_test.go | 24 +++++++++++------------ 11 files changed, 59 insertions(+), 59 deletions(-) diff --git a/internal/aria2/add.go b/internal/aria2/add.go index 5c5490af..354dcd1c 100644 --- a/internal/aria2/add.go +++ b/internal/aria2/add.go @@ -45,7 +45,7 @@ func AddURI(ctx context.Context, uri string, dstDirPath string) error { } DownTaskManager.Submit(task.WithCancelCtx(&task.Task[string]{ ID: gid, - Name: fmt.Sprintf("download %s to [%s](%s)", uri, storage.GetStorage().VirtualPath, dstDirActualPath), + Name: fmt.Sprintf("download %s to [%s](%s)", uri, storage.GetStorage().MountPath, dstDirActualPath), Func: func(tsk *task.Task[string]) error { m := &Monitor{ tsk: tsk, diff --git a/internal/aria2/aria2_test.go b/internal/aria2/aria2_test.go index eead66b1..6bdc0dd7 100644 --- a/internal/aria2/aria2_test.go +++ b/internal/aria2/aria2_test.go @@ -39,13 +39,13 @@ func TestConnect(t *testing.T) { func TestDown(t *testing.T) { TestConnect(t) err := operations.CreateStorage(context.Background(), model.Storage{ - ID: 0, - VirtualPath: "/", - Index: 0, - Driver: "Local", - Status: "", - Addition: `{"root_folder":"../../data"}`, - Remark: "", + ID: 0, + MountPath: "/", + Index: 0, + Driver: "Local", + Status: "", + Addition: `{"root_folder":"../../data"}`, + Remark: "", }) if err != nil { t.Fatalf("failed to create storage: %+v", err) diff --git a/internal/aria2/monitor.go b/internal/aria2/monitor.go index 5a510ce9..7ffac46b 100644 --- a/internal/aria2/monitor.go +++ b/internal/aria2/monitor.go @@ -135,7 +135,7 @@ func (m *Monitor) Complete() error { }() for _, file := range files { TransferTaskManager.Submit(task.WithCancelCtx[uint64](&task.Task[uint64]{ - Name: fmt.Sprintf("transfer %s to [%s](%s)", file.Path, storage.GetStorage().VirtualPath, dstDirActualPath), + Name: fmt.Sprintf("transfer %s to [%s](%s)", file.Path, storage.GetStorage().MountPath, dstDirActualPath), Func: func(tsk *task.Task[uint64]) error { defer wg.Done() size, _ := strconv.ParseInt(file.Length, 10, 64) diff --git a/internal/bootstrap/data/dev.go b/internal/bootstrap/data/dev.go index de98c4ef..98e8340e 100644 --- a/internal/bootstrap/data/dev.go +++ b/internal/bootstrap/data/dev.go @@ -12,11 +12,11 @@ import ( func initDevData() { err := operations.CreateStorage(context.Background(), model.Storage{ - VirtualPath: "/", - Index: 0, - Driver: "Local", - Status: "", - Addition: `{"root_folder":"."}`, + MountPath: "/", + Index: 0, + Driver: "Local", + Status: "", + Addition: `{"root_folder":"."}`, }) if err != nil { log.Fatalf("failed to create storage: %+v", err) diff --git a/internal/fs/copy.go b/internal/fs/copy.go index 51660d9b..1b4463c1 100644 --- a/internal/fs/copy.go +++ b/internal/fs/copy.go @@ -36,7 +36,7 @@ func _copy(ctx context.Context, srcObjPath, dstDirPath string) (bool, error) { } // not in the same storage CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{ - Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcStorage.GetStorage().VirtualPath, srcObjActualPath, dstStorage.GetStorage().VirtualPath, dstDirActualPath), + Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcStorage.GetStorage().MountPath, srcObjActualPath, dstStorage.GetStorage().MountPath, dstDirActualPath), Func: func(task *task.Task[uint64]) error { return copyBetween2Storages(task, srcStorage, dstStorage, srcObjActualPath, dstDirActualPath) }, @@ -63,7 +63,7 @@ func copyBetween2Storages(t *task.Task[uint64], srcStorage, dstStorage driver.Dr srcObjPath := stdpath.Join(srcObjPath, obj.GetName()) dstObjPath := stdpath.Join(dstDirPath, obj.GetName()) CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{ - Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcStorage.GetStorage().VirtualPath, srcObjPath, dstStorage.GetStorage().VirtualPath, dstObjPath), + Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcStorage.GetStorage().MountPath, srcObjPath, dstStorage.GetStorage().MountPath, dstObjPath), Func: func(t *task.Task[uint64]) error { return copyBetween2Storages(t, srcStorage, dstStorage, srcObjPath, dstObjPath) }, @@ -71,7 +71,7 @@ func copyBetween2Storages(t *task.Task[uint64], srcStorage, dstStorage driver.Dr } } else { CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{ - Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcStorage.GetStorage().VirtualPath, srcObjPath, dstStorage.GetStorage().VirtualPath, dstDirPath), + Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcStorage.GetStorage().MountPath, srcObjPath, dstStorage.GetStorage().MountPath, dstDirPath), Func: func(t *task.Task[uint64]) error { return copyFileBetween2Storages(t, srcStorage, dstStorage, srcObjPath, dstDirPath) }, diff --git a/internal/fs/put.go b/internal/fs/put.go index 6155e2f0..6055d62a 100644 --- a/internal/fs/put.go +++ b/internal/fs/put.go @@ -33,7 +33,7 @@ func putAsTask(dstDirPath string, file model.FileStreamer) error { file.SetReadCloser(tempFile) } UploadTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{ - Name: fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), storage.GetStorage().VirtualPath, dstDirActualPath), + Name: fmt.Sprintf("upload %s to [%s](%s)", file.GetName(), storage.GetStorage().MountPath, dstDirActualPath), Func: func(task *task.Task[uint64]) error { return operations.Put(task.Ctx, storage, dstDirActualPath, file, nil) }, diff --git a/internal/model/storage.go b/internal/model/storage.go index 1e4327be..f816b05f 100644 --- a/internal/model/storage.go +++ b/internal/model/storage.go @@ -3,14 +3,14 @@ package model import "time" type Storage struct { - ID uint `json:"id" gorm:"primaryKey"` // unique key - VirtualPath string `json:"virtual_path" gorm:"unique" binding:"required"` // must be standardized - Index int `json:"index"` // use to sort - Driver string `json:"driver"` - Status string `json:"status"` - Addition string `json:"addition" gorm:"type:text"` // Additional information, defined in the corresponding driver - Remark string `json:"remark"` - Modified time.Time `json:"modified"` + ID uint `json:"id" gorm:"primaryKey"` // unique key + MountPath string `json:"mount_path" gorm:"unique" binding:"required"` // must be standardized + Index int `json:"index"` // use to sort + Driver string `json:"driver"` // driver used + Status string `json:"status"` + Addition string `json:"addition" gorm:"type:text"` // Additional information, defined in the corresponding driver + Remark string `json:"remark"` + Modified time.Time `json:"modified"` Sort Proxy } diff --git a/internal/operations/fs.go b/internal/operations/fs.go index bf5bc67c..1fc0a8bd 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -24,7 +24,7 @@ var filesCache = cache.NewMemCache(cache.WithShards[[]model.Obj](64)) var filesG singleflight.Group[[]model.Obj] func ClearCache(storage driver.Driver, path string) { - key := stdpath.Join(storage.GetStorage().VirtualPath, path) + key := stdpath.Join(storage.GetStorage().MountPath, path) filesCache.Del(key) } @@ -42,7 +42,7 @@ func List(ctx context.Context, storage driver.Driver, path string, refresh ...bo if storage.Config().NoCache { return storage.List(ctx, dir) } - key := stdpath.Join(storage.GetStorage().VirtualPath, path) + key := stdpath.Join(storage.GetStorage().MountPath, path) if len(refresh) == 0 || !refresh[0] { if files, ok := filesCache.Get(key); ok { return files, nil @@ -132,7 +132,7 @@ func Link(ctx context.Context, storage driver.Driver, path string, args model.Li if file.IsDir() { return nil, nil, errors.WithStack(errs.NotFile) } - key := stdpath.Join(storage.GetStorage().VirtualPath, path) + key := stdpath.Join(storage.GetStorage().MountPath, path) if link, ok := linkCache.Get(key); ok { return link, file, nil } @@ -253,7 +253,7 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file mod log.Debugf("put file [%s] done", file.GetName()) if err == nil { // clear cache - key := stdpath.Join(storage.GetStorage().VirtualPath, dstDirPath) + key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath) filesCache.Del(key) } return err diff --git a/internal/operations/path.go b/internal/operations/path.go index d2a69a8e..95eed2f9 100644 --- a/internal/operations/path.go +++ b/internal/operations/path.go @@ -31,8 +31,8 @@ func GetStorageAndActualPath(rawPath string) (driver.Driver, string, error) { if storage == nil { return nil, "", errors.Errorf("can't find storage with rawPath: %s", rawPath) } - log.Debugln("use storage: ", storage.GetStorage().VirtualPath) - virtualPath := utils.GetActualVirtualPath(storage.GetStorage().VirtualPath) + log.Debugln("use storage: ", storage.GetStorage().MountPath) + virtualPath := utils.GetActualVirtualPath(storage.GetStorage().MountPath) actualPath := strings.TrimPrefix(rawPath, virtualPath) actualPath = ActualPath(storage.GetAddition(), actualPath) return storage, actualPath, nil diff --git a/internal/operations/storage.go b/internal/operations/storage.go index 80ce127d..9353b1e9 100644 --- a/internal/operations/storage.go +++ b/internal/operations/storage.go @@ -32,7 +32,7 @@ func GetStorageByVirtualPath(virtualPath string) (driver.Driver, error) { // then instantiate corresponding driver and save it in memory func CreateStorage(ctx context.Context, storage model.Storage) error { storage.Modified = time.Now() - storage.VirtualPath = utils.StandardizePath(storage.VirtualPath) + storage.MountPath = utils.StandardizePath(storage.MountPath) var err error // check driver first driverName := storage.Driver @@ -52,7 +52,7 @@ func CreateStorage(ctx context.Context, storage model.Storage) error { return errors.WithMessage(err, "failed init storage but storage is already created") } log.Debugf("storage %+v is created", storageDriver) - storagesMap.Store(storage.VirtualPath, storageDriver) + storagesMap.Store(storage.MountPath, storageDriver) return nil } @@ -68,15 +68,15 @@ func UpdateStorage(ctx context.Context, storage model.Storage) error { return errors.Errorf("driver cannot be changed") } storage.Modified = time.Now() - storage.VirtualPath = utils.StandardizePath(storage.VirtualPath) + storage.MountPath = utils.StandardizePath(storage.MountPath) err = db.UpdateStorage(&storage) if err != nil { return errors.WithMessage(err, "failed update storage in database") } - storageDriver, err := GetStorageByVirtualPath(oldStorage.VirtualPath) - if oldStorage.VirtualPath != storage.VirtualPath { + storageDriver, err := GetStorageByVirtualPath(oldStorage.MountPath) + if oldStorage.MountPath != storage.MountPath { // virtual path renamed, need to drop the storage - storagesMap.Delete(oldStorage.VirtualPath) + storagesMap.Delete(oldStorage.MountPath) } if err != nil { return errors.WithMessage(err, "failed get storage driver") @@ -89,7 +89,7 @@ func UpdateStorage(ctx context.Context, storage model.Storage) error { if err != nil { return errors.WithMessage(err, "failed init storage") } - storagesMap.Store(storage.VirtualPath, storageDriver) + storagesMap.Store(storage.MountPath, storageDriver) return nil } @@ -98,7 +98,7 @@ func DeleteStorageById(ctx context.Context, id uint) error { if err != nil { return errors.WithMessage(err, "failed get storage") } - storageDriver, err := GetStorageByVirtualPath(storage.VirtualPath) + storageDriver, err := GetStorageByVirtualPath(storage.MountPath) if err != nil { return errors.WithMessage(err, "failed get storage driver") } @@ -111,7 +111,7 @@ func DeleteStorageById(ctx context.Context, id uint) error { return errors.WithMessage(err, "failed delete storage in database") } // delete the storage in the memory - storagesMap.Delete(storage.VirtualPath) + storagesMap.Delete(storage.MountPath) return nil } @@ -145,7 +145,7 @@ func getStoragesByPath(path string) []driver.Driver { storages := make([]driver.Driver, 0) curSlashCount := 0 storagesMap.Range(func(key string, value driver.Driver) bool { - virtualPath := utils.GetActualVirtualPath(value.GetStorage().VirtualPath) + virtualPath := utils.GetActualVirtualPath(value.GetStorage().MountPath) if virtualPath == "/" { virtualPath = "" } @@ -167,7 +167,7 @@ func getStoragesByPath(path string) []driver.Driver { }) // make sure the order is the same for same input sort.Slice(storages, func(i, j int) bool { - return storages[i].GetStorage().VirtualPath < storages[j].GetStorage().VirtualPath + return storages[i].GetStorage().MountPath < storages[j].GetStorage().MountPath }) return storages } @@ -180,7 +180,7 @@ func GetStorageVirtualFilesByPath(prefix string) []model.Obj { storages := storagesMap.Values() sort.Slice(storages, func(i, j int) bool { if storages[i].GetStorage().Index == storages[j].GetStorage().Index { - return storages[i].GetStorage().VirtualPath < storages[j].GetStorage().VirtualPath + return storages[i].GetStorage().MountPath < storages[j].GetStorage().MountPath } return storages[i].GetStorage().Index < storages[j].GetStorage().Index }) @@ -192,10 +192,10 @@ func GetStorageVirtualFilesByPath(prefix string) []model.Obj { for _, v := range storages { // TODO should save a balanced storage // balance storage - if utils.IsBalance(v.GetStorage().VirtualPath) { + if utils.IsBalance(v.GetStorage().MountPath) { continue } - virtualPath := v.GetStorage().VirtualPath + virtualPath := v.GetStorage().MountPath if len(virtualPath) <= len(prefix) { continue } @@ -230,7 +230,7 @@ func GetBalancedStorage(path string) driver.Driver { case 1: return storages[0] default: - virtualPath := utils.GetActualVirtualPath(storages[0].GetStorage().VirtualPath) + virtualPath := utils.GetActualVirtualPath(storages[0].GetStorage().MountPath) cur, ok := balanceMap.Load(virtualPath) i := 0 if ok { diff --git a/internal/operations/storage_test.go b/internal/operations/storage_test.go index 33200727..7580d027 100644 --- a/internal/operations/storage_test.go +++ b/internal/operations/storage_test.go @@ -25,9 +25,9 @@ func TestCreateStorage(t *testing.T) { storage model.Storage isErr bool }{ - {storage: model.Storage{Driver: "Local", VirtualPath: "/local", Addition: `{"root_folder":"."}`}, isErr: false}, - {storage: model.Storage{Driver: "Local", VirtualPath: "/local", Addition: `{"root_folder":"."}`}, isErr: true}, - {storage: model.Storage{Driver: "None", VirtualPath: "/none", Addition: `{"root_folder":"."}`}, isErr: true}, + {storage: model.Storage{Driver: "Local", MountPath: "/local", Addition: `{"root_folder":"."}`}, isErr: false}, + {storage: model.Storage{Driver: "Local", MountPath: "/local", Addition: `{"root_folder":"."}`}, isErr: true}, + {storage: model.Storage{Driver: "None", MountPath: "/none", Addition: `{"root_folder":"."}`}, isErr: true}, } for _, storage := range storages { err := operations.CreateStorage(context.Background(), storage.storage) @@ -59,22 +59,22 @@ func TestGetStorageVirtualFilesByPath(t *testing.T) { func TestGetBalancedStorage(t *testing.T) { setupStorages(t) storage := operations.GetBalancedStorage("/a/d/e") - if storage.GetStorage().VirtualPath != "/a/d/e" { - t.Errorf("expected: /a/d/e, got: %+v", storage.GetStorage().VirtualPath) + if storage.GetStorage().MountPath != "/a/d/e" { + t.Errorf("expected: /a/d/e, got: %+v", storage.GetStorage().MountPath) } storage = operations.GetBalancedStorage("/a/d/e") - if storage.GetStorage().VirtualPath != "/a/d/e.balance" { - t.Errorf("expected: /a/d/e.balance, got: %+v", storage.GetStorage().VirtualPath) + if storage.GetStorage().MountPath != "/a/d/e.balance" { + t.Errorf("expected: /a/d/e.balance, got: %+v", storage.GetStorage().MountPath) } } func setupStorages(t *testing.T) { var storages = []model.Storage{ - {Driver: "Local", VirtualPath: "/a/b", Index: 0, Addition: `{"root_folder":"."}`}, - {Driver: "Local", VirtualPath: "/a/c", Index: 1, Addition: `{"root_folder":"."}`}, - {Driver: "Local", VirtualPath: "/a/d", Index: 2, Addition: `{"root_folder":"."}`}, - {Driver: "Local", VirtualPath: "/a/d/e", Index: 3, Addition: `{"root_folder":"."}`}, - {Driver: "Local", VirtualPath: "/a/d/e.balance", Index: 4, Addition: `{"root_folder":"."}`}, + {Driver: "Local", MountPath: "/a/b", Index: 0, Addition: `{"root_folder":"."}`}, + {Driver: "Local", MountPath: "/a/c", Index: 1, Addition: `{"root_folder":"."}`}, + {Driver: "Local", MountPath: "/a/d", Index: 2, Addition: `{"root_folder":"."}`}, + {Driver: "Local", MountPath: "/a/d/e", Index: 3, Addition: `{"root_folder":"."}`}, + {Driver: "Local", MountPath: "/a/d/e.balance", Index: 4, Addition: `{"root_folder":"."}`}, } for _, storage := range storages { err := operations.CreateStorage(context.Background(), storage)