mirror of https://github.com/Xhofe/alist
feat: meta manage api
parent
acd4083399
commit
4cef3adc90
|
@ -1,6 +1,7 @@
|
||||||
package bootstrap
|
package bootstrap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/alist-org/alist/v3/cmd/args"
|
||||||
"github.com/alist-org/alist/v3/internal/db"
|
"github.com/alist-org/alist/v3/internal/db"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/pkg/utils/random"
|
"github.com/alist-org/alist/v3/pkg/utils/random"
|
||||||
|
@ -15,11 +16,15 @@ func InitData() {
|
||||||
|
|
||||||
func initUser() {
|
func initUser() {
|
||||||
admin, err := db.GetAdmin()
|
admin, err := db.GetAdmin()
|
||||||
|
adminPassword := random.String(8)
|
||||||
|
if args.Dev {
|
||||||
|
adminPassword = "admin"
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
admin = &model.User{
|
admin = &model.User{
|
||||||
Username: "admin",
|
Username: "admin",
|
||||||
Password: random.RandomStr(8),
|
Password: adminPassword,
|
||||||
Role: model.ADMIN,
|
Role: model.ADMIN,
|
||||||
BasePath: "/",
|
BasePath: "/",
|
||||||
Webdav: true,
|
Webdav: true,
|
||||||
|
|
|
@ -47,7 +47,7 @@ func DefaultConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
Address: "0.0.0.0",
|
Address: "0.0.0.0",
|
||||||
Port: 5244,
|
Port: 5244,
|
||||||
JwtSecret: random.RandomStr(16),
|
JwtSecret: random.String(16),
|
||||||
Assets: "https://npm.elemecdn.com/alist-web@$version/dist",
|
Assets: "https://npm.elemecdn.com/alist-web@$version/dist",
|
||||||
TempDir: "data/temp",
|
TempDir: "data/temp",
|
||||||
Database: Database{
|
Database: Database{
|
||||||
|
|
|
@ -4,8 +4,8 @@ type Meta struct {
|
||||||
ID uint `json:"id" gorm:"primaryKey"`
|
ID uint `json:"id" gorm:"primaryKey"`
|
||||||
Path string `json:"path" gorm:"unique" binding:"required"`
|
Path string `json:"path" gorm:"unique" binding:"required"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Hide string `json:"hide"`
|
|
||||||
Upload bool `json:"upload"`
|
Upload bool `json:"upload"`
|
||||||
OnlyShows string `json:"only_shows"`
|
Hide string `json:"hide"`
|
||||||
|
SubFolder bool `json:"sub_folder"`
|
||||||
Readme string `json:"readme"`
|
Readme string `json:"readme"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,6 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
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) {
|
func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
|
||||||
if len(l) != 0 && l[0] {
|
if len(l) != 0 && l[0] {
|
||||||
log.Errorf("%+v", err)
|
log.Errorf("%+v", err)
|
||||||
|
|
|
@ -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"`
|
||||||
|
}
|
|
@ -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"`
|
||||||
|
}
|
|
@ -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 (
|
import (
|
||||||
"github.com/alist-org/alist/v3/internal/db"
|
"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/internal/server/common"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
@ -36,3 +37,13 @@ func Auth(c *gin.Context) {
|
||||||
c.Set("user", user)
|
c.Set("user", user)
|
||||||
c.Next()
|
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)
|
Cors(r)
|
||||||
|
|
||||||
api := r.Group("/api", middlewares.Auth)
|
api := r.Group("/api", middlewares.Auth)
|
||||||
api.POST("/user/login", controllers.Login)
|
api.POST("/auth/login", controllers.Login)
|
||||||
api.GET("/user/current", controllers.CurrentUser)
|
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) {
|
func Cors(r *gin.Engine) {
|
||||||
|
|
|
@ -9,7 +9,7 @@ var Rand *rand.Rand
|
||||||
|
|
||||||
const letterBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
const letterBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
|
||||||
func RandomStr(n int) string {
|
func String(n int) string {
|
||||||
b := make([]byte, n)
|
b := make([]byte, n)
|
||||||
for i := range b {
|
for i := range b {
|
||||||
b[i] = letterBytes[Rand.Intn(len(letterBytes))]
|
b[i] = letterBytes[Rand.Intn(len(letterBytes))]
|
||||||
|
|
Loading…
Reference in New Issue