alist/server/handles/storage.go

115 lines
2.3 KiB
Go
Raw Normal View History

2022-07-11 17:12:50 +08:00
package handles
2022-06-26 20:00:36 +08:00
import (
2022-06-28 18:12:53 +08:00
"strconv"
2022-06-26 20:00:36 +08: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 20:00:36 +08:00
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
2022-07-10 14:45:39 +08:00
func ListStorages(c *gin.Context) {
2022-06-26 20:00:36 +08:00
var req common.PageReq
if err := c.ShouldBind(&req); err != nil {
2022-06-28 18:12:53 +08:00
common.ErrorResp(c, err, 400)
2022-06-26 20:00:36 +08:00
return
}
2022-07-12 18:41:16 +08:00
req.Validate()
2022-06-26 20:00:36 +08:00
log.Debugf("%+v", req)
2022-08-14 23:46:30 +08:00
storages, total, err := db.GetStorages(req.Page, req.PerPage)
2022-06-26 20:00:36 +08:00
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c, common.PageResp{
2022-07-10 14:45:39 +08:00
Content: storages,
2022-06-26 20:00:36 +08:00
Total: total,
})
}
2022-07-10 14:45:39 +08:00
func CreateStorage(c *gin.Context) {
var req model.Storage
2022-06-26 20:00:36 +08:00
if err := c.ShouldBind(&req); err != nil {
2022-06-28 18:12:53 +08:00
common.ErrorResp(c, err, 400)
2022-06-26 20:00:36 +08:00
return
}
if err := op.CreateStorage(c, req); err != nil {
2022-06-28 18:12:53 +08:00
common.ErrorResp(c, err, 500, true)
2022-06-26 20:00:36 +08:00
} else {
common.SuccessResp(c)
}
}
2022-07-10 14:45:39 +08:00
func UpdateStorage(c *gin.Context) {
var req model.Storage
2022-06-26 20:00:36 +08:00
if err := c.ShouldBind(&req); err != nil {
2022-06-28 18:12:53 +08:00
common.ErrorResp(c, err, 400)
2022-06-26 20:00:36 +08:00
return
}
if err := op.UpdateStorage(c, req); err != nil {
2022-06-28 18:12:53 +08:00
common.ErrorResp(c, err, 500, true)
2022-06-26 20:00:36 +08:00
} else {
common.SuccessResp(c)
}
}
2022-07-10 14:45:39 +08:00
func DeleteStorage(c *gin.Context) {
2022-06-26 20:00:36 +08:00
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
2022-06-28 18:12:53 +08:00
common.ErrorResp(c, err, 400)
2022-06-26 20:00:36 +08:00
return
}
if err := op.DeleteStorageById(c, uint(id)); err != nil {
2022-06-28 18:12:53 +08:00
common.ErrorResp(c, err, 500, true)
2022-06-26 20:00:36 +08:00
return
}
common.SuccessResp(c)
}
2022-07-18 23:02:14 +08: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 23:02:14 +08: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)
}