mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-15 04:43:41 +00:00
feat: implement task patching functionality (#1267)
This commit is contained in:
22 files changed
+1281
-132
No files matched your search
@@ -480,6 +480,47 @@ func (d *Downloader) Create(rrId string) (taskId string, err error) {
|
||||
return d.doCreate(fetcher, nil)
|
||||
}
|
||||
|
||||
// Patch modifies task-specific data based on the protocol.
|
||||
// For HTTP protocol, it can modify Request info.
|
||||
// For BT protocol, it can modify SelectFiles.
|
||||
func (d *Downloader) Patch(id string, req *base.Request, opts *base.Options) error {
|
||||
task := d.GetTask(id)
|
||||
if task == nil {
|
||||
return ErrTaskNotFound
|
||||
}
|
||||
|
||||
// Restore fetcher if not loaded
|
||||
if task.fetcher == nil {
|
||||
err := func() error {
|
||||
task.statusLock.Lock()
|
||||
defer task.statusLock.Unlock()
|
||||
|
||||
return d.restoreFetcher(task)
|
||||
}()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Call the fetcher's Patch method
|
||||
if err := task.fetcher.Patch(req, opts); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Update task meta from fetcher
|
||||
task.Meta = task.fetcher.Meta()
|
||||
|
||||
// Save task to storage
|
||||
if err := d.saveTask(task); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Emit progress event to notify listeners
|
||||
d.emit(EventKeyProgress, task)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Downloader) Pause(filter *TaskFilter) (err error) {
|
||||
if filter == nil || filter.IsEmpty() {
|
||||
return d.pauseAll()
|
||||
|
||||
@@ -2648,3 +2648,87 @@ func TestDownloader_AutoTorrentDisabled(t *testing.T) {
|
||||
t.Error("Torrent file should have been downloaded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloader_PatchTask_HTTP(t *testing.T) {
|
||||
listener := test.StartTestFileServer()
|
||||
defer listener.Close()
|
||||
|
||||
downloader := NewDownloader(nil)
|
||||
if err := downloader.Setup(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer downloader.Clear()
|
||||
|
||||
req := &base.Request{
|
||||
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
||||
Extra: &http.OptsExtra{
|
||||
Connections: 2,
|
||||
},
|
||||
Labels: map[string]string{
|
||||
"test": "value1",
|
||||
},
|
||||
}
|
||||
opts := &base.Options{
|
||||
Path: test.Dir,
|
||||
Name: test.DownloadName,
|
||||
}
|
||||
|
||||
// Create task but don't start it yet
|
||||
taskId, err := downloader.CreateDirect(req, opts)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Pause the task immediately
|
||||
if err := downloader.Pause(&TaskFilter{IDs: []string{taskId}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Patch the task with new labels
|
||||
patchReq := &base.Request{
|
||||
Labels: map[string]string{
|
||||
"test": "value2",
|
||||
"newKey": "newValue",
|
||||
},
|
||||
}
|
||||
|
||||
if err := downloader.Patch(taskId, patchReq, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Verify the patch was applied
|
||||
task := downloader.GetTask(taskId)
|
||||
if task == nil {
|
||||
t.Fatal("task not found")
|
||||
}
|
||||
|
||||
if task.Meta.Req.Labels["test"] != "value2" {
|
||||
t.Errorf("PatchTask() label 'test' = %v, want %v", task.Meta.Req.Labels["test"], "value2")
|
||||
}
|
||||
if task.Meta.Req.Labels["newKey"] != "newValue" {
|
||||
t.Errorf("PatchTask() label 'newKey' = %v, want %v", task.Meta.Req.Labels["newKey"], "newValue")
|
||||
}
|
||||
|
||||
// Clean up
|
||||
downloader.Delete(&TaskFilter{IDs: []string{taskId}}, true)
|
||||
}
|
||||
|
||||
func TestDownloader_PatchTask_NotFound(t *testing.T) {
|
||||
downloader := NewDownloader(nil)
|
||||
if err := downloader.Setup(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer downloader.Clear()
|
||||
|
||||
// Try to patch a non-existent task
|
||||
patchReq := &base.Request{
|
||||
Labels: map[string]string{
|
||||
"test": "value",
|
||||
},
|
||||
}
|
||||
|
||||
err := downloader.Patch("non-existent-id", patchReq, nil)
|
||||
if err != ErrTaskNotFound {
|
||||
t.Errorf("Patch() error = %v, want %v", err, ErrTaskNotFound)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user