Skip to content

Commit

Permalink
chore: Merge pull request #1093 from Xhofe/dev
Browse files Browse the repository at this point in the history
2.5.2
  • Loading branch information
xhofe authored May 13, 2022
2 parents 0237e78 + 87e3398 commit e1ccc0b
Show file tree
Hide file tree
Showing 21 changed files with 210 additions and 103 deletions.
8 changes: 5 additions & 3 deletions drivers/123/123.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package _23
import (
"errors"
"fmt"
"path/filepath"
"strconv"

"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2"
jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
"path/filepath"
"strconv"
)

func (driver Pan123) Login(account *model.Account) error {
Expand Down Expand Up @@ -46,6 +47,7 @@ func (driver Pan123) FormatFile(file *File) *model.File {
Size: file.Size,
Driver: driver.Config().Name,
UpdatedAt: file.UpdateAt,
Thumbnail: file.DownloadUrl,
}
f.Type = file.GetType()
return f
Expand All @@ -65,7 +67,7 @@ func (driver Pan123) GetFiles(parentId string, account *model.Account) ([]File,
"parentFileId": parentId,
"trashed": "false",
}
_, err := driver.Request("https://www.123pan.com/api/file/list",
_, err := driver.Request("https://www.123pan.com/api/file/list/new",
base.Get, nil, query, nil, &resp, false, account)
if err != nil {
return nil, err
Expand Down
43 changes: 17 additions & 26 deletions drivers/123/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strconv"

"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/drivers/base"
"github.com/Xhofe/alist/model"
Expand All @@ -13,12 +20,6 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strconv"
)

type Pan123 struct{}
Expand Down Expand Up @@ -125,7 +126,7 @@ func (driver Pan123) Files(path string, account *model.Account) ([]model.File, e
_ = base.SetCache(path, rawFiles, account)
}
}
files := make([]model.File, 0)
files := make([]model.File, 0, len(rawFiles))
for _, file := range rawFiles {
files = append(files, *driver.FormatFile(&file))
}
Expand Down Expand Up @@ -300,46 +301,36 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
if !parentFile.IsDir() {
return base.ErrNotFolder
}
parentFileId, _ := strconv.Atoi(parentFile.Id)

tempFile, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
if err != nil {
return err
}
defer func() {
_ = tempFile.Close()
_ = 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
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
h := md5.New()
_, err = io.Copy(h, tempFile)
if err != nil {
if _, err = io.Copy(io.MultiWriter(tempFile, h), file); err != nil {
return err
}
etag := hex.EncodeToString(h.Sum(nil))
log.Debugln("md5:", etag)

_, err = tempFile.Seek(0, io.SeekStart)
if err != nil {
return err
}

data := base.Json{
"driveId": 0,
"duplicate": true,
"duplicate": 2, // 2->覆盖 1->重命名 0->默认
"etag": etag,
"fileName": file.GetFileName(),
"parentFileId": parentFileId,
"parentFileId": parentFile.Id,
"size": file.GetSize(),
"type": 0,
}
var resp UploadResp
_, 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)
if err != nil {
return err
Expand Down
20 changes: 11 additions & 9 deletions drivers/123/types.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package _23

import (
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
"path"
"time"

"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
)

type File struct {
FileName string `json:"FileName"`
Size int64 `json:"Size"`
UpdateAt *time.Time `json:"UpdateAt"`
FileId int64 `json:"FileId"`
Type int `json:"Type"`
Etag string `json:"Etag"`
S3KeyFlag string `json:"S3KeyFlag"`
FileName string `json:"FileName"`
Size int64 `json:"Size"`
UpdateAt *time.Time `json:"UpdateAt"`
FileId int64 `json:"FileId"`
Type int `json:"Type"`
Etag string `json:"Etag"`
S3KeyFlag string `json:"S3KeyFlag"`
DownloadUrl string `json:"DownloadUrl"`
}

func (f File) GetSize() uint64 {
Expand Down
1 change: 1 addition & 0 deletions drivers/139/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ func (driver Cloud139) Upload(file *model.FileStream, account *model.Account) er
return err
}
log.Debugf("%+v", res)
res.Body.Close()
start += byteSize
}
return nil
Expand Down
1 change: 1 addition & 0 deletions drivers/189/189.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ func (driver Cloud189) NewUpload(file *model.FileStream, account *model.Account)

r, err := base.HttpClient.Do(req)
log.Debugf("%+v %+v", r, r.Request.Header)
r.Body.Close()
if err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions drivers/189pc/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ func (driver Cloud189) CommonUpload(file *model.FileStream, parentFile *model.Fi

uploadData := uploadUrl.UploadUrls[fmt.Sprint("partNumber_", i)]
req, _ := http.NewRequest(http.MethodPut, uploadData.RequestURL, byteData)
req.Header.Del("User-Agent")
for k, v := range ParseHttpHeader(uploadData.RequestHeader) {
req.Header.Set(k, v)
}
Expand All @@ -619,8 +620,10 @@ func (driver Cloud189) CommonUpload(file *model.FileStream, parentFile *model.Fi
}
if r.StatusCode != http.StatusOK {
data, _ := io.ReadAll(r.Body)
r.Body.Close()
return fmt.Errorf(string(data))
}
r.Body.Close()
}

fileMd5Hex := strings.ToUpper(hex.EncodeToString(fileMd5.Sum(nil)))
Expand Down Expand Up @@ -715,6 +718,7 @@ func (driver Cloud189) FastUpload(file *model.FileStream, parentFile *model.File
for i := 1; i <= count; i++ {
uploadData := uploadUrls.UploadUrls[fmt.Sprint("partNumber_", i)]
req, _ := http.NewRequest(http.MethodPut, uploadData.RequestURL, io.NewSectionReader(tempFile, int64(i-1)*DEFAULT, DEFAULT))
req.Header.Del("User-Agent")
for k, v := range ParseHttpHeader(uploadData.RequestHeader) {
req.Header.Set(k, v)
}
Expand All @@ -727,8 +731,10 @@ func (driver Cloud189) FastUpload(file *model.FileStream, parentFile *model.File
}
if r.StatusCode != http.StatusOK {
data, _ := io.ReadAll(r.Body)
r.Body.Close()
return fmt.Errorf(string(data))
}
r.Body.Close()
}
}

Expand Down
19 changes: 14 additions & 5 deletions drivers/alidrive/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,17 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
}

if account.Bool1 {
buf := make([]byte, 1024)
n, _ := file.Read(buf[:])
reqBody["pre_hash"] = utils.GetSHA1Encode(string(buf[:n]))
file.File = io.NopCloser(io.MultiReader(bytes.NewReader(buf[:n]), file.File))
buf := bytes.NewBuffer(make([]byte, 0, 1024))
io.CopyN(buf, file, 1024)
reqBody["pre_hash"] = utils.GetSHA1Encode(buf.String())
// 把头部拼接回去
file.File = struct {
io.Reader
io.Closer
}{
Reader: io.MultiReader(buf, file.File),
Closer: file.File,
}
} else {
reqBody["content_hash_name"] = "none"
reqBody["proof_version"] = "v1"
Expand All @@ -454,7 +461,7 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
return fmt.Errorf("%s", e.Message)
}

if e.Code == "PreHashMatched" && account.Bool1 {
if account.Bool1 && e.Code == "PreHashMatched" {
tempFile, err := ioutil.TempFile(conf.Conf.TempDir, "file-*")
if err != nil {
return err
Expand Down Expand Up @@ -499,6 +506,7 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
return nil
}

// 秒传失败
if _, err = tempFile.Seek(0, io.SeekStart); err != nil {
return err
}
Expand All @@ -515,6 +523,7 @@ func (driver AliDrive) Upload(file *model.FileStream, account *model.Account) er
return err
}
log.Debugf("%+v", res)
res.Body.Close()
//res, err := base.BaseClient.R().
// SetHeader("Content-Type","").
// SetBody(byteData).Put(resp.PartInfoList[i].UploadUrl)
Expand Down
2 changes: 1 addition & 1 deletion drivers/baiduphoto/baidu.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (driver Baidu) GetAllFile(account *model.Account) (files []File, err error)

for {
var resp FileListResp
_, err = driver.Request(http.MethodGet, FILE_API_URL+"/list", func(r *resty.Request) {
_, err = driver.Request(http.MethodGet, FILE_API_URL_V1+"/list", func(r *resty.Request) {
r.SetQueryParams(map[string]string{
"need_thumbnail": "1",
"need_filter_hidden": "0",
Expand Down
55 changes: 53 additions & 2 deletions drivers/baiduphoto/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ func (driver Baidu) Items() []base.Item {
Label: "album_id",
Type: base.TypeString,
},
{
Name: "internal_type",
Label: "download api",
Type: base.TypeSelect,
Required: true,
Values: "file,album",
Default: "album",
},
{
Name: "client_id",
Label: "client id",
Expand Down Expand Up @@ -156,6 +164,13 @@ func (driver Baidu) Files(path string, account *model.Account) ([]model.File, er
}

func (driver Baidu) Link(args base.Args, account *model.Account) (*base.Link, error) {
if account.InternalType == "file" {
return driver.LinkFile(args, account)
}
return driver.LinkAlbum(args, account)
}

func (driver Baidu) LinkAlbum(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(args.Path, account)
if err != nil {
return nil, err
Expand Down Expand Up @@ -190,6 +205,42 @@ func (driver Baidu) Link(args base.Args, account *model.Account) (*base.Link, er
}, nil
}

func (driver Baidu) LinkFile(args base.Args, account *model.Account) (*base.Link, error) {
file, err := driver.File(args.Path, account)
if err != nil {
return nil, err
}

if !IsAlbumFile(file) {
return nil, base.ErrNotSupport
}

album, err := driver.File(utils.Dir(utils.ParsePath(args.Path)), account)
if err != nil {
return nil, err
}
// 拷贝到根目录
cfile, err := driver.CopyAlbumFile(album.Id, account, file.Id)
if err != nil {
return nil, err
}

res, err := driver.Request(http.MethodGet, FILE_API_URL_V2+"/download", func(r *resty.Request) {
r.SetQueryParams(map[string]string{
"fsid": fmt.Sprint(cfile.Fsid),
})
}, account)
if err != nil {
return nil, err
}
return &base.Link{
Headers: []base.Header{
{Name: "User-Agent", Value: base.UserAgent},
},
Url: utils.Json.Get(res.Body(), "dlink").ToString(),
}, nil
}

func (driver Baidu) Path(path string, account *model.Account) (*model.File, []model.File, error) {
path = utils.ParsePath(path)
file, err := driver.File(path, account)
Expand Down Expand Up @@ -402,7 +453,7 @@ func (driver Baidu) Upload(file *model.FileStream, account *model.Account) error

// 预上传
var precreateResp PrecreateResp
_, err = driver.Request(http.MethodPost, FILE_API_URL+"/precreate", func(r *resty.Request) {
_, err = driver.Request(http.MethodPost, FILE_API_URL_V1+"/precreate", func(r *resty.Request) {
r.SetFormData(params)
r.SetResult(&precreateResp)
}, account)
Expand Down Expand Up @@ -431,7 +482,7 @@ func (driver Baidu) Upload(file *model.FileStream, account *model.Account) error
fallthrough
case 2: // 创建文件
params["uploadid"] = precreateResp.UploadID
_, err = driver.Request(http.MethodPost, FILE_API_URL+"/create", func(r *resty.Request) {
_, err = driver.Request(http.MethodPost, FILE_API_URL_V1+"/create", func(r *resty.Request) {
r.SetFormData(params)
r.SetResult(&precreateResp)
}, account)
Expand Down
2 changes: 1 addition & 1 deletion drivers/baiduphoto/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type (
type (
UploadFile struct {
FsID int64 `json:"fs_id"`
Size int `json:"size"`
Size int64 `json:"size"`
Md5 string `json:"md5"`
ServerFilename string `json:"server_filename"`
Path string `json:"path"`
Expand Down
7 changes: 4 additions & 3 deletions drivers/baiduphoto/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
)

const (
API_URL = "https://photo.baidu.com/youai"
ALBUM_API_URL = API_URL + "/album/v1"
FILE_API_URL = API_URL + "/file/v1"
API_URL = "https://photo.baidu.com/youai"
ALBUM_API_URL = API_URL + "/album/v1"
FILE_API_URL_V1 = API_URL + "/file/v1"
FILE_API_URL_V2 = API_URL + "/file/v2"
)

var (
Expand Down
Loading

0 comments on commit e1ccc0b

Please sign in to comment.