Skip to content

Commit

Permalink
fix(aliyundrive): nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
Three-taile-dragon committed Sep 26, 2024
1 parent 44f8067 commit 88f3902
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 85 deletions.
71 changes: 33 additions & 38 deletions drivers/aliyundrive/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/cron"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -52,7 +51,7 @@ func (d *AliDrive) Init(ctx context.Context) error {
return err
}
// get driver id
res, err, _ := d.requestS("https://api.alipan.com/v2/user/get", http.MethodPost, nil, nil, nil)
res, err, _ := d.request("https://api.alipan.com/v2/user/get", http.MethodPost, nil, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -92,7 +91,14 @@ func (d *AliDrive) Drop(ctx context.Context) error {
}

func (d *AliDrive) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
files, err := d.getFiles(dir.GetID())
parentId := ""
if dir.GetID() == "root" {
parentId = "root"
} else {
parentId = dir.GetID()
}

files, err := d.getFiles(parentId)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -137,14 +143,12 @@ func (d *AliDrive) Link(ctx context.Context, file model.Obj, args model.LinkArgs
}

func (d *AliDrive) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) error {
_, err, _ := d.requestS("https://api.alipan.com/adrive/v2/file/createWithFolders", http.MethodPost, func(req *resty.Request) {
req.SetBody(base.Json{
"check_name_mode": "refuse",
"drive_id": d.DriveId,
"name": dirName,
"parent_file_id": parentDir.GetID(),
"type": "folder",
})
_, err, _ := d.requestS("https://api.alipan.com/adrive/v2/file/createWithFolders", http.MethodPost, base.Json{
"check_name_mode": "refuse",
"drive_id": d.DriveId,
"name": dirName,
"parent_file_id": parentDir.GetID(),
"type": "folder",
}, nil, nil)
return err
}
Expand All @@ -155,13 +159,11 @@ func (d *AliDrive) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
}

func (d *AliDrive) Rename(ctx context.Context, srcObj model.Obj, newName string) error {
_, err, _ := d.requestS("https://api.alipan.com/v3/file/update", http.MethodPost, func(req *resty.Request) {
req.SetBody(base.Json{
"check_name_mode": "refuse",
"drive_id": d.DriveId,
"file_id": srcObj.GetID(),
"name": newName,
})
_, err, _ := d.requestS("https://api.alipan.com/v3/file/update", http.MethodPost, base.Json{
"check_name_mode": "refuse",
"drive_id": d.DriveId,
"file_id": srcObj.GetID(),
"name": newName,
}, nil, nil)
return err
}
Expand All @@ -172,11 +174,9 @@ func (d *AliDrive) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
}

func (d *AliDrive) Remove(ctx context.Context, obj model.Obj) error {
_, err, _ := d.requestS("https://api.alipan.com/v2/recyclebin/trash", http.MethodPost, func(req *resty.Request) {
req.SetBody(base.Json{
"drive_id": d.DriveId,
"file_id": obj.GetID(),
})
_, err, _ := d.requestS("https://api.alipan.com/v2/recyclebin/trash", http.MethodPost, base.Json{
"drive_id": d.DriveId,
"file_id": obj.GetID(),
}, nil, nil)
return err
}
Expand Down Expand Up @@ -232,9 +232,7 @@ func (d *AliDrive) Put(ctx context.Context, dstDir model.Obj, streamer model.Fil
}

var resp UploadResp
_, err, e := d.requestS("https://api.alipan.com/adrive/v2/file/createWithFolders", http.MethodPost, func(req *resty.Request) {
req.SetBody(reqBody)
}, nil, &resp)
_, err, e := d.requestS("https://api.alipan.com/adrive/v2/file/createWithFolders", http.MethodPost, reqBody, nil, &resp)

if err != nil && e.Code != "PreHashMatched" {
return err
Expand Down Expand Up @@ -286,9 +284,7 @@ func (d *AliDrive) Put(ctx context.Context, dstDir model.Obj, streamer model.Fil
n, _ := io.NewSectionReader(localFile, o.Int64(), 8).Read(buf[:8])
reqBody["proof_code"] = base64.StdEncoding.EncodeToString(buf[:n])

_, err, e := d.requestS("https://api.alipan.com/adrive/v2/file/createWithFolders", http.MethodPost, func(req *resty.Request) {
req.SetBody(reqBody)
}, nil, &resp)
_, err, e := d.requestS("https://api.alipan.com/adrive/v2/file/createWithFolders", http.MethodPost, reqBody, nil, &resp)
if err != nil && e.Code != "PreHashMatched" {
return err
}
Expand Down Expand Up @@ -325,12 +321,10 @@ func (d *AliDrive) Put(ctx context.Context, dstDir model.Obj, streamer model.Fil
}
}
var resp2 base.Json
_, err, e = d.requestS("https://api.alipan.com/v2/file/complete", http.MethodPost, func(req *resty.Request) {
req.SetBody(base.Json{
"drive_id": d.DriveId,
"file_id": resp.FileId,
"upload_id": resp.UploadId,
})
_, err, e = d.requestS("https://api.alipan.com/v2/file/complete", http.MethodPost, base.Json{
"drive_id": d.DriveId,
"file_id": resp.FileId,
"upload_id": resp.UploadId,
}, nil, &resp2)
if err != nil && e.Code != "PreHashMatched" {
return err
Expand All @@ -356,12 +350,13 @@ func (d *AliDrive) Other(ctx context.Context, args model.OtherArgs) (interface{}
url = "https://api.alipan.com/v2/file/get_video_preview_play_info"
data["category"] = "live_transcoding"
data["url_expire_sec"] = 14400
data["get_subtitle_info"] = true
data["mode"] = "high_res"
data["template_id"] = ""
default:
return nil, errs.NotSupport
}
_, err, _ := d.requestS(url, http.MethodPost, func(req *resty.Request) {
req.SetBody(data)
}, nil, &resp)
_, err, _ := d.requestS(url, http.MethodPost, data, nil, &resp)
if err != nil {
return nil, err
}
Expand Down
64 changes: 30 additions & 34 deletions drivers/aliyundrive/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ func (d *AliDrive) createSession() error {
state.retry = 0
return fmt.Errorf("createSession failed after three retries")
}
_, err, _ := d.requestS("https://api.alipan.com/users/v1/users/device/create_session", http.MethodPost, func(req *resty.Request) {
req.SetBody(base.Json{
"deviceName": "samsung",
"modelName": "SM-G9810",
"nonce": 0,
"pubKey": PublicKeyToHex(&state.privateKey.PublicKey),
"refreshToken": d.RefreshToken,
})
_, err, _ := d.requestS("https://api.alipan.com/users/v1/users/device/create_session", http.MethodPost, base.Json{
"deviceName": "samsung",
"modelName": "SM-G9810",
"nonce": 0,
"pubKey": PublicKeyToHex(&state.privateKey.PublicKey),
"refreshToken": d.RefreshToken,
}, nil, nil)
if err == nil {
state.retry = 0
Expand Down Expand Up @@ -188,21 +186,21 @@ func (d *AliDrive) getFiles(fileId string) ([]File, error) {
}
var resp Files
data := base.Json{
"all": false,
"enable_timeline": false,
"drive_id": d.DriveId,
"fields": "*",
"image_thumbnail_process": "image/resize,w_400/format,jpeg",
"image_url_process": "image/resize,w_1920/format,jpeg",
"limit": 200,
"image_thumbnail_process": "image/resize,m_lfit,w_256,limit_0/format,avif",
"image_url_process": "image/resize,m_lfit,w_1080/format,avif",
"limit": 100,
"marker": marker,
"order_by": d.OrderBy,
"order_direction": d.OrderDirection,
"parent_file_id": fileId,
"video_thumbnail_process": "video/snapshot,t_0,f_jpg,ar_auto,w_300",
"url_expire_sec": 14400,
"timeline_order_by": "name asc",
"video_thumbnail_process": "video/snapshot,t_120000,f_jpg,m_lfit,w_256,ar_auto,m_fast",
}
_, err, _ := d.requestS("https://api.alipan.com/v2/file/list", http.MethodPost, func(req *resty.Request) {
req.SetBody(data)
}, nil, &resp)
_, err, _ := d.requestS("https://api.alipan.com/adrive/v4/file/list", http.MethodPost, data, nil, &resp)

if err != nil {
return nil, err
Expand All @@ -214,26 +212,24 @@ func (d *AliDrive) getFiles(fileId string) ([]File, error) {
}

func (d *AliDrive) batch(srcId, dstId string, url string) error {
res, err, _ := d.requestS("https://api.alipan.com/v3/batch", http.MethodPost, func(req *resty.Request) {
req.SetBody(base.Json{
"requests": []base.Json{
{
"headers": base.Json{
"Content-Type": "application/json",
},
"method": "POST",
"id": srcId,
"body": base.Json{
"drive_id": d.DriveId,
"file_id": srcId,
"to_drive_id": d.DriveId,
"to_parent_file_id": dstId,
},
"url": url,
res, err, _ := d.requestS("https://api.alipan.com/v3/batch", http.MethodPost, base.Json{
"requests": []base.Json{
{
"headers": base.Json{
"Content-Type": "application/json",
},
"method": "POST",
"id": srcId,
"body": base.Json{
"drive_id": d.DriveId,
"file_id": srcId,
"to_drive_id": d.DriveId,
"to_parent_file_id": dstId,
},
"url": url,
},
"resource": "file",
})
},
"resource": "file",
}, nil, nil)
if err != nil {
return err
Expand Down
10 changes: 8 additions & 2 deletions drivers/wopan/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,16 @@ func (d *Wopan) Remove(ctx context.Context, obj model.Obj) error {
}

func (d *Wopan) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
_, err := d.client.Upload2C(d.getSpaceType(), wopan.Upload2CFile{
file, err := convertToFile(stream)
if err != nil {
return err
}
defer file.Close()

_, err = d.client.Upload2C(d.getSpaceType(), wopan.Upload2CFile{
Name: stream.GetName(),
Size: stream.GetSize(),
Content: stream,
Content: file,
ContentType: stream.GetMimetype(),
}, dstDir.GetID(), d.FamilyID, wopan.Upload2COption{
OnProgress: func(current, total int64) {
Expand Down
2 changes: 1 addition & 1 deletion drivers/wopan/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Object struct {
FID string
}

func fileToObj(file wopan.File) (model.Obj, error) {
func fileToObj(file *wopan.File) (model.Obj, error) {
t, err := getTime(file.CreateTime)
if err != nil {
return nil, err
Expand Down
26 changes: 26 additions & 0 deletions drivers/wopan/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package template

import (
"github.com/alist-org/alist/v3/internal/model"
"io"
"os"
"time"

"github.com/xhofe/wopan-sdk-go"
Expand Down Expand Up @@ -38,3 +41,26 @@ func (d *Wopan) getSpaceType() string {
func getTime(str string) (time.Time, error) {
return time.Parse("20060102150405", str)
}

// 将 FileStreamer 转换为 *os.File
func convertToFile(stream model.FileStreamer) (*os.File, error) {
// 创建一个临时文件
tmpFile, err := os.CreateTemp("", "stream-*.tmp")
if err != nil {
return nil, err
}

// 将 stream 的内容复制到临时文件中
if _, err := io.Copy(tmpFile, stream); err != nil {
tmpFile.Close()
return nil, err
}

// 重新打开临时文件,以便读取
if _, err := tmpFile.Seek(0, io.SeekStart); err != nil {
tmpFile.Close()
return nil, err
}

return tmpFile, nil
}
11 changes: 6 additions & 5 deletions internal/search/meilisearch/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/alist-org/alist/v3/internal/search/searcher"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/meilisearch/meilisearch-go"
"time"
)

var config = searcher.Config{
Expand All @@ -18,10 +19,10 @@ var config = searcher.Config{
func init() {
searcher.RegisterSearcher(config, func() (searcher.Searcher, error) {
m := Meilisearch{
Client: meilisearch.NewClient(meilisearch.ClientConfig{
Host: conf.Conf.Meilisearch.Host,
APIKey: conf.Conf.Meilisearch.APIKey,
}),
Client: meilisearch.New(
conf.Conf.Meilisearch.Host,
meilisearch.WithAPIKey(conf.Conf.Meilisearch.APIKey),
),
IndexUid: conf.Conf.Meilisearch.IndexPrefix + "alist",
FilterableAttributes: []string{"parent", "is_dir", "name"},
SearchableAttributes: []string{"name"},
Expand All @@ -39,7 +40,7 @@ func init() {
if err != nil {
return nil, err
}
forTask, err := m.Client.WaitForTask(task.TaskUID)
forTask, err := m.Client.WaitForTask(task.TaskUID, time.Second)
if err != nil {
return nil, err
}
Expand Down
7 changes: 2 additions & 5 deletions internal/search/meilisearch/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type searchDocument struct {
}

type Meilisearch struct {
Client *meilisearch.Client
Client meilisearch.ServiceManager
IndexUid string
FilterableAttributes []string
SearchableAttributes []string
Expand Down Expand Up @@ -216,10 +216,7 @@ func (m *Meilisearch) Clear(ctx context.Context) error {
}

func (m *Meilisearch) getTaskStatus(ctx context.Context, taskUID int64) (meilisearch.TaskStatus, error) {
forTask, err := m.Client.WaitForTask(taskUID, meilisearch.WaitParams{
Context: ctx,
Interval: time.Second,
})
forTask, err := m.Client.WaitForTask(taskUID, time.Second)
if err != nil {
return meilisearch.TaskStatusUnknown, err
}
Expand Down

0 comments on commit 88f3902

Please sign in to comment.