mirror of https://github.com/Xhofe/alist
perf(123):upload
parent
f917882a84
commit
3b59bb5c09
|
@ -3,14 +3,15 @@ package _23
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/Xhofe/alist/drivers/base"
|
"github.com/Xhofe/alist/drivers/base"
|
||||||
"github.com/Xhofe/alist/model"
|
"github.com/Xhofe/alist/model"
|
||||||
"github.com/Xhofe/alist/utils"
|
"github.com/Xhofe/alist/utils"
|
||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (driver Pan123) Login(account *model.Account) error {
|
func (driver Pan123) Login(account *model.Account) error {
|
||||||
|
|
|
@ -4,6 +4,13 @@ import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/Xhofe/alist/conf"
|
"github.com/Xhofe/alist/conf"
|
||||||
"github.com/Xhofe/alist/drivers/base"
|
"github.com/Xhofe/alist/drivers/base"
|
||||||
"github.com/Xhofe/alist/model"
|
"github.com/Xhofe/alist/model"
|
||||||
|
@ -13,12 +20,6 @@ import (
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Pan123 struct{}
|
type Pan123 struct{}
|
||||||
|
@ -300,46 +301,36 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
|
||||||
if !parentFile.IsDir() {
|
if !parentFile.IsDir() {
|
||||||
return base.ErrNotFolder
|
return base.ErrNotFolder
|
||||||
}
|
}
|
||||||
parentFileId, _ := strconv.Atoi(parentFile.Id)
|
|
||||||
tempFile, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
|
tempFile, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer tempFile.Close()
|
||||||
_ = tempFile.Close()
|
defer os.Remove(tempFile.Name())
|
||||||
_ = os.Remove(tempFile.Name())
|
|
||||||
}()
|
|
||||||
_, err = io.Copy(tempFile, file)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = tempFile.Seek(0, io.SeekStart)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
_, err = io.Copy(h, tempFile)
|
if _, err = io.Copy(io.MultiWriter(tempFile, h), file); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
etag := hex.EncodeToString(h.Sum(nil))
|
etag := hex.EncodeToString(h.Sum(nil))
|
||||||
log.Debugln("md5:", etag)
|
|
||||||
_, err = tempFile.Seek(0, io.SeekStart)
|
_, err = tempFile.Seek(0, io.SeekStart)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
data := base.Json{
|
data := base.Json{
|
||||||
"driveId": 0,
|
"driveId": 0,
|
||||||
"duplicate": true,
|
"duplicate": 2, // 2->覆盖 1->重命名 0->默认
|
||||||
"etag": etag,
|
"etag": etag,
|
||||||
"fileName": file.GetFileName(),
|
"fileName": file.GetFileName(),
|
||||||
"parentFileId": parentFileId,
|
"parentFileId": parentFile.Id,
|
||||||
"size": file.GetSize(),
|
"size": file.GetSize(),
|
||||||
"type": 0,
|
"type": 0,
|
||||||
}
|
}
|
||||||
var resp UploadResp
|
var resp UploadResp
|
||||||
_, err = driver.Request("https://www.123pan.com/api/file/upload_request",
|
_, err = driver.Request("https://www.123pan.com/api/file/upload_request",
|
||||||
base.Post, nil, nil, &data, &resp, false, account)
|
base.Post, map[string]string{"app-version": "1.1"}, nil, &data, &resp, false, account)
|
||||||
//res, err := driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
//res, err := driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue