diff --git a/drivers/alidrive.go b/drivers/alidrive.go index 559a0da7..7b0d664b 100644 --- a/drivers/alidrive.go +++ b/drivers/alidrive.go @@ -26,6 +26,45 @@ func init() { 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 { return []Item{ { @@ -133,6 +172,27 @@ func (a AliDrive) GetFiles(fileId string, account *model.Account) ([]AliFile, er 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 func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*model.File, error) { path = utils.ParsePath(path) @@ -191,40 +251,27 @@ func (a AliDrive) Path(path string, account *model.Account) (*model.File, []*mod } func (a AliDrive) Link(path string, account *model.Account) (string, error) { - dir, name := filepath.Split(path) - dir = utils.ParsePath(dir) - _, _, err := a.Path(dir, account) + file, err := a.GetFile(path, account) if err != nil { 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 e AliRespError - _, err = aliClient.R().SetResult(&resp). - SetError(&e). - SetHeader("authorization", "Bearer\t"+account.AccessToken). - SetBody(Json{ - "drive_id": account.DriveId, - "file_id": file.FileId, - "expire_sec": 14400, - }).Post("https://api.aliyundrive.com/v2/file/get_download_url") - if err != nil { - return "", err - } - if e.Code != "" { - return "", fmt.Errorf("%s", e.Message) - } - return resp["url"].(string), nil - } else { - return "", fmt.Errorf("can't down folder") - } - } + var resp Json + var e AliRespError + _, err = aliClient.R().SetResult(&resp). + SetError(&e). + SetHeader("authorization", "Bearer\t"+account.AccessToken). + SetBody(Json{ + "drive_id": account.DriveId, + "file_id": file.FileId, + "expire_sec": 14400, + }).Post("https://api.aliyundrive.com/v2/file/get_download_url") + if err != nil { + return "", err } - return "", fmt.Errorf("path not found") + if e.Code != "" { + return "", fmt.Errorf("%s", e.Message) + } + return resp["url"].(string), nil } type AliTokenResp struct { diff --git a/drivers/driver.go b/drivers/driver.go index 787f277b..67b6e21e 100644 --- a/drivers/driver.go +++ b/drivers/driver.go @@ -12,6 +12,7 @@ type Driver interface { Link(path string, account *model.Account) (string, error) Save(account *model.Account, old *model.Account) error Proxy(ctx *fiber.Ctx) + Preview(path string, account *model.Account) (interface{},error) // TODO //MakeDir(path string, account *model.Account) error //Move(src string, des string, account *model.Account) error diff --git a/drivers/native.go b/drivers/native.go index cb7ecf7d..69e1277f 100644 --- a/drivers/native.go +++ b/drivers/native.go @@ -16,6 +16,10 @@ import ( 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 { return []Item{ { diff --git a/server/down.go b/server/down.go index 020a065a..76825c1e 100644 --- a/server/down.go +++ b/server/down.go @@ -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 } } \ No newline at end of file diff --git a/server/path.go b/server/path.go index ac60afa1..018094fc 100644 --- a/server/path.go +++ b/server/path.go @@ -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) + } } \ No newline at end of file diff --git a/server/router.go b/server/router.go index 2a8dff33..9ed7a5c8 100644 --- a/server/router.go +++ b/server/router.go @@ -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) }