mirror of https://github.com/Xhofe/alist
				
				
				
			✨ preview api
							parent
							
								
									a3f1553d40
								
							
						
					
					
						commit
						6313939e8c
					
				|  | @ -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 { | ||||
|  |  | |||
|  | @ -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
 | ||||
|  |  | |||
|  | @ -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{ | ||||
| 		{ | ||||
|  |  | |||
|  | @ -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 | ||||
| 	} | ||||
| } | ||||
|  | @ -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) | ||||
| 	} | ||||
| } | ||||
|  | @ -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) | ||||
| 	} | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	 微凉
						微凉