alist/server/path.go

120 lines
2.7 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-11-02 11:25:54 +00:00
"strings"
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)
2021-11-02 11:25:54 +00:00
log.Debugf("path: %s", req.Path)
2021-10-28 14:50:09 +00:00
meta, err := model.GetMetaByPath(req.Path)
if err == nil {
2021-11-02 11:25:54 +00:00
if meta.Password != "" && meta.Password != req.Password {
return ErrorResp(ctx, fmt.Errorf("wrong password"), 401)
2021-10-28 14:50:09 +00:00
}
// 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{
2021-11-02 11:25:54 +00:00
Code: 200,
Message: "folder",
Data: model.GetAccountFiles(),
2021-10-26 14:28:37 +00:00
})
}
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 {
2021-11-01 14:42:24 +00:00
if account.Type == "Native" {
2021-11-02 11:25:54 +00:00
file.Url = fmt.Sprintf("%s://%s/p%s", ctx.Protocol(), ctx.Hostname(), req.Path)
2021-11-01 14:42:24 +00:00
}
2021-10-26 14:28:37 +00:00
return ctx.JSON(Resp{
2021-11-02 11:25:54 +00:00
Code: 200,
Message: "file",
Data: []*model.File{file},
2021-10-26 14:28:37 +00:00
})
} else {
2021-11-02 11:25:54 +00:00
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
}
2021-10-26 14:28:37 +00:00
return ctx.JSON(Resp{
2021-11-02 11:25:54 +00:00
Code: 200,
Message: "folder",
Data: files,
2021-10-26 14:28:37 +00:00
})
}
}
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-11-02 11:25:54 +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{
2021-11-02 11:25:54 +00:00
"url": fmt.Sprintf("%s://%s/p%s", ctx.Protocol(), ctx.Hostname(), rawPath),
2021-10-30 16:36:17 +00:00
})
} else {
2021-11-02 11:25:54 +00:00
return SuccessResp(ctx, fiber.Map{
"url": link,
2021-10-30 16:36:17 +00:00
})
}
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)
2021-11-02 11:25:54 +00:00
log.Debugf("preview: %s", rawPath)
2021-10-31 13:27:47 +00:00
account, path, driver, err := ParsePath(rawPath)
if err != nil {
return ErrorResp(ctx, err, 500)
}
data, err := driver.Preview(path, account)
if err != nil {
2021-11-02 11:25:54 +00:00
return ErrorResp(ctx, err, 500)
} else {
return SuccessResp(ctx, data)
2021-10-31 13:27:47 +00:00
}
2021-11-02 11:25:54 +00:00
}