createAria2Task 创建 Aria2 任务
(taskID string, createdAt time.Time, req *CreateTaskRequest, stor storage.Storage)
| 139 | |
| 140 | // createAria2Task 创建 Aria2 任务 |
| 141 | func (f *TaskFactory) createAria2Task(taskID string, createdAt time.Time, req *CreateTaskRequest, stor storage.Storage) (*CreateTaskResponse, error) { |
| 142 | var params Aria2Params |
| 143 | if err := json.Unmarshal(req.Params, ¶ms); err != nil { |
| 144 | return nil, fmt.Errorf("invalid params: %w", err) |
| 145 | } |
| 146 | |
| 147 | if len(params.URLs) == 0 { |
| 148 | return nil, fmt.Errorf("no URLs provided") |
| 149 | } |
| 150 | |
| 151 | // 检查 Aria2 是否启用 |
| 152 | cfg := config.C().Aria2 |
| 153 | if !cfg.Enable { |
| 154 | return nil, fmt.Errorf("aria2 is not enabled") |
| 155 | } |
| 156 | |
| 157 | aria2Client, err := aria2.NewClient(cfg.Url, cfg.Secret) |
| 158 | if err != nil { |
| 159 | return nil, fmt.Errorf("failed to create aria2 client: %w", err) |
| 160 | } |
| 161 | |
| 162 | // 添加下载任务到 Aria2 |
| 163 | gid, err := aria2Client.AddURI(f.ctx, params.URLs, nil) |
| 164 | if err != nil { |
| 165 | return nil, fmt.Errorf("failed to add aria2 task: %w", err) |
| 166 | } |
| 167 | |
| 168 | task := aria2dl.NewTask(taskID, f.ctx, gid, params.URLs, aria2Client, stor, req.Path, nil) |
| 169 | |
| 170 | err = f.registerAndEnqueueTask(task, tasktype.TaskTypeAria2, req.Storage, req.Path, req.Webhook) |
| 171 | if err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | |
| 175 | return &CreateTaskResponse{ |
| 176 | TaskID: taskID, |
| 177 | Type: tasktype.TaskTypeAria2, |
| 178 | Status: TaskStatusQueued, |
| 179 | CreatedAt: createdAt, |
| 180 | }, nil |
| 181 | } |
| 182 | |
| 183 | // createParsedTask 创建解析任务 |
| 184 | func (f *TaskFactory) createParsedTask(taskID string, createdAt time.Time, req *CreateTaskRequest, stor storage.Storage) (*CreateTaskResponse, error) { |
no test coverage detected