alist/server/handles/storage.go

153 lines
3.4 KiB
Go
Raw Permalink Normal View History

2022-07-11 09:12:50 +00:00
package handles
2022-06-26 12:00:36 +00:00
import (
"context"
2022-06-28 10:12:53 +00:00
"strconv"
"github.com/alist-org/alist/v3/internal/conf"
2022-06-26 12:00:36 +00:00
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
2022-06-26 12:00:36 +00:00
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
2022-07-10 06:45:39 +00:00
func ListStorages(c *gin.Context) {
var req model.PageReq
2022-06-26 12:00:36 +00:00
if err := c.ShouldBind(&req); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 400)
2022-06-26 12:00:36 +00:00
return
}
2022-07-12 10:41:16 +00:00
req.Validate()
2022-06-26 12:00:36 +00:00
log.Debugf("%+v", req)
2022-08-14 15:46:30 +00:00
storages, total, err := db.GetStorages(req.Page, req.PerPage)
2022-06-26 12:00:36 +00:00
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c, common.PageResp{
2022-07-10 06:45:39 +00:00
Content: storages,
2022-06-26 12:00:36 +00:00
Total: total,
})
}
2022-07-10 06:45:39 +00:00
func CreateStorage(c *gin.Context) {
var req model.Storage
2022-06-26 12:00:36 +00:00
if err := c.ShouldBind(&req); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 400)
2022-06-26 12:00:36 +00:00
return
}
if id, err := op.CreateStorage(c, req); err != nil {
common.ErrorWithDataResp(c, err, 500, gin.H{
"id": id,
}, true)
2022-06-26 12:00:36 +00:00
} else {
common.SuccessResp(c, gin.H{
"id": id,
})
2022-06-26 12:00:36 +00:00
}
}
2022-07-10 06:45:39 +00:00
func UpdateStorage(c *gin.Context) {
var req model.Storage
2022-06-26 12:00:36 +00:00
if err := c.ShouldBind(&req); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 400)
2022-06-26 12:00:36 +00:00
return
}
if err := op.UpdateStorage(c, req); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500, true)
2022-06-26 12:00:36 +00:00
} else {
common.SuccessResp(c)
}
}
2022-07-10 06:45:39 +00:00
func DeleteStorage(c *gin.Context) {
2022-06-26 12:00:36 +00:00
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 400)
2022-06-26 12:00:36 +00:00
return
}
if err := op.DeleteStorageById(c, uint(id)); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500, true)
2022-06-26 12:00:36 +00:00
return
}
common.SuccessResp(c)
}
2022-07-18 15:02:14 +00:00
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 := op.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 := op.EnableStorage(c, uint(id)); err != nil {
common.ErrorResp(c, err, 500, true)
return
}
common.SuccessResp(c)
}
2022-07-18 15:02:14 +00:00
func GetStorage(c *gin.Context) {
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
storage, err := db.GetStorageById(uint(id))
if err != nil {
common.ErrorResp(c, err, 500, true)
return
}
common.SuccessResp(c, storage)
}
func LoadAllStorages(c *gin.Context) {
storages, err := db.GetEnabledStorages()
if err != nil {
log.Errorf("failed get enabled storages: %+v", err)
common.ErrorResp(c, err, 500, true)
return
}
conf.StoragesLoaded = false
go func(storages []model.Storage) {
for _, storage := range storages {
storageDriver, err := op.GetStorageByMountPath(storage.MountPath)
if err != nil {
log.Errorf("failed get storage driver: %+v", err)
continue
}
// drop the storage in the driver
if err := storageDriver.Drop(context.Background()); err != nil {
log.Errorf("failed drop storage: %+v", err)
continue
}
if err := op.LoadStorage(context.Background(), storage); err != nil {
log.Errorf("failed get enabled storages: %+v", err)
continue
}
log.Infof("success load storage: [%s], driver: [%s]",
storage.MountPath, storage.Driver)
}
conf.StoragesLoaded = true
}(storages)
common.SuccessResp(c)
}