meta api

This commit is contained in:
微凉 2021-11-02 19:25:54 +08:00
parent 8b81aeb5a1
commit 510292c8b0
6 changed files with 98 additions and 28 deletions

1
.gitignore vendored
View File

@ -20,7 +20,6 @@ dist/
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
*.yml
bin/* bin/*
alist alist
*.json *.json

View File

@ -21,6 +21,7 @@ type Account struct {
OrderDirection string `json:"order_direction"` OrderDirection string `json:"order_direction"`
Proxy bool `json:"proxy"` Proxy bool `json:"proxy"`
UpdatedAt *time.Time `json:"updated_at"` UpdatedAt *time.Time `json:"updated_at"`
Search bool `json:"search"`
} }
var accountsMap = map[string]Account{} var accountsMap = map[string]Account{}

View File

@ -3,10 +3,9 @@ package model
import "github.com/Xhofe/alist/conf" import "github.com/Xhofe/alist/conf"
type Meta struct { type Meta struct {
Path string `json:"path" gorm:"primaryKey"` Path string `json:"path" gorm:"primaryKey" validate:"required"`
Password string `json:"password"` Password string `json:"password"`
Hide bool `json:"hide"` Hide string `json:"hide"`
Ignore bool `json:"ignore"`
} }
func GetMetaByPath(path string) (*Meta, error) { func GetMetaByPath(path string) (*Meta, error) {
@ -18,3 +17,20 @@ func GetMetaByPath(path string) (*Meta,error) {
} }
return &meta, nil return &meta, nil
} }
func SaveMeta(meta Meta) error {
return conf.DB.Save(meta).Error
}
func DeleteMeta(path string) error {
meta := Meta{Path: path}
return conf.DB.Delete(&meta).Error
}
func GetMetas() (*[]Meta, error) {
var metas []Meta
if err := conf.DB.Find(&metas).Error; err != nil {
return nil, err
}
return &metas, nil
}

39
server/meta.go Normal file
View File

@ -0,0 +1,39 @@
package server
import (
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/gofiber/fiber/v2"
)
func GetMetas(ctx *fiber.Ctx) error {
metas,err := model.GetMetas()
if err != nil {
return ErrorResp(ctx,err,500)
}
return SuccessResp(ctx, metas)
}
func SaveMeta(ctx *fiber.Ctx) error {
var req model.Meta
if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx, err, 400)
}
if err := validate.Struct(req); err != nil {
return ErrorResp(ctx, err, 400)
}
if err := model.SaveMeta(req); err != nil {
return ErrorResp(ctx, err, 500)
} else {
return SuccessResp(ctx)
}
}
func DeleteMeta(ctx *fiber.Ctx) error {
path := ctx.Query("path")
path = utils.ParsePath(path)
if err := model.DeleteMeta(path); err != nil {
return ErrorResp(ctx, err, 500)
}
return SuccessResp(ctx)
}

View File

@ -6,6 +6,7 @@ import (
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"strings"
) )
type PathReq struct { type PathReq struct {
@ -52,6 +53,16 @@ func Path(ctx *fiber.Ctx) error {
Data: []*model.File{file}, Data: []*model.File{file},
}) })
} else { } else {
if meta != nil && meta.Hide != "" {
tmpFiles := make([]*model.File, 0)
hideFiles := strings.Split(meta.Hide, ",")
for _, item := range files {
if !utils.IsContain(hideFiles, item.Name) {
tmpFiles = append(tmpFiles, item)
}
}
files = tmpFiles
}
return ctx.JSON(Resp{ return ctx.JSON(Resp{
Code: 200, Code: 200,
Message: "folder", Message: "folder",

View File

@ -34,5 +34,9 @@ func InitApiRouter(app *fiber.App) {
admin.Delete("/account", DeleteAccount) admin.Delete("/account", DeleteAccount)
admin.Get("/drivers", GetDrivers) admin.Get("/drivers", GetDrivers)
admin.Get("/clear_cache",ClearCache) admin.Get("/clear_cache",ClearCache)
admin.Get("/metas", GetMetas)
admin.Post("/meta", SaveMeta)
admin.Delete("/meta", DeleteMeta)
} }
} }