Create 创建任务,并将任务加入全局任务列表
()
| 101 | |
| 102 | // Create 创建任务,并将任务加入全局任务列表 |
| 103 | func (task *Task) Start() { |
| 104 | if task.DownloadType == "" { |
| 105 | task.DownloadType = "merge" |
| 106 | } |
| 107 | GlobalTaskMux.Lock() |
| 108 | GlobalTaskList = append(GlobalTaskList, task) |
| 109 | GlobalTaskMux.Unlock() |
| 110 | db := util.MustGetDB() |
| 111 | defer db.Close() |
| 112 | sessdata, err := bilibili.GetSessdata(db) |
| 113 | if err != nil { |
| 114 | task.UpdateStatus(db, "error", fmt.Errorf("bilibili.GetSessdata: %v", err)) |
| 115 | return |
| 116 | } |
| 117 | client := &bilibili.BiliClient{SESSDATA: sessdata} |
| 118 | |
| 119 | GlobalDownloadSem.Acquire() |
| 120 | task.UpdateStatus(db, "running") |
| 121 | |
| 122 | if task.DownloadType == "audio" { |
| 123 | // 仅音频模式:只下载音频,重命名音频文件为输出文件 |
| 124 | err = DownloadMedia(client, task.Audio, task, "audio") |
| 125 | if err != nil { |
| 126 | GlobalDownloadSem.Release() |
| 127 | task.UpdateStatus(db, "error", fmt.Errorf("DownloadMedia: %v", err)) |
| 128 | return |
| 129 | } |
| 130 | GlobalDownloadSem.Release() |
| 131 | outputPath := task.TaskInDB.FilePath() |
| 132 | audioPath := filepath.Join(task.Folder, strconv.FormatInt(task.ID, 10)+".audio") |
| 133 | err = os.Rename(audioPath, outputPath) |
| 134 | if err != nil { |
| 135 | task.UpdateStatus(db, "error", fmt.Errorf("os.Rename: %v", err)) |
| 136 | return |
| 137 | } |
| 138 | // 添加元数据 |
| 139 | if err := task.addMetadata(outputPath); err != nil { |
| 140 | log.Printf("添加元数据失败 (任务ID: %d): %v", task.ID, err) |
| 141 | } |
| 142 | task.UpdateStatus(db, "done") |
| 143 | return |
| 144 | } else if task.DownloadType == "video" { |
| 145 | // 仅视频模式:只下载视频,重命名视频文件为输出文件 |
| 146 | err = DownloadMedia(client, task.Video, task, "video") |
| 147 | if err != nil { |
| 148 | GlobalDownloadSem.Release() |
| 149 | task.UpdateStatus(db, "error", fmt.Errorf("DownloadMedia: %v", err)) |
| 150 | return |
| 151 | } |
| 152 | GlobalDownloadSem.Release() |
| 153 | outputPath := task.TaskInDB.FilePath() |
| 154 | videoPath := filepath.Join(task.Folder, strconv.FormatInt(task.ID, 10)+".video") |
| 155 | err = os.Rename(videoPath, outputPath) |
| 156 | if err != nil { |
| 157 | task.UpdateStatus(db, "error", fmt.Errorf("os.Rename: %v", err)) |
| 158 | return |
| 159 | } |
| 160 | // 添加元数据 |