alist/server/path.go

105 lines
2.2 KiB
Go
Raw Normal View History

2021-10-26 14:28:37 +00:00
package server
import (
2021-10-28 14:50:09 +00:00
"fmt"
2021-10-26 14:28:37 +00:00
"github.com/Xhofe/alist/model"
2021-10-28 14:50:09 +00:00
"github.com/Xhofe/alist/utils"
2021-10-26 14:28:37 +00:00
"github.com/gofiber/fiber/v2"
2021-10-28 14:50:09 +00:00
log "github.com/sirupsen/logrus"
2021-10-26 14:28:37 +00:00
)
type PathReq struct {
Path string `json:"Path"`
Password string `json:"Password"`
}
func Path(ctx *fiber.Ctx) error {
var req PathReq
if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx, err, 400)
}
2021-10-28 14:50:09 +00:00
req.Path = utils.ParsePath(req.Path)
log.Debugf("path: %s",req.Path)
meta, err := model.GetMetaByPath(req.Path)
if err == nil {
if meta.Password != "" && meta.Password!= req.Password {
return ErrorResp(ctx,fmt.Errorf("wrong password"),401)
}
// TODO hide or ignore?
2021-10-28 04:37:31 +00:00
}
if model.AccountsCount() > 1 && req.Path == "/" {
2021-10-26 14:28:37 +00:00
return ctx.JSON(Resp{
Code: 200,
2021-10-29 16:35:29 +00:00
Message: "folder",
2021-10-26 14:28:37 +00:00
Data: model.GetAccountFiles(),
})
}
account, path, driver, err := ParsePath(req.Path)
if err != nil {
return ErrorResp(ctx, err, 500)
}
file, files, err := driver.Path(path, account)
if err != nil {
return ErrorResp(ctx, err, 500)
}
if file != nil {
return ctx.JSON(Resp{
Code: 200,
2021-10-29 16:35:29 +00:00
Message: "file",
2021-10-26 14:28:37 +00:00
Data: []*model.File{file},
})
} else {
return ctx.JSON(Resp{
Code: 200,
2021-10-29 16:35:29 +00:00
Message: "folder",
2021-10-26 14:28:37 +00:00
Data: files,
})
}
}
2021-10-30 16:36:17 +00:00
func Link(ctx *fiber.Ctx) error {
var req PathReq
if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx, err, 400)
}
rawPath := req.Path
rawPath = utils.ParsePath(rawPath)
2021-10-31 13:27:47 +00:00
log.Debugf("link: %s",rawPath)
2021-10-30 16:36:17 +00:00
account, path, driver, err := ParsePath(rawPath)
if err != nil {
return ErrorResp(ctx, err, 500)
}
link, err := driver.Link(path, account)
if err != nil {
return ErrorResp(ctx, err, 500)
}
if account.Type == "Native" {
return SuccessResp(ctx, fiber.Map{
"url":"",
})
} else {
return SuccessResp(ctx,fiber.Map{
"url":link,
})
}
2021-10-31 13:27:47 +00:00
}
func Preview(ctx *fiber.Ctx) error {
var req PathReq
if err := ctx.BodyParser(&req); err != nil {
return ErrorResp(ctx, err, 400)
}
rawPath := req.Path
rawPath = utils.ParsePath(rawPath)
log.Debugf("preview: %s",rawPath)
account, path, driver, err := ParsePath(rawPath)
if err != nil {
return ErrorResp(ctx, err, 500)
}
data, err := driver.Preview(path, account)
if err != nil {
return ErrorResp(ctx,err,500)
}else {
return SuccessResp(ctx,data)
}
2021-10-30 16:36:17 +00:00
}