Skip to content

Commit

Permalink
feat(offline-download): allow using offline download tools in any sto…
Browse files Browse the repository at this point in the history
…rage (#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>
  • Loading branch information
Lanfei and xhofe authored Jan 10, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent e04114d commit b60da97
Showing 14 changed files with 478 additions and 204 deletions.
9 changes: 9 additions & 0 deletions internal/conf/const.go
Original file line number Diff line number Diff line change
@@ -58,6 +58,15 @@ const (
TransmissionUri = "transmission_uri"
TransmissionSeedtime = "transmission_seedtime"

// 115
Pan115TempDir = "115_temp_dir"

// pikpak
PikPakTempDir = "pikpak_temp_dir"

// thunder
ThunderTempDir = "thunder_temp_dir"

// single
Token = "token"
IndexProgress = "index_progress"
23 changes: 20 additions & 3 deletions internal/offline_download/115/client.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ package _115
import (
"context"
"fmt"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/setting"

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

func (p *Cloud115) IsReady() bool {
tempDir := setting.GetStr(conf.Pan115TempDir)
if tempDir == "" {
return false
}
storage, _, err := op.GetStorageAndActualPath(tempDir)
if err != nil {
return false
}
if _, ok := storage.(*_115.Pan115); !ok {
return false
}
return true
}

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

ctx := context.Background()

if err := op.MakeDir(ctx, storage, actualPath); err != nil {
return "", err
}

parentDir, err := op.GetUnwrap(ctx, storage, actualPath)
if err != nil {
return "", err
@@ -64,7 +81,7 @@ func (p *Cloud115) AddURL(args *tool.AddUrlArgs) (string, error) {
}

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

func (p *Cloud115) Status(task *tool.DownloadTask) (*tool.Status, error) {
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
if err != nil {
return nil, err
}
25 changes: 21 additions & 4 deletions internal/offline_download/pikpak/pikpak.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ package pikpak
import (
"context"
"fmt"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/setting"
"strconv"

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

func (p *PikPak) Name() string {
return "pikpak"
return "PikPak"
}

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

func (p *PikPak) IsReady() bool {
tempDir := setting.GetStr(conf.PikPakTempDir)
if tempDir == "" {
return false
}
storage, _, err := op.GetStorageAndActualPath(tempDir)
if err != nil {
return false
}
if _, ok := storage.(*pikpak.PikPak); !ok {
return false
}
return true
}

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

ctx := context.Background()

if err := op.MakeDir(ctx, storage, actualPath); err != nil {
return "", err
}

parentDir, err := op.GetUnwrap(ctx, storage, actualPath)
if err != nil {
return "", err
@@ -65,7 +82,7 @@ func (p *PikPak) AddURL(args *tool.AddUrlArgs) (string, error) {
}

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

func (p *PikPak) Status(task *tool.DownloadTask) (*tool.Status, error) {
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
if err != nil {
return nil, err
}
25 changes: 21 additions & 4 deletions internal/offline_download/thunder/thunder.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/setting"
"strconv"

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

func (t *Thunder) Name() string {
return "thunder"
return "Thunder"
}

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

func (t *Thunder) IsReady() bool {
tempDir := setting.GetStr(conf.ThunderTempDir)
if tempDir == "" {
return false
}
storage, _, err := op.GetStorageAndActualPath(tempDir)
if err != nil {
return false
}
if _, ok := storage.(*thunder.Thunder); !ok {
return false
}
return true
}

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

ctx := context.Background()

if err := op.MakeDir(ctx, storage, actualPath); err != nil {
return "", err
}

parentDir, err := op.GetUnwrap(ctx, storage, actualPath)
if err != nil {
return "", err
@@ -66,7 +83,7 @@ func (t *Thunder) AddURL(args *tool.AddUrlArgs) (string, error) {
}

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

func (t *Thunder) Status(task *tool.DownloadTask) (*tool.Status, error) {
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
if err != nil {
return nil, err
}
33 changes: 22 additions & 11 deletions internal/offline_download/tool/add.go
Original file line number Diff line number Diff line change
@@ -2,8 +2,12 @@ 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"
"github.com/alist-org/alist/v3/internal/task"
"net/url"
"path"
@@ -76,19 +80,26 @@ func AddURL(ctx context.Context, args *AddURLArgs) (task.TaskExtensionInfo, erro
tempDir := filepath.Join(conf.Conf.TempDir, args.Tool, uid)
deletePolicy := args.DeletePolicy

// 如果当前 storage 是对应网盘,则直接下载到目标路径,无需转存
switch args.Tool {
case "115 Cloud":
tempDir = args.DstDirPath
// 防止将下载好的文件删除
deletePolicy = DeleteNever
case "pikpak":
tempDir = args.DstDirPath
// 防止将下载好的文件删除
deletePolicy = DeleteNever
case "thunder":
tempDir = args.DstDirPath
// 防止将下载好的文件删除
deletePolicy = DeleteNever
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 {
tempDir = args.DstDirPath
} else {
tempDir = filepath.Join(setting.GetStr(conf.ThunderTempDir), uid)
}
}

taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed
17 changes: 0 additions & 17 deletions internal/offline_download/tool/all_test.go

This file was deleted.

29 changes: 0 additions & 29 deletions internal/offline_download/tool/base.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package tool

import (
"io"
"os"
"time"

"github.com/alist-org/alist/v3/internal/model"
)

@@ -40,28 +36,3 @@ type Tool interface {
// Run for simple http download
Run(task *DownloadTask) error
}

type GetFileser interface {
// GetFiles return the files of the download task, if nil, means walk the temp dir to get the files
GetFiles(task *DownloadTask) []File
}

type File struct {
// ReadCloser for http client
ReadCloser io.ReadCloser
Name string
Size int64
Path string
Modified time.Time
}

func (f *File) GetReadCloser() (io.ReadCloser, error) {
if f.ReadCloser != nil {
return f.ReadCloser, nil
}
file, err := os.Open(f.Path)
if err != nil {
return nil, err
}
return file, nil
}
Loading

0 comments on commit b60da97

Please sign in to comment.