Skip to content

Commit

Permalink
🎨 change link interface
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Dec 9, 2021
1 parent b36eaf0 commit a295e70
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 146 deletions.
26 changes: 9 additions & 17 deletions alist-proxy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
// We support the GET, POST, HEAD, and OPTIONS methods from any origin,
// and allow any header on requests. These headers must be present
// on all responses to all CORS preflight requests. In practice, this means
// all responses to OPTIONS requests.

const HOST = "YOUR_HOST";
const TOKEN = "YOUR_TOKEN";

Expand All @@ -26,8 +21,6 @@ async function handleRequest(request) {
JSON.stringify({
code: 401,
message: `sign mismatch`,
you: sign,
right: right,
}),
{
headers: {
Expand All @@ -49,18 +42,17 @@ async function handleRequest(request) {
path: decodeURI(path),
}),
});
let data = await resp.json();
if (data.code != 200) {
return new Response(JSON.stringify(data));
let res = await resp.json();
if (res.code !== 200) {
return new Response(JSON.stringify(res));
}
let headers = {};
if (data.data.header) {
headers[data.data.header.name] = data.data.header.value;
request = new Request(res.data.url, request);
if (res.data.headers) {
for(const header of res.data.headers){
request.headers.set(header.name, header.value);
}
}
let response = await fetch(data.data.url, {
method: "GET",
headers: headers,
});
let response = await fetch(request);

// Recreate the response so we can modify the headers
response = new Response(response.body, response);
Expand Down
29 changes: 18 additions & 11 deletions drivers/123/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ func (driver Pan123) Files(path string, account *model.Account) ([]model.File, e
return files, nil
}

func (driver Pan123) Link(path string, account *model.Account) (string, error) {
func (driver Pan123) Link(path string, account *model.Account) (*base.Link, error) {
file, err := driver.GetFile(utils.ParsePath(path), account)
if err != nil {
return "", err
return nil, err
}
var resp Pan123DownResp
_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken).
Expand All @@ -136,32 +136,35 @@ func (driver Pan123) Link(path string, account *model.Account) (string, error) {
"type": file.Type,
}).Post("https://www.123pan.com/api/file/download_info")
if err != nil {
return "", err
return nil, err
}
if resp.Code != 0 {
if resp.Code == 401 {
err := driver.Login(account)
if err != nil {
return "", err
return nil, err
}
return driver.Link(path, account)
}
return "", fmt.Errorf(resp.Message)
return nil, fmt.Errorf(resp.Message)
}
u, err := url.Parse(resp.Data.DownloadUrl)
if err != nil {
return "", err
return nil, err
}
u_ := fmt.Sprintf("https://%s%s", u.Host, u.Path)
res, err := base.NoRedirectClient.R().SetQueryParamsFromValues(u.Query()).Get(u_)
if err != nil {
return "", err
return nil, err
}
log.Debug(res.String())
link := base.Link{}
if res.StatusCode() == 302 {
return res.Header().Get("location"), nil
link.Url = res.Header().Get("location")
}else {
link.Url = resp.Data.DownloadUrl
}
return resp.Data.DownloadUrl, nil
return &link, nil
}

func (driver Pan123) Path(path string, account *model.Account) (*model.File, []model.File, error) {
Expand All @@ -171,8 +174,12 @@ func (driver Pan123) Path(path string, account *model.Account) (*model.File, []m
if err != nil {
return nil, nil, err
}
if file.Type != conf.FOLDER {
file.Url, _ = driver.Link(path, account)
if !file.IsDir() {
link, err := driver.Link(path, account)
if err != nil {
return nil, nil, err
}
file.Url = link.Url
return file, nil, nil
}
files, err := driver.Files(path, account)
Expand Down
31 changes: 19 additions & 12 deletions drivers/189/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ func (driver Cloud189) Files(path string, account *model.Account) ([]model.File,
return files, nil
}

func (driver Cloud189) Link(path string, account *model.Account) (string, error) {
func (driver Cloud189) Link(path string, account *model.Account) (*base.Link, error) {
file, err := driver.File(utils.ParsePath(path), account)
if err != nil {
return "", err
return nil, err
}
if file.Type == conf.FOLDER {
return "", base.ErrNotFile
return nil, base.ErrNotFile
}
client, ok := client189Map[account.Name]
if !ok {
return "", fmt.Errorf("can't find [%s] client", account.Name)
return nil, fmt.Errorf("can't find [%s] client", account.Name)
}
var e Cloud189Error
var resp Cloud189Down
Expand All @@ -148,28 +148,31 @@ func (driver Cloud189) Link(path string, account *model.Account) (string, error)
"fileId": file.Id,
}).Get("https://cloud.189.cn/api/open/file/getFileDownloadUrl.action")
if err != nil {
return "", err
return nil, err
}
if e.ErrorCode != "" {
if e.ErrorCode == "InvalidSessionKey" {
err = driver.Login(account)
if err != nil {
return "", err
return nil, err
}
return driver.Link(path, account)
}
}
if resp.ResCode != 0 {
return "", fmt.Errorf(resp.ResMessage)
return nil, fmt.Errorf(resp.ResMessage)
}
res, err := base.NoRedirectClient.R().Get(resp.FileDownloadUrl)
if err != nil {
return "", err
return nil, err
}
link := base.Link{}
if res.StatusCode() == 302 {
return res.Header().Get("location"), nil
link.Url = res.Header().Get("location")
}else {
link.Url = resp.FileDownloadUrl
}
return resp.FileDownloadUrl, nil
return &link, nil
}

func (driver Cloud189) Path(path string, account *model.Account) (*model.File, []model.File, error) {
Expand All @@ -179,8 +182,12 @@ func (driver Cloud189) Path(path string, account *model.Account) (*model.File, [
if err != nil {
return nil, nil, err
}
if file.Type != conf.FOLDER {
file.Url, _ = driver.Link(path, account)
if !file.IsDir() {
link, err := driver.Link(path, account)
if err != nil {
return nil, nil, err
}
file.Url = link.Url
return file, nil, nil
}
files, err := driver.Files(path, account)
Expand Down
38 changes: 22 additions & 16 deletions drivers/alidrive/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ func (driver AliDrive) Files(path string, account *model.Account) ([]model.File,
return files, nil
}

func (driver AliDrive) Link(path string, account *model.Account) (string, error) {
func (driver AliDrive) Link(path string, account *model.Account) (*base.Link, error) {
file, err := driver.File(path, account)
if err != nil {
return "", err
return nil, err
}
var resp base.Json
var e AliRespError
Expand All @@ -174,21 +174,23 @@ func (driver AliDrive) Link(path string, account *model.Account) (string, error)
"expire_sec": 14400,
}).Post("https://api.aliyundrive.com/v2/file/get_download_url")
if err != nil {
return "", err
return nil, err
}
if e.Code != "" {
if e.Code == "AccessTokenInvalid" {
err = driver.RefreshToken(account)
if err != nil {
return "", err
return nil, err
} else {
_ = model.SaveAccount(account)
return driver.Link(path, account)
}
}
return "", fmt.Errorf("%s", e.Message)
return nil, fmt.Errorf("%s", e.Message)
}
return resp["url"].(string), nil
return &base.Link{
Url: resp["url"].(string),
}, nil
}

func (driver AliDrive) Path(path string, account *model.Account) (*model.File, []model.File, error) {
Expand All @@ -198,8 +200,12 @@ func (driver AliDrive) Path(path string, account *model.Account) (*model.File, [
if err != nil {
return nil, nil, err
}
if file.Type != conf.FOLDER {
file.Url, _ = driver.Link(path, account)
if !file.IsDir() {
link, err := driver.Link(path, account)
if err != nil {
return nil, nil, err
}
file.Url = link.Url
return file, nil, nil
}
files, err := driver.Files(path, account)
Expand Down Expand Up @@ -414,20 +420,20 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
if DEFAULT < byteSize {
byteSize = DEFAULT
}
log.Debugf("%d,%d",byteSize,finish)
log.Debugf("%d,%d", byteSize, finish)
byteData := make([]byte, byteSize)
n, err := io.ReadFull(file, byteData)
//n, err := file.Read(byteData)
//byteData, err := io.ReadAll(file)
//n := len(byteData)
log.Debug(err,n)
log.Debug(err, n)
if err != nil {
return err
}

finish += uint64(n)

req,err := http.NewRequest("PUT", resp.PartInfoList[i].UploadUrl, bytes.NewBuffer(byteData))
req, err := http.NewRequest("PUT", resp.PartInfoList[i].UploadUrl, bytes.NewBuffer(byteData))
if err != nil {
return err
}
Expand All @@ -445,13 +451,13 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
//log.Debugf("put to %s : %d,%s", resp.PartInfoList[i].UploadUrl, res.StatusCode(),res.String())
}
var resp2 base.Json
_,err = aliClient.R().SetResult(&resp2).SetError(&e).
_, err = aliClient.R().SetResult(&resp2).SetError(&e).
SetHeader("authorization", "Bearer\t"+account.AccessToken).
SetBody(base.Json{
"drive_id": account.DriveId,
"file_id": resp.FileId,
"upload_id": resp.UploadId,
}).Post("https://api.aliyundrive.com/v2/file/complete")
"drive_id": account.DriveId,
"file_id": resp.FileId,
"upload_id": resp.UploadId,
}).Post("https://api.aliyundrive.com/v2/file/complete")
if e.Code != "" {
//if e.Code == "AccessTokenInvalid" {
// err = driver.RefreshToken(account)
Expand Down
6 changes: 4 additions & 2 deletions drivers/alist/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,16 @@ func (driver Alist) Files(path string, account *model.Account) ([]model.File, er
return files, nil
}

func (driver Alist) Link(path string, account *model.Account) (string, error) {
func (driver Alist) Link(path string, account *model.Account) (*base.Link, error) {
path = utils.ParsePath(path)
name := utils.Base(path)
flag := "d"
if utils.GetFileType(filepath.Ext(path)) == conf.TEXT {
flag = "p"
}
return fmt.Sprintf("%s/%s%s?sign=%s", account.SiteUrl, flag, path, utils.SignWithToken(name, conf.Token)), nil
link := base.Link{}
link.Url = fmt.Sprintf("%s/%s%s?sign=%s", account.SiteUrl, flag, path, utils.SignWithToken(name, conf.Token))
return &link, nil
}

func (driver Alist) Path(path string, account *model.Account) (*model.File, []model.File, error) {
Expand Down
25 changes: 14 additions & 11 deletions drivers/base/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

type DriverConfig struct {
Name string
OnlyProxy bool
NeedHeader bool
Name string
OnlyProxy bool
NoLink bool // 必须本机返回的
}

type Driver interface {
Expand All @@ -20,7 +20,7 @@ type Driver interface {
Save(account *model.Account, old *model.Account) error
File(path string, account *model.Account) (*model.File, error)
Files(path string, account *model.Account) ([]model.File, error)
Link(path string, account *model.Account) (string, error)
Link(path string, account *model.Account) (*Link, error)
Path(path string, account *model.Account) (*model.File, []model.File, error)
Proxy(c *gin.Context, account *model.Account)
Preview(path string, account *model.Account) (interface{}, error)
Expand Down Expand Up @@ -84,13 +84,16 @@ func GetDrivers() map[string][]Item {
},
}, v.Items()...)
}
res[k] = append(res[k], Item{
Name: "proxy_url",
Label: "proxy_url",
Type: TypeString,
Required: false,
Description: "proxy url",
})
// 不支持给本地文件添加中转
if v.Config().Name != "Native" {
res[k] = append(res[k], Item{
Name: "proxy_url",
Label: "proxy_url",
Type: TypeString,
Required: false,
Description: "proxy url",
})
}
}
return res
}
Expand Down
10 changes: 10 additions & 0 deletions drivers/base/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ type TokenResp struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}

type Header struct{
Name string `json:"name"`
Value string `json:"value"`
}

type Link struct {
Url string `json:"url"`
Headers []Header `json:"headers"`
}
Loading

0 comments on commit a295e70

Please sign in to comment.