feat: add onedrive driver

This commit is contained in:
Noah Hsu
2022-08-30 21:52:06 +08:00
parent c95a7c2a04
commit f551dc76d0
21 changed files with 535 additions and 30 deletions

View File

@ -43,7 +43,7 @@ func TestDown(t *testing.T) {
ID: 0,
MountPath: "/",
Index: 0,
Driver: "Local",
Driver: "local",
Status: "",
Addition: `{"root_folder":"../../data"}`,
Remark: "",

View File

@ -15,7 +15,7 @@ func initDevData() {
err := operations.CreateStorage(context.Background(), model.Storage{
MountPath: "/",
Index: 0,
Driver: "Local",
Driver: "local",
Status: "",
Addition: `{"root_folder":"."}`,
})

View File

@ -15,13 +15,14 @@ type Driver interface {
type Meta interface {
Config() Config
// GetStorage just get raw storage, no need to implement, because model.Storage have implemented
GetStorage() *model.Storage
// GetAddition Additional can't be modified externally, so needn't return pointer
GetAddition() Additional
// Init If already initialized, drop first
// need to unmarshal string to addition first
Init(ctx context.Context, storage model.Storage) error
Drop(ctx context.Context) error
// GetStorage just get raw storage
GetStorage() model.Storage
GetAddition() Additional
}
type Other interface {

7
internal/errs/driver.go Normal file
View File

@ -0,0 +1,7 @@
package errs
import "errors"
var (
EmptyToken = errors.New("empty token")
)

View File

@ -29,12 +29,12 @@ type Proxy struct {
DownProxyUrl string `json:"down_proxy_url"`
}
func (a *Storage) GetStorage() Storage {
return *a
func (s *Storage) GetStorage() *Storage {
return s
}
func (a *Storage) SetStatus(status string) {
a.Status = status
func (s *Storage) SetStatus(status string) {
s.Status = status
}
func (p Proxy) Webdav302() bool {

View File

@ -128,12 +128,13 @@ func getAdditionalItems(t reflect.Type, defaultRoot string) []driver.Item {
continue
}
tag := field.Tag
ignore, ok := tag.Lookup("ignore")
if ok && ignore == "true" {
ignore, ok1 := tag.Lookup("ignore")
name, ok2 := tag.Lookup("json")
if (ok1 && ignore == "true") || !ok2 {
continue
}
item := driver.Item{
Name: tag.Get("json"),
Name: name,
Type: strings.ToLower(field.Type.Name()),
Default: tag.Get("default"),
Options: tag.Get("options"),

View File

@ -2,6 +2,7 @@ package operations
import (
"context"
"fmt"
"sort"
"strings"
"time"
@ -49,7 +50,12 @@ func CreateStorage(ctx context.Context, storage model.Storage) error {
// already has an id
err = storageDriver.Init(ctx, storage)
if err != nil {
storageDriver.GetStorage().SetStatus(fmt.Sprintf("%+v", err.Error()))
MustSaveDriverStorage(storageDriver)
return errors.WithMessage(err, "failed init storage but storage is already created")
} else {
storageDriver.GetStorage().SetStatus("work")
MustSaveDriverStorage(storageDriver)
}
log.Debugf("storage %+v is created", storageDriver)
storagesMap.Store(storage.MountPath, storageDriver)
@ -204,7 +210,7 @@ func saveDriverStorage(driver driver.Driver) error {
return errors.Wrap(err, "error while marshal addition")
}
storage.Addition = string(bytes)
err = db.UpdateStorage(&storage)
err = db.UpdateStorage(storage)
if err != nil {
return errors.WithMessage(err, "failed update storage in database")
}

View File

@ -25,8 +25,8 @@ func TestCreateStorage(t *testing.T) {
storage model.Storage
isErr bool
}{
{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: "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 {
@ -70,11 +70,11 @@ func TestGetBalancedStorage(t *testing.T) {
func setupStorages(t *testing.T) {
var storages = []model.Storage{
{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":"."}`},
{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)