🚧 aliyundrive webdav write

pull/548/head
微凉 2021-12-06 22:14:32 +08:00
parent 28998d6f8c
commit 6e8d551420
5 changed files with 76 additions and 23 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"github.com/Xhofe/alist/bootstrap" "github.com/Xhofe/alist/bootstrap"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
@ -12,14 +11,6 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func init() {
flag.StringVar(&conf.ConfigFile, "conf", "data/config.json", "config file")
flag.BoolVar(&conf.Debug, "debug", false, "start with debug mode")
flag.BoolVar(&conf.Version, "version", false, "print version info")
flag.BoolVar(&conf.Password, "password", false, "print current password")
flag.Parse()
}
func Init() bool { func Init() bool {
//bootstrap.InitLog() //bootstrap.InitLog()
bootstrap.InitConf() bootstrap.InitConf()

View File

@ -1,6 +1,7 @@
package bootstrap package bootstrap
import ( import (
"flag"
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -22,5 +23,10 @@ func InitLog() {
} }
func init() { func init() {
flag.StringVar(&conf.ConfigFile, "conf", "data/config.json", "config file")
flag.BoolVar(&conf.Debug, "debug", false, "start with debug mode")
flag.BoolVar(&conf.Version, "version", false, "print version info")
flag.BoolVar(&conf.Password, "password", false, "print current password")
flag.Parse()
InitLog() InitLog()
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io"
"math" "math"
"path/filepath" "path/filepath"
) )
@ -317,6 +318,7 @@ 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 {
@ -350,12 +352,18 @@ func (driver AliDrive) Delete(path string, account *model.Account) error {
} }
type UploadResp struct { type UploadResp struct {
FileId string `json:"file_id"`
UploadId string `json:"upload_id"`
PartInfoList []struct {
UploadUrl string `json:"upload_url"`
} `json:"part_info_list"`
} }
// TODO wrong
func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) error { func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) error {
const DEFAULT int64 = 10485760 const DEFAULT uint64 = 10485760
var count = math.Ceil(float64(file.GetSize()) / float64(DEFAULT)) var count = int64(math.Ceil(float64(file.GetSize()) / float64(DEFAULT)))
//var finish int64 = 0 var finish uint64 = 0
parentFile, err := driver.File(file.ParentPath, account) parentFile, err := driver.File(file.ParentPath, account)
if err != nil { if err != nil {
return err return err
@ -363,7 +371,8 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
var resp UploadResp var resp UploadResp
var e AliRespError var e AliRespError
partInfoList := make([]base.Json, 0) partInfoList := make([]base.Json, 0)
for i := 0; i < int(count); i++ { var i int64
for i = 0; i < count; i++ {
partInfoList = append(partInfoList, base.Json{ partInfoList = append(partInfoList, base.Json{
"part_number": i + 1, "part_number": i + 1,
}) })
@ -382,7 +391,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/v2/recyclebin/trash") }).Post("https://api.aliyundrive.com/adrive/v2/file/createWithFolders")
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)
@ -395,7 +405,53 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
} }
return fmt.Errorf("%s", e.Message) return fmt.Errorf("%s", e.Message)
} }
return base.ErrNotImplement var byteSize uint64
for i = 0; i < count; i++ {
byteSize = file.GetSize() - finish
if DEFAULT < byteSize {
byteSize = DEFAULT
}
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)
if err != nil {
return err
}
finish += uint64(n)
_, err = aliClient.R().SetBody(byteData).Put(resp.PartInfoList[i].UploadUrl)
if err != nil {
return err
}
}
var resp2 base.Json
_,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")
if e.Code != "" {
//if e.Code == "AccessTokenInvalid" {
// err = driver.RefreshToken(account)
// if err != nil {
// return err
// } else {
// _ = model.SaveAccount(account)
// return driver.Upload(file, account)
// }
//}
return fmt.Errorf("%s", e.Message)
}
if resp2["file_id"] == resp.FileId {
return nil
}
return fmt.Errorf("%+v", resp2)
} }
var _ base.Driver = (*AliDrive)(nil) var _ base.Driver = (*AliDrive)(nil)

View File

@ -11,8 +11,8 @@ import (
) )
type PathReq struct { type PathReq struct {
Path string `json:"ParentPath"` Path string `json:"path"`
Password string `json:"Password"` Password string `json:"password"`
} }
func Path(c *gin.Context) { func Path(c *gin.Context) {

View File

@ -15,7 +15,6 @@ import (
"net/http" "net/http"
"path" "path"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -154,10 +153,11 @@ func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath strin
if err != nil { if err != nil {
return err return err
} }
fileSize, err := strconv.ParseUint(r.Header.Get("Content-Length"), 10, 64) //fileSize, err := strconv.ParseUint(r.Header.Get("Content-Length"), 10, 64)
if err != nil { fileSize := uint64(r.ContentLength)
return err //if err != nil {
} // return err
//}
filePath, fileName := filepath.Split(path_) filePath, fileName := filepath.Split(path_)
fileData := model.FileStream{ fileData := model.FileStream{
MIMEType: r.Header.Get("Content-Type"), MIMEType: r.Header.Get("Content-Type"),