(url: string, destPath: string)
| 1207 | .then(resolve) |
| 1208 | .catch(() => resolve(false)); |
| 1209 | return; |
| 1210 | } |
| 1211 | const chunks: Buffer[] = []; |
| 1212 | res.on("data", (c: Buffer) => chunks.push(c)); |
| 1213 | res.on("end", async () => { |
| 1214 | try { |
| 1215 | const buf = Buffer.concat(chunks); |
| 1216 | if (!buf || buf.length === 0) return resolve(false); |
| 1217 | await fs.promises.writeFile(destPath, buf); |
| 1218 | resolve(true); |
| 1219 | } catch { |
| 1220 | resolve(false); |
| 1221 | } |
| 1222 | }); |
| 1223 | }); |
| 1224 | req.on("error", () => resolve(false)); |
| 1225 | req.end(); |
| 1226 | } catch { |
| 1227 | resolve(false); |
| 1228 | } |
| 1229 | }); |
| 1230 | } |
| 1231 | |
| 1232 | async download( |
| 1233 | url: string, |
| 1234 | metadata?: SongInfo |
| 1235 | ): Promise<{ audioPath: string | null; thumbnailPath?: string }> { |
| 1236 | try { |
| 1237 | const filename = metadata |
| 1238 | ? `${Utils.sanitizeFilename(metadata.artist)}_${Utils.sanitizeFilename( |
| 1239 | metadata.title |
| 1240 | )}` |
| 1241 | : `download_${Date.now()}`; |
| 1242 | |
| 1243 | // 每次下载到临时目录,确保全新下载 |
| 1244 | const timestamp = Date.now(); |
no test coverage detected