mirror of
https://github.com/AlistGo/alist.git
synced 2025-04-24 22:34:04 +08:00
feat: meta manage api
This commit is contained in:
parent
acd4083399
commit
4cef3adc90
@ -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,
|
||||
|
@ -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{
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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)
|
||||
|
6
internal/server/common/req.go
Normal file
6
internal/server/common/req.go
Normal 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"`
|
||||
}
|
12
internal/server/common/resp.go
Normal file
12
internal/server/common/resp.go
Normal 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"`
|
||||
}
|
71
internal/server/controllers/meta.go
Normal file
71
internal/server/controllers/meta.go
Normal 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)
|
||||
}
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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))]
|
||||
|
Loading…
x
Reference in New Issue
Block a user