feat: meta manage api

This commit is contained in:
Noah Hsu 2022-06-26 19:09:28 +08:00
parent acd4083399
commit 4cef3adc90
10 changed files with 120 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package bootstrap
import (
"github.com/alist-org/alist/v3/cmd/args"
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils/random"
@ -15,11 +16,15 @@ func InitData() {
func initUser() {
admin, err := db.GetAdmin()
adminPassword := random.String(8)
if args.Dev {
adminPassword = "admin"
}
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
admin = &model.User{
Username: "admin",
Password: random.RandomStr(8),
Password: adminPassword,
Role: model.ADMIN,
BasePath: "/",
Webdav: true,

View File

@ -47,7 +47,7 @@ func DefaultConfig() *Config {
return &Config{
Address: "0.0.0.0",
Port: 5244,
JwtSecret: random.RandomStr(16),
JwtSecret: random.String(16),
Assets: "https://npm.elemecdn.com/alist-web@$version/dist",
TempDir: "data/temp",
Database: Database{

View File

@ -4,8 +4,8 @@ type Meta struct {
ID uint `json:"id" gorm:"primaryKey"`
Path string `json:"path" gorm:"unique" binding:"required"`
Password string `json:"password"`
Hide string `json:"hide"`
Upload bool `json:"upload"`
OnlyShows string `json:"only_shows"`
Hide string `json:"hide"`
SubFolder bool `json:"sub_folder"`
Readme string `json:"readme"`
}

View File

@ -5,12 +5,6 @@ import (
log "github.com/sirupsen/logrus"
)
type Resp struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
if len(l) != 0 && l[0] {
log.Errorf("%+v", err)

View File

@ -0,0 +1,6 @@
package common
type PageReq struct {
PageIndex int `json:"page_index" form:"page_index"`
PageSize int `json:"page_size" form:"page_size"`
}

View File

@ -0,0 +1,12 @@
package common
type Resp struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
type PageResp struct {
Content interface{} `json:"content"`
Total int64 `json:"total"`
}

View File

@ -0,0 +1,71 @@
package controllers
import (
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/server/common"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"strconv"
)
func ListMetas(c *gin.Context) {
var req common.PageReq
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
log.Debugf("%+v", req)
metas, total, err := db.GetMetas(req.PageIndex, req.PageSize)
if err != nil {
common.ErrorResp(c, err, 500, true)
return
}
common.SuccessResp(c, common.PageResp{
Content: metas,
Total: total,
})
}
func CreateMeta(c *gin.Context) {
var req model.Meta
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
req.Path = utils.StandardizePath(req.Path)
if err := db.CreateMeta(&req); err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
}
func UpdateMeta(c *gin.Context) {
var req model.Meta
if err := c.ShouldBind(&req); err != nil {
common.ErrorResp(c, err, 400)
return
}
req.Path = utils.StandardizePath(req.Path)
if err := db.UpdateMeta(&req); err != nil {
common.ErrorResp(c, err, 500)
} else {
common.SuccessResp(c)
}
}
func DeleteMeta(c *gin.Context) {
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
if err := db.DeleteMetaById(uint(id)); err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}

View File

@ -2,6 +2,7 @@ package middlewares
import (
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/server/common"
"github.com/gin-gonic/gin"
)
@ -36,3 +37,13 @@ func Auth(c *gin.Context) {
c.Set("user", user)
c.Next()
}
func AuthAdmin(c *gin.Context) {
user := c.MustGet("user").(*model.User)
if !user.IsAdmin() {
common.ErrorStrResp(c, "You are not an admin", 403)
c.Abort()
} else {
c.Next()
}
}

View File

@ -14,8 +14,16 @@ func Init(r *gin.Engine) {
Cors(r)
api := r.Group("/api", middlewares.Auth)
api.POST("/user/login", controllers.Login)
api.GET("/user/current", controllers.CurrentUser)
api.POST("/auth/login", controllers.Login)
api.GET("/auth/current", controllers.CurrentUser)
admin := api.Group("/admin", middlewares.AuthAdmin)
meta := admin.Group("/meta")
meta.GET("/list", controllers.ListMetas)
meta.POST("/create", controllers.CreateMeta)
meta.POST("/update", controllers.UpdateMeta)
meta.POST("/delete", controllers.DeleteMeta)
}
func Cors(r *gin.Engine) {

View File

@ -9,7 +9,7 @@ var Rand *rand.Rand
const letterBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
func RandomStr(n int) string {
func String(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[Rand.Intn(len(letterBytes))]