Skip to content

Commit

Permalink
feat(offline-download): support for Thunder Expert driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Lanfei committed Jan 25, 2025
1 parent 11b6a60 commit 4532b42
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 47 deletions.
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

0 comments on commit 4532b42

Please sign in to comment.