Skip to content

Commit b60da97

Browse files
Lanfeixhofe
andauthored
feat(offline-download): allow using offline download tools in any storage (#7716)
* Feat(offline-download): allow using thunder offline download tool in any storage * Feat(offline-download): allow using 115 offline download tool in any storage * Feat(offline-download): allow using pikpak offline download tool in any storage * style(offline-download): unify offline download tool names * feat(offline-download): show available offline download tools only * Fix(offline-download): update unmodified tool names. --------- Co-authored-by: Andy Hsu <i@nn.ci>
1 parent e04114d commit b60da97

File tree

14 files changed

+478
-204
lines changed

14 files changed

+478
-204
lines changed

internal/conf/const.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ const (
5858
TransmissionUri = "transmission_uri"
5959
TransmissionSeedtime = "transmission_seedtime"
6060

61+
// 115
62+
Pan115TempDir = "115_temp_dir"
63+
64+
// pikpak
65+
PikPakTempDir = "pikpak_temp_dir"
66+
67+
// thunder
68+
ThunderTempDir = "thunder_temp_dir"
69+
6170
// single
6271
Token = "token"
6372
IndexProgress = "index_progress"

internal/offline_download/115/client.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package _115
33
import (
44
"context"
55
"fmt"
6+
"github.com/alist-org/alist/v3/internal/conf"
7+
"github.com/alist-org/alist/v3/internal/setting"
68

79
"github.com/alist-org/alist/v3/drivers/115"
810
"github.com/alist-org/alist/v3/internal/errs"
@@ -33,13 +35,23 @@ func (p *Cloud115) Init() (string, error) {
3335
}
3436

3537
func (p *Cloud115) IsReady() bool {
38+
tempDir := setting.GetStr(conf.Pan115TempDir)
39+
if tempDir == "" {
40+
return false
41+
}
42+
storage, _, err := op.GetStorageAndActualPath(tempDir)
43+
if err != nil {
44+
return false
45+
}
46+
if _, ok := storage.(*_115.Pan115); !ok {
47+
return false
48+
}
3649
return true
3750
}
3851

3952
func (p *Cloud115) AddURL(args *tool.AddUrlArgs) (string, error) {
4053
// 添加新任务刷新缓存
4154
p.refreshTaskCache = true
42-
// args.TempDir 已经被修改为了 DstDirPath
4355
storage, actualPath, err := op.GetStorageAndActualPath(args.TempDir)
4456
if err != nil {
4557
return "", err
@@ -50,6 +62,11 @@ func (p *Cloud115) AddURL(args *tool.AddUrlArgs) (string, error) {
5062
}
5163

5264
ctx := context.Background()
65+
66+
if err := op.MakeDir(ctx, storage, actualPath); err != nil {
67+
return "", err
68+
}
69+
5370
parentDir, err := op.GetUnwrap(ctx, storage, actualPath)
5471
if err != nil {
5572
return "", err
@@ -64,7 +81,7 @@ func (p *Cloud115) AddURL(args *tool.AddUrlArgs) (string, error) {
6481
}
6582

6683
func (p *Cloud115) Remove(task *tool.DownloadTask) error {
67-
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
84+
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
6885
if err != nil {
6986
return err
7087
}
@@ -81,7 +98,7 @@ func (p *Cloud115) Remove(task *tool.DownloadTask) error {
8198
}
8299

83100
func (p *Cloud115) Status(task *tool.DownloadTask) (*tool.Status, error) {
84-
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
101+
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
85102
if err != nil {
86103
return nil, err
87104
}

internal/offline_download/pikpak/pikpak.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package pikpak
33
import (
44
"context"
55
"fmt"
6+
"github.com/alist-org/alist/v3/internal/conf"
7+
"github.com/alist-org/alist/v3/internal/setting"
68
"strconv"
79

810
"github.com/alist-org/alist/v3/drivers/pikpak"
@@ -17,7 +19,7 @@ type PikPak struct {
1719
}
1820

1921
func (p *PikPak) Name() string {
20-
return "pikpak"
22+
return "PikPak"
2123
}
2224

2325
func (p *PikPak) Items() []model.SettingItem {
@@ -34,13 +36,23 @@ func (p *PikPak) Init() (string, error) {
3436
}
3537

3638
func (p *PikPak) IsReady() bool {
39+
tempDir := setting.GetStr(conf.PikPakTempDir)
40+
if tempDir == "" {
41+
return false
42+
}
43+
storage, _, err := op.GetStorageAndActualPath(tempDir)
44+
if err != nil {
45+
return false
46+
}
47+
if _, ok := storage.(*pikpak.PikPak); !ok {
48+
return false
49+
}
3750
return true
3851
}
3952

4053
func (p *PikPak) AddURL(args *tool.AddUrlArgs) (string, error) {
4154
// 添加新任务刷新缓存
4255
p.refreshTaskCache = true
43-
// args.TempDir 已经被修改为了 DstDirPath
4456
storage, actualPath, err := op.GetStorageAndActualPath(args.TempDir)
4557
if err != nil {
4658
return "", err
@@ -51,6 +63,11 @@ func (p *PikPak) AddURL(args *tool.AddUrlArgs) (string, error) {
5163
}
5264

5365
ctx := context.Background()
66+
67+
if err := op.MakeDir(ctx, storage, actualPath); err != nil {
68+
return "", err
69+
}
70+
5471
parentDir, err := op.GetUnwrap(ctx, storage, actualPath)
5572
if err != nil {
5673
return "", err
@@ -65,7 +82,7 @@ func (p *PikPak) AddURL(args *tool.AddUrlArgs) (string, error) {
6582
}
6683

6784
func (p *PikPak) Remove(task *tool.DownloadTask) error {
68-
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
85+
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
6986
if err != nil {
7087
return err
7188
}
@@ -82,7 +99,7 @@ func (p *PikPak) Remove(task *tool.DownloadTask) error {
8299
}
83100

84101
func (p *PikPak) Status(task *tool.DownloadTask) (*tool.Status, error) {
85-
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
102+
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
86103
if err != nil {
87104
return nil, err
88105
}

internal/offline_download/thunder/thunder.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"github.com/alist-org/alist/v3/internal/conf"
8+
"github.com/alist-org/alist/v3/internal/setting"
79
"strconv"
810

911
"github.com/alist-org/alist/v3/drivers/thunder"
@@ -18,7 +20,7 @@ type Thunder struct {
1820
}
1921

2022
func (t *Thunder) Name() string {
21-
return "thunder"
23+
return "Thunder"
2224
}
2325

2426
func (t *Thunder) Items() []model.SettingItem {
@@ -35,13 +37,23 @@ func (t *Thunder) Init() (string, error) {
3537
}
3638

3739
func (t *Thunder) IsReady() bool {
40+
tempDir := setting.GetStr(conf.ThunderTempDir)
41+
if tempDir == "" {
42+
return false
43+
}
44+
storage, _, err := op.GetStorageAndActualPath(tempDir)
45+
if err != nil {
46+
return false
47+
}
48+
if _, ok := storage.(*thunder.Thunder); !ok {
49+
return false
50+
}
3851
return true
3952
}
4053

4154
func (t *Thunder) AddURL(args *tool.AddUrlArgs) (string, error) {
4255
// 添加新任务刷新缓存
4356
t.refreshTaskCache = true
44-
// args.TempDir 已经被修改为了 DstDirPath
4557
storage, actualPath, err := op.GetStorageAndActualPath(args.TempDir)
4658
if err != nil {
4759
return "", err
@@ -52,6 +64,11 @@ func (t *Thunder) AddURL(args *tool.AddUrlArgs) (string, error) {
5264
}
5365

5466
ctx := context.Background()
67+
68+
if err := op.MakeDir(ctx, storage, actualPath); err != nil {
69+
return "", err
70+
}
71+
5572
parentDir, err := op.GetUnwrap(ctx, storage, actualPath)
5673
if err != nil {
5774
return "", err
@@ -66,7 +83,7 @@ func (t *Thunder) AddURL(args *tool.AddUrlArgs) (string, error) {
6683
}
6784

6885
func (t *Thunder) Remove(task *tool.DownloadTask) error {
69-
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
86+
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
7087
if err != nil {
7188
return err
7289
}
@@ -83,7 +100,7 @@ func (t *Thunder) Remove(task *tool.DownloadTask) error {
83100
}
84101

85102
func (t *Thunder) Status(task *tool.DownloadTask) (*tool.Status, error) {
86-
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
103+
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
87104
if err != nil {
88105
return nil, err
89106
}

internal/offline_download/tool/add.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package tool
22

33
import (
44
"context"
5+
_115 "github.com/alist-org/alist/v3/drivers/115"
6+
"github.com/alist-org/alist/v3/drivers/pikpak"
7+
"github.com/alist-org/alist/v3/drivers/thunder"
58
"github.com/alist-org/alist/v3/internal/driver"
69
"github.com/alist-org/alist/v3/internal/model"
10+
"github.com/alist-org/alist/v3/internal/setting"
711
"github.com/alist-org/alist/v3/internal/task"
812
"net/url"
913
"path"
@@ -76,19 +80,26 @@ func AddURL(ctx context.Context, args *AddURLArgs) (task.TaskExtensionInfo, erro
7680
tempDir := filepath.Join(conf.Conf.TempDir, args.Tool, uid)
7781
deletePolicy := args.DeletePolicy
7882

83+
// 如果当前 storage 是对应网盘,则直接下载到目标路径,无需转存
7984
switch args.Tool {
8085
case "115 Cloud":
81-
tempDir = args.DstDirPath
82-
// 防止将下载好的文件删除
83-
deletePolicy = DeleteNever
84-
case "pikpak":
85-
tempDir = args.DstDirPath
86-
// 防止将下载好的文件删除
87-
deletePolicy = DeleteNever
88-
case "thunder":
89-
tempDir = args.DstDirPath
90-
// 防止将下载好的文件删除
91-
deletePolicy = DeleteNever
86+
if _, ok := storage.(*_115.Pan115); ok {
87+
tempDir = args.DstDirPath
88+
} else {
89+
tempDir = filepath.Join(setting.GetStr(conf.Pan115TempDir), uid)
90+
}
91+
case "PikPak":
92+
if _, ok := storage.(*pikpak.PikPak); ok {
93+
tempDir = args.DstDirPath
94+
} else {
95+
tempDir = filepath.Join(setting.GetStr(conf.PikPakTempDir), uid)
96+
}
97+
case "Thunder":
98+
if _, ok := storage.(*thunder.Thunder); ok {
99+
tempDir = args.DstDirPath
100+
} else {
101+
tempDir = filepath.Join(setting.GetStr(conf.ThunderTempDir), uid)
102+
}
92103
}
93104

94105
taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed

internal/offline_download/tool/all_test.go

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package tool
22

33
import (
4-
"io"
5-
"os"
6-
"time"
7-
84
"github.com/alist-org/alist/v3/internal/model"
95
)
106

@@ -40,28 +36,3 @@ type Tool interface {
4036
// Run for simple http download
4137
Run(task *DownloadTask) error
4238
}
43-
44-
type GetFileser interface {
45-
// GetFiles return the files of the download task, if nil, means walk the temp dir to get the files
46-
GetFiles(task *DownloadTask) []File
47-
}
48-
49-
type File struct {
50-
// ReadCloser for http client
51-
ReadCloser io.ReadCloser
52-
Name string
53-
Size int64
54-
Path string
55-
Modified time.Time
56-
}
57-
58-
func (f *File) GetReadCloser() (io.ReadCloser, error) {
59-
if f.ReadCloser != nil {
60-
return f.ReadCloser, nil
61-
}
62-
file, err := os.Open(f.Path)
63-
if err != nil {
64-
return nil, err
65-
}
66-
return file, nil
67-
}

0 commit comments

Comments
 (0)