meta api

pull/548/head
微凉 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,13 +3,12 @@ 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) {
var meta Meta var meta Meta
meta.Path = path meta.Path = path
err := conf.DB.First(&meta).Error err := conf.DB.First(&meta).Error
@ -17,4 +16,21 @@ func GetMetaByPath(path string) (*Meta,error) {
return nil, err return nil, err
} }
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 {
@ -19,19 +20,19 @@ func Path(ctx *fiber.Ctx) error {
return ErrorResp(ctx, err, 400) return ErrorResp(ctx, err, 400)
} }
req.Path = utils.ParsePath(req.Path) req.Path = utils.ParsePath(req.Path)
log.Debugf("path: %s",req.Path) log.Debugf("path: %s", req.Path)
meta, err := model.GetMetaByPath(req.Path) meta, err := model.GetMetaByPath(req.Path)
if err == nil { if err == nil {
if meta.Password != "" && meta.Password!= req.Password { if meta.Password != "" && meta.Password != req.Password {
return ErrorResp(ctx,fmt.Errorf("wrong password"),401) return ErrorResp(ctx, fmt.Errorf("wrong password"), 401)
} }
// TODO hide or ignore? // TODO hide or ignore?
} }
if model.AccountsCount() > 1 && req.Path == "/" { if model.AccountsCount() > 1 && req.Path == "/" {
return ctx.JSON(Resp{ return ctx.JSON(Resp{
Code: 200, Code: 200,
Message: "folder", Message: "folder",
Data: model.GetAccountFiles(), Data: model.GetAccountFiles(),
}) })
} }
account, path, driver, err := ParsePath(req.Path) account, path, driver, err := ParsePath(req.Path)
@ -44,18 +45,28 @@ func Path(ctx *fiber.Ctx) error {
} }
if file != nil { if file != nil {
if account.Type == "Native" { if account.Type == "Native" {
file.Url = fmt.Sprintf("%s://%s/p%s",ctx.Protocol(),ctx.Hostname(),req.Path) file.Url = fmt.Sprintf("%s://%s/p%s", ctx.Protocol(), ctx.Hostname(), req.Path)
} }
return ctx.JSON(Resp{ return ctx.JSON(Resp{
Code: 200, Code: 200,
Message: "file", Message: "file",
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",
Data: files, Data: files,
}) })
} }
} }
@ -67,7 +78,7 @@ func Link(ctx *fiber.Ctx) error {
} }
rawPath := req.Path rawPath := req.Path
rawPath = utils.ParsePath(rawPath) rawPath = utils.ParsePath(rawPath)
log.Debugf("link: %s",rawPath) log.Debugf("link: %s", rawPath)
account, path, driver, err := ParsePath(rawPath) account, path, driver, err := ParsePath(rawPath)
if err != nil { if err != nil {
return ErrorResp(ctx, err, 500) return ErrorResp(ctx, err, 500)
@ -78,11 +89,11 @@ func Link(ctx *fiber.Ctx) error {
} }
if account.Type == "Native" { if account.Type == "Native" {
return SuccessResp(ctx, fiber.Map{ return SuccessResp(ctx, fiber.Map{
"url":fmt.Sprintf("%s://%s/p%s",ctx.Protocol(),ctx.Hostname(),rawPath), "url": fmt.Sprintf("%s://%s/p%s", ctx.Protocol(), ctx.Hostname(), rawPath),
}) })
} else { } else {
return SuccessResp(ctx,fiber.Map{ return SuccessResp(ctx, fiber.Map{
"url":link, "url": link,
}) })
} }
} }
@ -94,15 +105,15 @@ func Preview(ctx *fiber.Ctx) error {
} }
rawPath := req.Path rawPath := req.Path
rawPath = utils.ParsePath(rawPath) rawPath = utils.ParsePath(rawPath)
log.Debugf("preview: %s",rawPath) log.Debugf("preview: %s", rawPath)
account, path, driver, err := ParsePath(rawPath) account, path, driver, err := ParsePath(rawPath)
if err != nil { if err != nil {
return ErrorResp(ctx, err, 500) return ErrorResp(ctx, err, 500)
} }
data, err := driver.Preview(path, account) data, err := driver.Preview(path, account)
if err != nil { if err != nil {
return ErrorResp(ctx,err,500) return ErrorResp(ctx, err, 500)
}else { } else {
return SuccessResp(ctx,data) return SuccessResp(ctx, data)
} }
} }

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)
} }
} }