2022-07-11 09:12:50 +00:00
|
|
|
package handles
|
2022-06-26 12:00:36 +00:00
|
|
|
|
|
|
|
import (
|
2022-12-21 11:21:18 +00:00
|
|
|
"context"
|
2022-06-28 10:12:53 +00:00
|
|
|
"strconv"
|
|
|
|
|
2022-12-21 11:21:18 +00:00
|
|
|
"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"
|
2022-08-31 13:01:15 +00:00
|
|
|
"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) {
|
2022-11-28 05:45:25 +00:00
|
|
|
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
|
|
|
|
}
|
2022-11-13 12:17:10 +00:00
|
|
|
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 {
|
2022-11-13 12:17:10 +00:00
|
|
|
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
|
|
|
|
}
|
2022-08-31 13:01:15 +00:00
|
|
|
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
|
|
|
|
}
|
2022-08-31 13:01:15 +00:00
|
|
|
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
|
|
|
|
2022-08-11 13:08:50 +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
|
|
|
|
}
|
2022-08-31 13:01:15 +00:00
|
|
|
if err := op.DisableStorage(c, uint(id)); err != nil {
|
2022-08-11 13:08:50 +00:00
|
|
|
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
|
|
|
|
}
|
2022-08-31 13:01:15 +00:00
|
|
|
if err := op.EnableStorage(c, uint(id)); err != nil {
|
2022-08-11 13:08:50 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-12-21 11:21:18 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|