Skip to content

Commit

Permalink
SongDownloader - Switch saveFile to use toBlob
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Jun 24, 2024
1 parent 9485f90 commit 6101a06
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion plugins/SongDownloader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
5 changes: 3 additions & 2 deletions plugins/SongDownloader/src/lib/saveFile.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toBlob } from "@inrixia/lib/fetch";
import type * as fs from "fs/promises";
const { writeFile } = <typeof fs>require("fs/promises");

Expand All @@ -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");
Expand Down
7 changes: 7 additions & 0 deletions plugins/_lib/fetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Blob>((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 = {}) =>
Expand Down

0 comments on commit 6101a06

Please sign in to comment.