🐛 fix aliyundrive webdav delete and upload

pull/548/head
微凉 2021-12-07 16:32:59 +08:00
parent 03580fd76c
commit 43e4928bb9
3 changed files with 31 additions and 12 deletions

View File

@ -239,5 +239,5 @@ func init() {
SetRetryCount(3). SetRetryCount(3).
SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"). SetHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36").
SetHeader("content-type", "application/json"). SetHeader("content-type", "application/json").
SetHeader("origin", "https://aliyundrive.com") SetHeader("origin", "https://www.aliyundrive.com")
} }

View File

@ -1,6 +1,8 @@
package alidrive package alidrive
import ( import (
"bytes"
"errors"
"fmt" "fmt"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base" "github.com/Xhofe/alist/drivers/base"
@ -11,6 +13,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io" "io"
"math" "math"
"net/http"
"path/filepath" "path/filepath"
) )
@ -318,20 +321,21 @@ func (driver AliDrive) Copy(src string, dst string, account *model.Account) erro
return base.ErrNotSupport return base.ErrNotSupport
} }
// TODO wrong
func (driver AliDrive) Delete(path string, account *model.Account) error { func (driver AliDrive) Delete(path string, account *model.Account) error {
file, err := driver.File(path, account) file, err := driver.File(path, account)
if err != nil { if err != nil {
return err return err
} }
var resp base.Json
var e AliRespError var e AliRespError
_, err = aliClient.R().SetResult(&resp).SetError(&e). res, err := aliClient.R().SetError(&e).
SetHeader("authorization", "Bearer\t"+account.AccessToken). SetHeader("authorization", "Bearer\t"+account.AccessToken).
SetBody(base.Json{ SetBody(base.Json{
"drive_id": account.DriveId, "drive_id": account.DriveId,
"file_id": file.Id, "file_id": file.Id,
}).Post("https://api.aliyundrive.com/v2/recyclebin/trash") }).Post("https://api.aliyundrive.com/v2/recyclebin/trash")
if err != nil {
return err
}
if e.Code != "" { if e.Code != "" {
if e.Code == "AccessTokenInvalid" { if e.Code == "AccessTokenInvalid" {
err = driver.RefreshToken(account) err = driver.RefreshToken(account)
@ -344,11 +348,11 @@ func (driver AliDrive) Delete(path string, account *model.Account) error {
} }
return fmt.Errorf("%s", e.Message) return fmt.Errorf("%s", e.Message)
} }
if resp["file_id"] == file.Id { if res.StatusCode() == 204 {
_ = base.DeleteCache(utils.Dir(path), account) _ = base.DeleteCache(utils.Dir(path), account)
return nil return nil
} }
return fmt.Errorf("%+v", resp) return errors.New(res.String())
} }
type UploadResp struct { type UploadResp struct {
@ -391,8 +395,8 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
"proof_version": "v1", "proof_version": "v1",
"size": file.GetSize(), "size": file.GetSize(),
"type": "file", "type": "file",
}).Post("https://api.aliyundrive.com/adrive/v2/file/createWithFolders") }).Post("https://api.aliyundrive.com/adrive/v2/file/createWithFolders") // /v2/file/create_with_proof
log.Debugf("%+v\n%+v", resp, e) //log.Debugf("%+v\n%+v", resp, e)
if e.Code != "" { if e.Code != "" {
if e.Code == "AccessTokenInvalid" { if e.Code == "AccessTokenInvalid" {
err = driver.RefreshToken(account) err = driver.RefreshToken(account)
@ -413,20 +417,33 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
} }
log.Debugf("%d,%d",byteSize,finish) log.Debugf("%d,%d",byteSize,finish)
byteData := make([]byte, byteSize) byteData := make([]byte, byteSize)
//n, err := io.ReadFull(file, byteData) n, err := io.ReadFull(file, byteData)
//n, err := file.Read(byteData) //n, err := file.Read(byteData)
byteData, err := io.ReadAll(file) //byteData, err := io.ReadAll(file)
n := len(byteData) //n := len(byteData)
log.Debug(err,n) log.Debug(err,n)
if err != nil { if err != nil {
return err return err
} }
finish += uint64(n) finish += uint64(n)
_, err = aliClient.R().SetBody(byteData).Put(resp.PartInfoList[i].UploadUrl)
req,err := http.NewRequest("PUT", resp.PartInfoList[i].UploadUrl, bytes.NewBuffer(byteData))
if err != nil { if err != nil {
return err return err
} }
res, err := base.BaseHttpClient.Do(req)
if err != nil {
return err
}
log.Debugf("%+v", res)
//res, err := base.BaseClient.R().
// SetHeader("Content-Type","").
// SetBody(byteData).Put(resp.PartInfoList[i].UploadUrl)
//if err != nil {
// return err
//}
//log.Debugf("put to %s : %d,%s", resp.PartInfoList[i].UploadUrl, res.StatusCode(),res.String())
} }
var resp2 base.Json var resp2 base.Json
_,err = aliClient.R().SetResult(&resp2).SetError(&e). _,err = aliClient.R().SetResult(&resp2).SetError(&e).

View File

@ -81,6 +81,8 @@ func GetDrivers() map[string][]Item {
} }
var NoRedirectClient *resty.Client var NoRedirectClient *resty.Client
//var BaseClient = resty.New()
var BaseHttpClient = &http.Client{}
func init() { func init() {
NoRedirectClient = resty.New().SetRedirectPolicy( NoRedirectClient = resty.New().SetRedirectPolicy(