diff --git a/alist.go b/alist.go index e2d13237..c2235e1c 100644 --- a/alist.go +++ b/alist.go @@ -1,7 +1,6 @@ package main import ( - "flag" "fmt" "github.com/Xhofe/alist/bootstrap" "github.com/Xhofe/alist/conf" @@ -12,14 +11,6 @@ import ( 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 { //bootstrap.InitLog() bootstrap.InitConf() diff --git a/bootstrap/log.go b/bootstrap/log.go index 2bdc1b19..e8ac6918 100644 --- a/bootstrap/log.go +++ b/bootstrap/log.go @@ -1,6 +1,7 @@ package bootstrap import ( + "flag" "github.com/Xhofe/alist/conf" log "github.com/sirupsen/logrus" ) @@ -22,5 +23,10 @@ func InitLog() { } 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() } \ No newline at end of file diff --git a/drivers/alidrive/driver.go b/drivers/alidrive/driver.go index 116db4e4..dc702a61 100644 --- a/drivers/alidrive/driver.go +++ b/drivers/alidrive/driver.go @@ -9,6 +9,7 @@ import ( "github.com/gin-gonic/gin" "github.com/robfig/cron/v3" log "github.com/sirupsen/logrus" + "io" "math" "path/filepath" ) @@ -317,6 +318,7 @@ func (driver AliDrive) Copy(src string, dst string, account *model.Account) erro return base.ErrNotSupport } +// TODO wrong func (driver AliDrive) Delete(path string, account *model.Account) error { file, err := driver.File(path, account) if err != nil { @@ -350,12 +352,18 @@ func (driver AliDrive) Delete(path string, account *model.Account) error { } 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 { - const DEFAULT int64 = 10485760 - var count = math.Ceil(float64(file.GetSize()) / float64(DEFAULT)) - //var finish int64 = 0 + const DEFAULT uint64 = 10485760 + var count = int64(math.Ceil(float64(file.GetSize()) / float64(DEFAULT))) + var finish uint64 = 0 parentFile, err := driver.File(file.ParentPath, account) if err != nil { return err @@ -363,7 +371,8 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er var resp UploadResp var e AliRespError 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{ "part_number": i + 1, }) @@ -377,12 +386,13 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er "drive_id": account.DriveId, "name": file.GetFileName(), "parent_file_id": parentFile.Id, - "part_info_list": partInfoList, + "part_info_list": partInfoList, //proof_code "proof_version": "v1", "size": file.GetSize(), "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 == "AccessTokenInvalid" { 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 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) diff --git a/server/path.go b/server/path.go index 4b3df0db..d0977747 100644 --- a/server/path.go +++ b/server/path.go @@ -11,8 +11,8 @@ import ( ) type PathReq struct { - Path string `json:"ParentPath"` - Password string `json:"Password"` + Path string `json:"path"` + Password string `json:"password"` } func Path(c *gin.Context) { diff --git a/server/webdav/file.go b/server/webdav/file.go index 16e991c5..5dc8148d 100644 --- a/server/webdav/file.go +++ b/server/webdav/file.go @@ -15,7 +15,6 @@ import ( "net/http" "path" "path/filepath" - "strconv" "strings" "time" ) @@ -154,10 +153,11 @@ func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath strin if err != nil { return err } - fileSize, err := strconv.ParseUint(r.Header.Get("Content-Length"), 10, 64) - if err != nil { - return err - } + //fileSize, err := strconv.ParseUint(r.Header.Get("Content-Length"), 10, 64) + fileSize := uint64(r.ContentLength) + //if err != nil { + // return err + //} filePath, fileName := filepath.Split(path_) fileData := model.FileStream{ MIMEType: r.Header.Get("Content-Type"),