preview api

This commit is contained in:
微凉
2021-10-31 21:27:47 +08:00
parent a3f1553d40
commit 6313939e8c
6 changed files with 111 additions and 33 deletions

View File

@ -2,11 +2,13 @@ package server
import (
"fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/proxy"
log "github.com/sirupsen/logrus"
"net/url"
"path/filepath"
)
func Down(ctx *fiber.Ctx) error {
@ -37,12 +39,12 @@ func Proxy(ctx *fiber.Ctx) error {
return ErrorResp(ctx,err,500)
}
rawPath = utils.ParsePath(rawPath)
log.Debugf("down: %s",rawPath)
log.Debugf("proxy: %s",rawPath)
account, path, driver, err := ParsePath(rawPath)
if err != nil {
return ErrorResp(ctx, err, 500)
}
if !account.Proxy {
if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath))!=conf.TEXT {
return ErrorResp(ctx,fmt.Errorf("[%s] not allowed proxy",account.Name),403)
}
link, err := driver.Link(path, account)
@ -54,10 +56,13 @@ func Proxy(ctx *fiber.Ctx) error {
} else {
driver.Proxy(ctx)
if err := proxy.Do(ctx, link); err != nil {
log.Errorf("proxy error: %s", err)
return ErrorResp(ctx,err,500)
}
// Remove Server header from response
ctx.Response().Header.Del(fiber.HeaderServer)
ctx.Set("Access-Control-Allow-Origin","*")
log.Debugf("proxy hedaer: %+v", ctx.Response().Header.String())
return nil
}
}

View File

@ -64,7 +64,7 @@ func Link(ctx *fiber.Ctx) error {
}
rawPath := req.Path
rawPath = utils.ParsePath(rawPath)
log.Debugf("down: %s",rawPath)
log.Debugf("link: %s",rawPath)
account, path, driver, err := ParsePath(rawPath)
if err != nil {
return ErrorResp(ctx, err, 500)
@ -82,4 +82,24 @@ func Link(ctx *fiber.Ctx) error {
"url":link,
})
}
}
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)
}
}

View File

@ -18,6 +18,7 @@ func InitApiRouter(app *fiber.App) {
public := api.Group("/public")
{
public.Post("/path", CheckAccount, Path)
public.Post("/preview", CheckAccount, Preview)
public.Get("/settings", GetSettingsPublic)
public.Post("/link", CheckAccount, Link)
}