Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(offline-download): support for Thunder Expert driver #7876

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions internal/offline_download/thunder/thunder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ func (t *Thunder) IsReady() bool {
if err != nil {
return false
}
if _, ok := storage.(*thunder.Thunder); !ok {
return false
if _, ok := storage.(*thunder.Thunder); ok {
return true
}
if _, ok := storage.(*thunder.ThunderExpert); ok {
return true
}
return true
return false
}

func (t *Thunder) AddURL(args *tool.AddUrlArgs) (string, error) {
Expand All @@ -58,11 +61,6 @@ func (t *Thunder) AddURL(args *tool.AddUrlArgs) (string, error) {
if err != nil {
return "", err
}
thunderDriver, ok := storage.(*thunder.Thunder)
if !ok {
return "", fmt.Errorf("unsupported storage driver for offline download, only Thunder is supported")
}

ctx := context.Background()

if err := op.MakeDir(ctx, storage, actualPath); err != nil {
Expand All @@ -74,7 +72,15 @@ func (t *Thunder) AddURL(args *tool.AddUrlArgs) (string, error) {
return "", err
}

task, err := thunderDriver.OfflineDownload(ctx, args.Url, parentDir, "")
var task *thunder.OfflineTask
if thunderDriver, ok := storage.(*thunder.Thunder); ok {
task, err = thunderDriver.OfflineDownload(ctx, args.Url, parentDir, "")
} else if expertDriver, ok := storage.(*thunder.ThunderExpert); ok {
task, err = expertDriver.OfflineDownload(ctx, args.Url, parentDir, "")
} else {
return "", fmt.Errorf("unsupported storage driver for offline download, only Thunder or Thunder Expert is supported")
}

if err != nil {
return "", fmt.Errorf("failed to add offline download task: %w", err)
}
Expand All @@ -87,28 +93,29 @@ func (t *Thunder) Remove(task *tool.DownloadTask) error {
if err != nil {
return err
}
thunderDriver, ok := storage.(*thunder.Thunder)
if !ok {
return fmt.Errorf("unsupported storage driver for offline download, only Thunder is supported")
if thunderDriver, ok := storage.(*thunder.Thunder); ok {
err = thunderDriver.DeleteOfflineTasks(context.Background(), []string{task.GID}, false)
} else if expertDriver, ok := storage.(*thunder.ThunderExpert); ok {
err = expertDriver.DeleteOfflineTasks(context.Background(), []string{task.GID}, false)
} else {
return fmt.Errorf("unsupported storage driver for offline download, only Thunder or Thunder Expert is supported")
}
ctx := context.Background()
err = thunderDriver.DeleteOfflineTasks(ctx, []string{task.GID}, false)
if err != nil {
return err
}
return nil
return err
}

func (t *Thunder) Status(task *tool.DownloadTask) (*tool.Status, error) {
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
if err != nil {
return nil, err
}
thunderDriver, ok := storage.(*thunder.Thunder)
if !ok {
return nil, fmt.Errorf("unsupported storage driver for offline download, only Thunder is supported")
var tasks []thunder.OfflineTask
if thunderDriver, ok := storage.(*thunder.Thunder); ok {
tasks, err = t.GetTasks(thunderDriver)
} else if expertDriver, ok := storage.(*thunder.ThunderExpert); ok {
tasks, err = t.GetTasks(expertDriver)
} else {
return nil, fmt.Errorf("unsupported storage driver for offline download, only Thunder or Thunder Expert is supported")
}
tasks, err := t.GetTasks(thunderDriver)
if err != nil {
return nil, err
}
Expand Down
14 changes: 10 additions & 4 deletions internal/offline_download/thunder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package thunder

import (
"context"
"github.com/alist-org/alist/v3/internal/driver"
"time"

"github.com/Xhofe/go-cache"
Expand All @@ -13,17 +14,22 @@ import (
var taskCache = cache.NewMemCache(cache.WithShards[[]thunder.OfflineTask](16))
var taskG singleflight.Group[[]thunder.OfflineTask]

func (t *Thunder) GetTasks(thunderDriver *thunder.Thunder) ([]thunder.OfflineTask, error) {
key := op.Key(thunderDriver, "/drive/v1/task")
func (t *Thunder) GetTasks(storage driver.Driver) ([]thunder.OfflineTask, error) {
key := op.Key(storage, "/drive/v1/task")
if !t.refreshTaskCache {
if tasks, ok := taskCache.Get(key); ok {
return tasks, nil
}
}
t.refreshTaskCache = false
tasks, err, _ := taskG.Do(key, func() ([]thunder.OfflineTask, error) {
ctx := context.Background()
tasks, err := thunderDriver.OfflineList(ctx, "")
var tasks []thunder.OfflineTask
var err error
if thunderDriver, ok := storage.(*thunder.Thunder); ok {
tasks, err = thunderDriver.OfflineList(context.Background(), "")
} else if expertDriver, ok := storage.(*thunder.ThunderExpert); ok {
tasks, err = expertDriver.OfflineList(context.Background(), "")
}
if err != nil {
return nil, err
}
Expand Down
29 changes: 10 additions & 19 deletions internal/offline_download/tool/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package tool

import (
"context"
_115 "github.com/alist-org/alist/v3/drivers/115"
"github.com/alist-org/alist/v3/drivers/pikpak"
"github.com/alist-org/alist/v3/drivers/thunder"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/setting"
Expand Down Expand Up @@ -81,24 +78,18 @@ func AddURL(ctx context.Context, args *AddURLArgs) (task.TaskExtensionInfo, erro
deletePolicy := args.DeletePolicy

// 如果当前 storage 是对应网盘,则直接下载到目标路径,无需转存
switch args.Tool {
case "115 Cloud":
if _, ok := storage.(*_115.Pan115); ok {
tempDir = args.DstDirPath
} else {
tempDir = filepath.Join(setting.GetStr(conf.Pan115TempDir), uid)
}
case "PikPak":
if _, ok := storage.(*pikpak.PikPak); ok {
tempDir = args.DstDirPath
} else {
tempDir = filepath.Join(setting.GetStr(conf.PikPakTempDir), uid)
}
case "Thunder":
if _, ok := storage.(*thunder.Thunder); ok {
tempDirConfMap := map[string]string{
"115 Cloud": conf.Pan115TempDir,
"PikPak": conf.PikPakTempDir,
"Thunder": conf.ThunderTempDir,
}
if tempDirConf, ok := tempDirConfMap[args.Tool]; ok {
tempDir = setting.GetStr(tempDirConf)
tempDirStorage, _, _ := op.GetStorageAndActualPath(tempDir)
if storage == tempDirStorage {
tempDir = args.DstDirPath
} else {
tempDir = filepath.Join(setting.GetStr(conf.ThunderTempDir), uid)
tempDir = filepath.Join(tempDir, uid)
}
}

Expand Down
6 changes: 4 additions & 2 deletions server/handles/offline_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ func SetThunder(c *gin.Context) {
return
}
if _, ok := storage.(*thunder.Thunder); !ok {
common.ErrorStrResp(c, "unsupported storage driver for offline download, only Thunder is supported", 400)
return
if _, ok := storage.(*thunder.ThunderExpert); !ok {
common.ErrorStrResp(c, "unsupported storage driver for offline download, only Thunder or Thunder Expert is supported", 400)
return
}
}
}
items := []model.SettingItem{
Expand Down
Loading