mirror of https://github.com/Xhofe/alist
parent
639b7817bf
commit
1b14d33b9f
|
@ -6,9 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/drivers/base"
|
"github.com/alist-org/alist/v3/drivers/base"
|
||||||
"github.com/alist-org/alist/v3/internal/conf"
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
|
@ -17,6 +15,7 @@ import (
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
"github.com/alist-org/alist/v3/server/common"
|
"github.com/alist-org/alist/v3/server/common"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AListV3 struct {
|
type AListV3 struct {
|
||||||
|
@ -42,7 +41,7 @@ func (d *AListV3) Init(ctx context.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// if the username is not empty and the username is not the same as the current username, then login again
|
// if the username is not empty and the username is not the same as the current username, then login again
|
||||||
if d.Username != "" && d.Username != resp.Data.Username {
|
if d.Username != resp.Data.Username {
|
||||||
err = d.login()
|
err = d.login()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -183,15 +182,42 @@ func (d *AListV3) Remove(ctx context.Context, obj model.Obj) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *AListV3) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
|
func (d *AListV3) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
|
||||||
_, err := d.requestWithTimeout("/fs/put", http.MethodPut, func(req *resty.Request) {
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, d.Address+"/api/fs/put", stream)
|
||||||
req.SetHeader("File-Path", path.Join(dstDir.GetPath(), stream.GetName())).
|
if err != nil {
|
||||||
SetHeader("Password", d.MetaPassword).
|
|
||||||
SetHeader("Content-Length", strconv.FormatInt(stream.GetSize(), 10)).
|
|
||||||
SetContentLength(true).
|
|
||||||
SetBody(io.ReadCloser(stream))
|
|
||||||
}, time.Hour*6)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
req.Header.Set("Authorization", d.Token)
|
||||||
|
req.Header.Set("File-Path", path.Join(dstDir.GetPath(), stream.GetName()))
|
||||||
|
req.Header.Set("Password", d.MetaPassword)
|
||||||
|
|
||||||
|
req.ContentLength = stream.GetSize()
|
||||||
|
// client := base.NewHttpClient()
|
||||||
|
// client.Timeout = time.Hour * 6
|
||||||
|
res, err := base.HttpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Debugf("[alist_v3] response body: %s", string(bytes))
|
||||||
|
if res.StatusCode >= 400 {
|
||||||
|
return fmt.Errorf("request failed, status: %s", res.Status)
|
||||||
|
}
|
||||||
|
code := utils.Json.Get(bytes, "code").ToInt()
|
||||||
|
if code != 200 {
|
||||||
|
if code == 401 || code == 403 {
|
||||||
|
err = d.login()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("request failed,code: %d, message: %s", code, utils.Json.Get(bytes, "message").ToString())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
//func (d *AList) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
|
//func (d *AList) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
|
||||||
// return nil, errs.NotSupport
|
// return nil, errs.NotSupport
|
||||||
|
|
|
@ -3,7 +3,6 @@ package alist_v3
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/drivers/base"
|
"github.com/alist-org/alist/v3/drivers/base"
|
||||||
"github.com/alist-org/alist/v3/internal/op"
|
"github.com/alist-org/alist/v3/internal/op"
|
||||||
|
@ -14,6 +13,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *AListV3) login() error {
|
func (d *AListV3) login() error {
|
||||||
|
if d.Username == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
var resp common.Resp[LoginResp]
|
var resp common.Resp[LoginResp]
|
||||||
_, err := d.request("/auth/login", http.MethodPost, func(req *resty.Request) {
|
_, err := d.request("/auth/login", http.MethodPost, func(req *resty.Request) {
|
||||||
req.SetResult(&resp).SetBody(base.Json{
|
req.SetResult(&resp).SetBody(base.Json{
|
||||||
|
@ -57,33 +59,3 @@ func (d *AListV3) request(api, method string, callback base.ReqCallback, retry .
|
||||||
}
|
}
|
||||||
return res.Body(), nil
|
return res.Body(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *AListV3) requestWithTimeout(api, method string, callback base.ReqCallback, timeout time.Duration, retry ...bool) ([]byte, error) {
|
|
||||||
url := d.Address + "/api" + api
|
|
||||||
client := base.NewRestyClient().SetTimeout(timeout)
|
|
||||||
req := client.R()
|
|
||||||
req.SetHeader("Authorization", d.Token)
|
|
||||||
if callback != nil {
|
|
||||||
callback(req)
|
|
||||||
}
|
|
||||||
res, err := req.Execute(method, url)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
log.Debugf("[alist_v3] response body: %s", res.String())
|
|
||||||
if res.StatusCode() >= 400 {
|
|
||||||
return nil, fmt.Errorf("request failed, status: %s", res.Status())
|
|
||||||
}
|
|
||||||
code := utils.Json.Get(res.Body(), "code").ToInt()
|
|
||||||
if code != 200 {
|
|
||||||
if (code == 401 || code == 403) && !utils.IsBool(retry...) {
|
|
||||||
err = d.login()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return d.requestWithTimeout(api, method, callback, timeout, true)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("request failed,code: %d, message: %s", code, utils.Json.Get(res.Body(), "message").ToString())
|
|
||||||
}
|
|
||||||
return res.Body(), nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue