Skip to content

Commit

Permalink
feat: meta manage api
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 26, 2022
1 parent acd4083 commit 4cef3ad
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 13 deletions.
7 changes: 6 additions & 1 deletion bootstrap/data.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions internal/model/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
6 changes: 0 additions & 6 deletions internal/server/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions internal/server/common/req.go
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions internal/server/common/resp.go
Original file line number Diff line number Diff line change
@@ -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 changes: 71 additions & 0 deletions internal/server/controllers/meta.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 11 additions & 0 deletions internal/server/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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()
}
}
12 changes: 10 additions & 2 deletions internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/random/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down

0 comments on commit 4cef3ad

Please sign in to comment.