-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
210 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package operations | ||
|
||
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/store" | ||
"github.com/alist-org/alist/v3/pkg/utils" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// Although the driver type is stored, | ||
// there is an account in each driver, | ||
// so it should actually be an account, just wrapped by the driver | ||
var accountsMap = map[string]driver.Driver{} | ||
|
||
func GetAccountByVirtualPath(virtualPath string) (driver.Driver, error) { | ||
accountDriver, ok := accountsMap[virtualPath] | ||
if !ok { | ||
return nil, errors.Errorf("no virtual path for an account is: %s", virtualPath) | ||
} | ||
return accountDriver, nil | ||
} | ||
|
||
// CreateAccount Save the account to database so account can get an id | ||
// then instantiate corresponding driver and save it in memory | ||
func CreateAccount(ctx context.Context, account model.Account) error { | ||
err := store.CreateAccount(&account) | ||
if err != nil { | ||
return errors.WithMessage(err, "failed create account in database") | ||
} | ||
// already has an id | ||
driverName := account.Driver | ||
driverNew, err := GetDriverNew(driverName) | ||
if err != nil { | ||
return errors.WithMessage(err, "failed get driver new") | ||
} | ||
accountDriver := driverNew() | ||
err = accountDriver.Init(ctx, account) | ||
if err != nil { | ||
return errors.WithMessage(err, "failed init account") | ||
} | ||
accountsMap[account.VirtualPath] = accountDriver | ||
return nil | ||
} | ||
|
||
// UpdateAccount update account | ||
// get old account first | ||
// drop the account then reinitialize | ||
func UpdateAccount(ctx context.Context, account model.Account) error { | ||
oldAccount, err := store.GetAccountById(account.ID) | ||
if err != nil { | ||
return errors.WithMessage(err, "failed get old account") | ||
} | ||
err = store.UpdateAccount(&account) | ||
if err != nil { | ||
return errors.WithMessage(err, "failed update account in database") | ||
} | ||
if oldAccount.VirtualPath != account.VirtualPath { | ||
// virtual path renamed | ||
delete(accountsMap, oldAccount.VirtualPath) | ||
} | ||
accountDriver, err := GetAccountByVirtualPath(oldAccount.VirtualPath) | ||
if err != nil { | ||
return errors.WithMessage(err, "failed get account driver") | ||
} | ||
err = accountDriver.Drop(ctx) | ||
if err != nil { | ||
return errors.WithMessage(err, "failed drop account") | ||
} | ||
err = accountDriver.Init(ctx, account) | ||
if err != nil { | ||
return errors.WithMessage(err, "failed init account") | ||
} | ||
accountsMap[account.VirtualPath] = accountDriver | ||
return nil | ||
} | ||
|
||
// SaveDriverAccount call from specific driver | ||
func SaveDriverAccount(driver driver.Driver) error { | ||
account := driver.GetAccount() | ||
addition := driver.GetAddition() | ||
bytes, err := utils.Json.Marshal(addition) | ||
if err != nil { | ||
return errors.Wrap(err, "error while marshal addition") | ||
} | ||
account.Addition = string(bytes) | ||
err = store.UpdateAccount(&account) | ||
if err != nil { | ||
return errors.WithMessage(err, "failed update account in database") | ||
} | ||
return nil | ||
} |
60 changes: 35 additions & 25 deletions
60
internal/driver/manage.go → internal/operations/driver.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package operations | ||
|
||
var () |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,40 @@ | ||
package store | ||
|
||
import "github.com/alist-org/alist/v3/internal/model" | ||
|
||
type Account interface { | ||
Create(account model.Account) error | ||
Update(account model.Account) error | ||
Delete(id uint) error | ||
GetByID(id uint) (*model.Account, error) | ||
List() ([]model.Account, error) | ||
import ( | ||
"github.com/alist-org/alist/v3/internal/model" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// CreateAccount just insert account to database | ||
func CreateAccount(account *model.Account) error { | ||
return errors.WithStack(db.Create(account).Error) | ||
} | ||
|
||
// UpdateAccount just update account in database | ||
func UpdateAccount(account *model.Account) error { | ||
return errors.WithStack(db.Save(account).Error) | ||
} | ||
|
||
// DeleteAccountById just delete account from database by id | ||
func DeleteAccountById(id uint) error { | ||
return errors.WithStack(db.Delete(&model.Account{}, id).Error) | ||
} | ||
|
||
// GetAccounts Get all accounts from database | ||
func GetAccounts() ([]model.Account, error) { | ||
var accounts []model.Account | ||
if err := db.Order(columnName("index")).Find(&accounts).Error; err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return accounts, nil | ||
} | ||
|
||
// GetAccountById Get Account by id, used to update account usually | ||
func GetAccountById(id uint) (*model.Account, error) { | ||
var account model.Account | ||
account.ID = id | ||
if err := db.First(&account).Error; err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
return &account, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package store | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
) | ||
|
||
var db gorm.DB |
Oops, something went wrong.