From 6101a06364140bb22712f32ac6293c5a35d4585b Mon Sep 17 00:00:00 2001 From: Inrixia Date: Tue, 25 Jun 2024 05:36:52 +1200 Subject: [PATCH] SongDownloader - Switch saveFile to use toBlob --- plugins/SongDownloader/src/index.ts | 2 +- plugins/SongDownloader/src/lib/saveFile.ts | 5 +++-- plugins/_lib/fetch/index.ts | 7 +++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/plugins/SongDownloader/src/index.ts b/plugins/SongDownloader/src/index.ts index e6f837c5..6b6b18f3 100644 --- a/plugins/SongDownloader/src/index.ts +++ b/plugins/SongDownloader/src/index.ts @@ -142,5 +142,5 @@ export const downloadTrack = async (track: TrackItem, trackOptions: TrackOptions const fileName = fileNameFromInfo(track, trackInfo); if (settings.defaultDownloadPath !== "") return saveFileNode(streamWithTags ?? trackInfo.stream, settings.defaultDownloadPath, fileName); - return saveFile(new Blob([(await streamWithTags?.toBuffer()) ?? (await toBuffer(trackInfo.stream))], { type: "application/octet-stream" }), fileName); + return saveFile(streamWithTags ?? trackInfo.stream, fileName); }; diff --git a/plugins/SongDownloader/src/lib/saveFile.ts b/plugins/SongDownloader/src/lib/saveFile.ts index cde0293c..d39e8340 100644 --- a/plugins/SongDownloader/src/lib/saveFile.ts +++ b/plugins/SongDownloader/src/lib/saveFile.ts @@ -1,3 +1,4 @@ +import { toBlob } from "@inrixia/lib/fetch"; import type * as fs from "fs/promises"; const { writeFile } = require("fs/promises"); @@ -10,9 +11,9 @@ export const saveFileNode = (stream: Readable, path: string, fileName: string) = return writeFile(`${path}/${sanitizeFilename(fileName)}`, stream); }; -export const saveFile = async (blob: Blob, fileName: string) => { +export const saveFile = async (blob: Readable, fileName: string) => { // Create a new Object URL for the Blob - const objectUrl = URL.createObjectURL(blob); + const objectUrl = URL.createObjectURL(await toBlob(blob)); // Create a link element const a = document.createElement("a"); diff --git a/plugins/_lib/fetch/index.ts b/plugins/_lib/fetch/index.ts index c1d2fe0e..aa5a0004 100644 --- a/plugins/_lib/fetch/index.ts +++ b/plugins/_lib/fetch/index.ts @@ -42,6 +42,13 @@ export const toBuffer = (stream: Readable) => stream.on("end", () => resolve(Buffer.concat(chunks))); stream.on("error", reject); }); +export const toBlob = (stream: Readable) => + new Promise((resolve, reject) => { + const chunks: Buffer[] = []; + stream.on("data", (chunk) => chunks.push(chunk)); + stream.on("end", () => resolve(new Blob(chunks))); + stream.on("error", reject); + }); export type ExtendedRequestOptions = RequestOptions & { body?: string; poke?: true }; export const requestStream = (url: string, options: ExtendedRequestOptions = {}) =>