alist/server/handles/storage.go

115 lines
2.3 KiB
Go
Raw Normal View History

2022-07-11 09:12:50 +00:00
package handles
2022-06-26 12:00:36 +00:00
import (
2022-06-28 10:12:53 +00:00
"strconv"
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) {
2022-06-26 12:00:36 +00:00
var req common.PageReq
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 err := op.CreateStorage(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 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)
}