alist/server/handles/meta.go

110 lines
2.2 KiB
Go
Raw Normal View History

2022-07-11 09:12:50 +00:00
package handles
2022-06-26 11:09:28 +00:00
import (
2022-06-30 08:09:06 +00:00
"fmt"
2022-06-28 10:12:53 +00:00
"strconv"
2022-06-30 08:09:06 +00:00
"strings"
2022-06-28 10:12:53 +00:00
2022-06-26 11:09:28 +00:00
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/op"
2022-06-26 11:20:19 +00:00
"github.com/alist-org/alist/v3/server/common"
2024-01-01 09:16:07 +00:00
"github.com/dlclark/regexp2"
2022-06-26 11:09:28 +00:00
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func ListMetas(c *gin.Context) {
var req model.PageReq
2022-06-26 11:09:28 +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 11:09:28 +00:00
return
}
2022-07-12 10:41:16 +00:00
req.Validate()
2022-06-26 11:09:28 +00:00
log.Debugf("%+v", req)
metas, total, err := op.GetMetas(req.Page, req.PerPage)
2022-06-26 11:09:28 +00:00
if err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500, true)
2022-06-26 11:09:28 +00:00
return
}
2022-06-26 11:20:19 +00:00
common.SuccessResp(c, common.PageResp{
2022-06-26 11:09:28 +00:00
Content: metas,
Total: total,
})
}
func CreateMeta(c *gin.Context) {
var req model.Meta
if err := c.ShouldBind(&req); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 400)
2022-06-26 11:09:28 +00:00
return
}
2022-06-30 08:09:06 +00:00
r, err := validHide(req.Hide)
if err != nil {
common.ErrorStrResp(c, fmt.Sprintf("%s is illegal: %s", r, err.Error()), 400)
return
}
if err := op.CreateMeta(&req); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500, true)
2022-06-26 11:09:28 +00:00
} else {
2022-06-26 11:20:19 +00:00
common.SuccessResp(c)
2022-06-26 11:09:28 +00:00
}
}
func UpdateMeta(c *gin.Context) {
var req model.Meta
if err := c.ShouldBind(&req); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 400)
2022-06-26 11:09:28 +00:00
return
}
2022-06-30 08:09:06 +00:00
r, err := validHide(req.Hide)
if err != nil {
common.ErrorStrResp(c, fmt.Sprintf("%s is illegal: %s", r, err.Error()), 400)
return
}
if err := op.UpdateMeta(&req); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500, true)
2022-06-26 11:09:28 +00:00
} else {
2022-06-26 11:20:19 +00:00
common.SuccessResp(c)
2022-06-26 11:09:28 +00:00
}
}
2022-06-30 08:09:06 +00:00
func validHide(hide string) (string, error) {
rs := strings.Split(hide, "\n")
for _, r := range rs {
2024-01-01 09:16:07 +00:00
_, err := regexp2.Compile(r, regexp2.None)
2022-06-30 08:09:06 +00:00
if err != nil {
return r, err
}
}
return "", nil
}
2022-06-26 11:09:28 +00:00
func DeleteMeta(c *gin.Context) {
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 11:09:28 +00:00
return
}
if err := op.DeleteMetaById(uint(id)); err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500, true)
2022-06-26 11:09:28 +00:00
return
}
2022-06-26 11:20:19 +00:00
common.SuccessResp(c)
2022-06-26 11:09:28 +00:00
}
2022-07-27 09:41:25 +00:00
func GetMeta(c *gin.Context) {
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
meta, err := op.GetMetaById(uint(id))
2022-07-27 09:41:25 +00:00
if err != nil {
common.ErrorResp(c, err, 500, true)
return
}
common.SuccessResp(c, meta)
}