preview api

pull/548/head
微凉 2021-10-31 21:27:47 +08:00
parent a3f1553d40
commit 6313939e8c
6 changed files with 111 additions and 33 deletions

View File

@ -26,6 +26,45 @@ func init() {
type AliDrive struct{} type AliDrive struct{}
func (a AliDrive) Preview(path string, account *model.Account) (interface{}, error) {
file, err := a.GetFile(path, account)
if err != nil {
return nil, err
}
// office
var resp Json
var e AliRespError
var url string
req := Json{
"drive_id": account.DriveId,
"file_id": file.FileId,
}
switch file.Category {
case "doc":
{
url = "https://api.aliyundrive.com/v2/file/get_office_preview_url"
req["access_token"] = account.AccessToken
}
case "video":
{
url = "https://api.aliyundrive.com/v2/file/get_video_preview_play_info"
req["category"] = "live_transcoding"
}
default:
return nil, fmt.Errorf("don't support")
}
_, err = aliClient.R().SetResult(&resp).SetError(&e).
SetHeader("authorization", "Bearer\t"+account.AccessToken).
SetBody(req).Post(url)
if err != nil {
return nil, err
}
if e.Code != "" {
return nil, fmt.Errorf("%s",e.Message)
}
return resp, nil
}
func (a AliDrive) Items() []Item { func (a AliDrive) Items() []Item {
return []Item{ return []Item{
{ {
@ -133,6 +172,27 @@ func (a AliDrive) GetFiles(fileId string, account *model.Account) ([]AliFile, er
return res, nil return res, nil
} }
func (a AliDrive) GetFile(path string, account *model.Account) (*AliFile, error) {
dir, name := filepath.Split(path)
dir = utils.ParsePath(dir)
_, _, err := a.Path(dir, account)
if err != nil {
return nil, err
}
parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
parentFiles, _ := parentFiles_.([]AliFile)
for _, file := range parentFiles {
if file.Name == name {
if file.Type == "file" {
return &file, err
} else {
return nil, fmt.Errorf("not file")
}
}
}
return nil, fmt.Errorf("path not found")
}
// path: /aaa/bbb // path: /aaa/bbb
func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*model.File, error) { func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*model.File, error) {
path = utils.ParsePath(path) path = utils.ParsePath(path)
@ -191,17 +251,10 @@ func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*mod
} }
func (a AliDrive) Link(path string, account *model.Account) (string, error) { func (a AliDrive) Link(path string, account *model.Account) (string, error) {
dir, name := filepath.Split(path) file, err := a.GetFile(path, account)
dir = utils.ParsePath(dir)
_, _, err := a.Path(dir, account)
if err != nil { if err != nil {
return "", err return "", err
} }
parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
parentFiles, _ := parentFiles_.([]AliFile)
for _, file := range parentFiles {
if file.Name == name {
if file.Type == "file" {
var resp Json var resp Json
var e AliRespError var e AliRespError
_, err = aliClient.R().SetResult(&resp). _, err = aliClient.R().SetResult(&resp).
@ -219,12 +272,6 @@ func (a AliDrive) Link(path string, account *model.Account) (string, error) {
return "", fmt.Errorf("%s", e.Message) return "", fmt.Errorf("%s", e.Message)
} }
return resp["url"].(string), nil return resp["url"].(string), nil
} else {
return "", fmt.Errorf("can't down folder")
}
}
}
return "", fmt.Errorf("path not found")
} }
type AliTokenResp struct { type AliTokenResp struct {

View File

@ -12,6 +12,7 @@ type Driver interface {
Link(path string, account *model.Account) (string, error) Link(path string, account *model.Account) (string, error)
Save(account *model.Account, old *model.Account) error Save(account *model.Account, old *model.Account) error
Proxy(ctx *fiber.Ctx) Proxy(ctx *fiber.Ctx)
Preview(path string, account *model.Account) (interface{},error)
// TODO // TODO
//MakeDir(path string, account *model.Account) error //MakeDir(path string, account *model.Account) error
//Move(src string, des string, account *model.Account) error //Move(src string, des string, account *model.Account) error

View File

@ -16,6 +16,10 @@ import (
type Native struct { type Native struct {
} }
func (n Native) Preview(path string, account *model.Account) (interface{}, error) {
return nil,fmt.Errorf("no need")
}
func (n Native) Items() []Item { func (n Native) Items() []Item {
return []Item{ return []Item{
{ {

View File

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

View File

@ -64,7 +64,7 @@ func Link(ctx *fiber.Ctx) error {
} }
rawPath := req.Path rawPath := req.Path
rawPath = utils.ParsePath(rawPath) rawPath = utils.ParsePath(rawPath)
log.Debugf("down: %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)
@ -83,3 +83,23 @@ func Link(ctx *fiber.Ctx) error {
}) })
} }
} }
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 := api.Group("/public")
{ {
public.Post("/path", CheckAccount, Path) public.Post("/path", CheckAccount, Path)
public.Post("/preview", CheckAccount, Preview)
public.Get("/settings", GetSettingsPublic) public.Get("/settings", GetSettingsPublic)
public.Post("/link", CheckAccount, Link) public.Post("/link", CheckAccount, Link)
} }