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(离线下载):允许在任何存储中使用离线下载工具” #8052

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
9 changes: 0 additions & 9 deletions internal/conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ 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"
Expand Down
23 changes: 3 additions & 20 deletions internal/offline_download/115/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ 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"
Expand Down Expand Up @@ -35,23 +33,13 @@ 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
Expand All @@ -62,11 +50,6 @@ 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
Expand All @@ -81,7 +64,7 @@ func (p *Cloud115) AddURL(args *tool.AddUrlArgs) (string, error) {
}

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

func (p *Cloud115) Status(task *tool.DownloadTask) (*tool.Status, error) {
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
if err != nil {
return nil, err
}
Expand Down
25 changes: 4 additions & 21 deletions internal/offline_download/pikpak/pikpak.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ 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"
Expand All @@ -19,7 +17,7 @@ type PikPak struct {
}

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

func (p *PikPak) Items() []model.SettingItem {
Expand All @@ -36,23 +34,13 @@ 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
Expand All @@ -63,11 +51,6 @@ 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
Expand All @@ -82,7 +65,7 @@ func (p *PikPak) AddURL(args *tool.AddUrlArgs) (string, error) {
}

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

func (p *PikPak) Status(task *tool.DownloadTask) (*tool.Status, error) {
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
if err != nil {
return nil, err
}
Expand Down
25 changes: 4 additions & 21 deletions internal/offline_download/thunder/thunder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ 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"
Expand All @@ -20,7 +18,7 @@ type Thunder struct {
}

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

func (t *Thunder) Items() []model.SettingItem {
Expand All @@ -37,23 +35,13 @@ 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
Expand All @@ -64,11 +52,6 @@ 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
Expand All @@ -83,7 +66,7 @@ func (t *Thunder) AddURL(args *tool.AddUrlArgs) (string, error) {
}

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

func (t *Thunder) Status(task *tool.DownloadTask) (*tool.Status, error) {
storage, _, err := op.GetStorageAndActualPath(task.TempDir)
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath)
if err != nil {
return nil, err
}
Expand Down
33 changes: 11 additions & 22 deletions internal/offline_download/tool/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ 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"
Expand Down Expand Up @@ -80,26 +76,19 @@ 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":
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)
}
tempDir = args.DstDirPath
// 防止将下载好的文件删除
deletePolicy = DeleteNever
case "pikpak":
tempDir = args.DstDirPath
// 防止将下载好的文件删除
deletePolicy = DeleteNever
case "thunder":
tempDir = args.DstDirPath
// 防止将下载好的文件删除
deletePolicy = DeleteNever
}

taskCreator, _ := ctx.Value("user").(*model.User) // taskCreator is nil when convert failed
Expand Down
17 changes: 17 additions & 0 deletions internal/offline_download/tool/all_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tool_test

import (
"testing"

"github.com/alist-org/alist/v3/internal/offline_download/tool"
)

func TestGetFiles(t *testing.T) {
files, err := tool.GetFiles("..")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
t.Log(file.Name, file.Size, file.Path, file.Modified)
}
}
29 changes: 29 additions & 0 deletions internal/offline_download/tool/base.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package tool

import (
"io"
"os"
"time"

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

Expand Down Expand Up @@ -36,3 +40,28 @@ 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