mirror of
https://github.com/AlistGo/alist.git
synced 2025-04-24 06:14:04 +08:00
feat: add disable option for storage (close #1476)
This commit is contained in:
parent
af884010d1
commit
74f1154e5e
@ -12,6 +12,7 @@ type Storage struct {
|
||||
Addition string `json:"addition" gorm:"type:text"` // Additional information, defined in the corresponding driver
|
||||
Remark string `json:"remark"`
|
||||
Modified time.Time `json:"modified"`
|
||||
Disabled bool `json:"disabled"` // if disabled
|
||||
Sort
|
||||
Proxy
|
||||
}
|
||||
|
@ -56,6 +56,76 @@ func CreateStorage(ctx context.Context, storage model.Storage) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadStorage load exist storage in db to memory
|
||||
func LoadStorage(ctx context.Context, storage model.Storage) error {
|
||||
storage.MountPath = utils.StandardizePath(storage.MountPath)
|
||||
// check driver first
|
||||
driverName := storage.Driver
|
||||
driverNew, err := GetDriverNew(driverName)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get driver new")
|
||||
}
|
||||
storageDriver := driverNew()
|
||||
err = storageDriver.Init(ctx, storage)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed init storage but storage is already created")
|
||||
}
|
||||
log.Debugf("storage %+v is created", storageDriver)
|
||||
storagesMap.Store(storage.MountPath, storageDriver)
|
||||
return nil
|
||||
}
|
||||
|
||||
func EnableStorage(ctx context.Context, id uint) error {
|
||||
storage, err := db.GetStorageById(id)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get storage")
|
||||
}
|
||||
if !storage.Disabled {
|
||||
return errors.Errorf("this storage have enabled")
|
||||
}
|
||||
err = LoadStorage(ctx, *storage)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed load storage")
|
||||
}
|
||||
// reget storage from db, because it maybe hava updated
|
||||
storage, err = db.GetStorageById(id)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed reget storage again")
|
||||
}
|
||||
storage.Disabled = false
|
||||
err = db.UpdateStorage(storage)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed update storage in db, but have load in memory. you can try restart")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DisableStorage(ctx context.Context, id uint) error {
|
||||
storage, err := db.GetStorageById(id)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get storage")
|
||||
}
|
||||
if storage.Disabled {
|
||||
return errors.Errorf("this storage have disabled")
|
||||
}
|
||||
storageDriver, err := GetStorageByVirtualPath(storage.MountPath)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed get storage driver")
|
||||
}
|
||||
// drop the storage in the driver
|
||||
if err := storageDriver.Drop(ctx); err != nil {
|
||||
return errors.WithMessage(err, "failed drop storage")
|
||||
}
|
||||
// delete the storage in the memory
|
||||
storagesMap.Delete(storage.MountPath)
|
||||
storage.Disabled = true
|
||||
err = db.UpdateStorage(storage)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "failed update storage in db, but have drop in memory. you can try restart")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateStorage update storage
|
||||
// get old storage first
|
||||
// drop the storage then reinitialize
|
||||
|
@ -70,6 +70,34 @@ func DeleteStorage(c *gin.Context) {
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
|
||||
func DisableStorage(c *gin.Context) {
|
||||
idStr := c.Query("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
if err := operations.DisableStorage(c, uint(id)); err != nil {
|
||||
common.ErrorResp(c, err, 500, true)
|
||||
return
|
||||
}
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
|
||||
func EnableStorage(c *gin.Context) {
|
||||
idStr := c.Query("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 400)
|
||||
return
|
||||
}
|
||||
if err := operations.EnableStorage(c, uint(id)); err != nil {
|
||||
common.ErrorResp(c, err, 500, true)
|
||||
return
|
||||
}
|
||||
common.SuccessResp(c)
|
||||
}
|
||||
|
||||
func GetStorage(c *gin.Context) {
|
||||
idStr := c.Query("id")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
|
@ -61,6 +61,8 @@ func admin(g *gin.RouterGroup) {
|
||||
storage.POST("/create", handles.CreateStorage)
|
||||
storage.POST("/update", handles.UpdateStorage)
|
||||
storage.POST("/delete", handles.DeleteStorage)
|
||||
storage.POST("/enable", handles.EnableStorage)
|
||||
storage.POST("/disable", handles.DisableStorage)
|
||||
|
||||
driver := g.Group("/driver")
|
||||
driver.GET("/list", handles.ListDriverItems)
|
||||
|
Loading…
x
Reference in New Issue
Block a user