-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add pikpak offline download function (#6648)
* add pikpak offline download function * 完善PikPak离线下载功能 * 删除多余的代码 * add task cache to avoid too many requests about API * 优化Status函数 * 完善所有功能,目前测试无BUG * 减少缓存时间,优化添加离线任务的参数
- Loading branch information
Showing
7 changed files
with
339 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package pikpak | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/alist-org/alist/v3/drivers/pikpak" | ||
"github.com/alist-org/alist/v3/internal/errs" | ||
"github.com/alist-org/alist/v3/internal/model" | ||
"github.com/alist-org/alist/v3/internal/offline_download/tool" | ||
"github.com/alist-org/alist/v3/internal/op" | ||
) | ||
|
||
type PikPak struct { | ||
refreshTaskCache bool | ||
} | ||
|
||
func (p *PikPak) Name() string { | ||
return "pikpak" | ||
} | ||
|
||
func (p *PikPak) Items() []model.SettingItem { | ||
return nil | ||
} | ||
|
||
func (p *PikPak) Run(task *tool.DownloadTask) error { | ||
return errs.NotSupport | ||
} | ||
|
||
func (p *PikPak) Init() (string, error) { | ||
p.refreshTaskCache = false | ||
return "ok", nil | ||
} | ||
|
||
func (p *PikPak) IsReady() bool { | ||
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 | ||
} | ||
pikpakDriver, ok := storage.(*pikpak.PikPak) | ||
if !ok { | ||
return "", fmt.Errorf("unsupported storage driver for offline download, only Pikpak is supported") | ||
} | ||
|
||
ctx := context.Background() | ||
parentDir, err := op.GetUnwrap(ctx, storage, actualPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
t, err := pikpakDriver.OfflineDownload(ctx, args.Url, parentDir, "") | ||
if err != nil { | ||
return "", fmt.Errorf("failed to add offline download task: %w", err) | ||
} | ||
|
||
return t.ID, nil | ||
} | ||
|
||
func (p *PikPak) Remove(task *tool.DownloadTask) error { | ||
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath) | ||
if err != nil { | ||
return err | ||
} | ||
pikpakDriver, ok := storage.(*pikpak.PikPak) | ||
if !ok { | ||
return fmt.Errorf("unsupported storage driver for offline download, only Pikpak is supported") | ||
} | ||
ctx := context.Background() | ||
err = pikpakDriver.DeleteOfflineTasks(ctx, []string{task.GID}, false) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (p *PikPak) Status(task *tool.DownloadTask) (*tool.Status, error) { | ||
storage, _, err := op.GetStorageAndActualPath(task.DstDirPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pikpakDriver, ok := storage.(*pikpak.PikPak) | ||
if !ok { | ||
return nil, fmt.Errorf("unsupported storage driver for offline download, only Pikpak is supported") | ||
} | ||
tasks, err := p.GetTasks(pikpakDriver) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s := &tool.Status{ | ||
Progress: 0, | ||
NewGID: "", | ||
Completed: false, | ||
Status: "the task has been deleted", | ||
Err: nil, | ||
} | ||
for _, t := range tasks { | ||
if t.ID == task.GID { | ||
s.Progress = float64(t.Progress) | ||
s.Status = t.Message | ||
s.Completed = (t.Phase == "PHASE_TYPE_COMPLETE") | ||
if t.Phase == "PHASE_TYPE_ERROR" { | ||
s.Err = fmt.Errorf(t.Message) | ||
} | ||
return s, nil | ||
} | ||
} | ||
s.Err = fmt.Errorf("the task has been deleted") | ||
return s, nil | ||
} | ||
|
||
func init() { | ||
tool.Tools.Add(&PikPak{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package pikpak | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/Xhofe/go-cache" | ||
"github.com/alist-org/alist/v3/drivers/pikpak" | ||
"github.com/alist-org/alist/v3/internal/op" | ||
"github.com/alist-org/alist/v3/pkg/singleflight" | ||
) | ||
|
||
var taskCache = cache.NewMemCache(cache.WithShards[[]pikpak.OfflineTask](16)) | ||
var taskG singleflight.Group[[]pikpak.OfflineTask] | ||
|
||
func (p *PikPak) GetTasks(pikpakDriver *pikpak.PikPak) ([]pikpak.OfflineTask, error) { | ||
key := op.Key(pikpakDriver, "/drive/v1/task") | ||
if !p.refreshTaskCache { | ||
if tasks, ok := taskCache.Get(key); ok { | ||
return tasks, nil | ||
} | ||
} | ||
p.refreshTaskCache = false | ||
tasks, err, _ := taskG.Do(key, func() ([]pikpak.OfflineTask, error) { | ||
ctx := context.Background() | ||
phase := []string{"PHASE_TYPE_RUNNING", "PHASE_TYPE_ERROR", "PHASE_TYPE_PENDING", "PHASE_TYPE_COMPLETE"} | ||
tasks, err := pikpakDriver.OfflineList(ctx, "", phase) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// 添加缓存 10s | ||
if len(tasks) > 0 { | ||
taskCache.Set(key, tasks, cache.WithEx[[]pikpak.OfflineTask](time.Second*10)) | ||
} else { | ||
taskCache.Del(key) | ||
} | ||
return tasks, nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tasks, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.