Skip to content

Commit

Permalink
SongDownloader - Working native download progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Jun 25, 2024
1 parent 19887c8 commit 86480c1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
14 changes: 10 additions & 4 deletions plugins/SongDownloader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import safeUnload from "@inrixia/lib/safeUnload";
import { settings } from "./Settings";
import { ContextMenu } from "@inrixia/lib/ContextMenu";
import { PlaybackInfoCache } from "@inrixia/lib/Caches/PlaybackInfoCache";
import { downloadTrackStream, openDialog, saveDialog } from "@inrixia/lib/nativeBridge";
import { startTrackDownload, openDialog, saveDialog, getDownloadProgress } from "@inrixia/lib/nativeBridge";
export { Settings } from "./Settings";

type DownloadButtoms = Record<string, HTMLButtonElement>;
Expand Down Expand Up @@ -104,8 +104,14 @@ const downloadTrack = async (track: TrackItem, updateMethods: ButtonMethods, fil
const dialogResult = await saveDialog({ defaultPath, filters: [{ name: "", extensions: [parseExtension(fileName) ?? "*"] }] });
filePath = dialogResult?.filePath;
}
console.log(filePath);
updateMethods.set("Downloading...");
await downloadTrackStream(playbackInfo, filePath);
console.log("Done!");
let downloadEnded = false;
const downloadComplete = startTrackDownload(playbackInfo, filePath).finally(() => (downloadEnded = true));
const updateDownloadProgress = async () => {
const downloadProgress = await getDownloadProgress(filePath);
if (downloadProgress !== undefined) updateMethods.onProgress(downloadProgress);
if (!downloadEnded) setTimeout(updateDownloadProgress, 100);
};
updateDownloadProgress();
return downloadComplete;
};
3 changes: 2 additions & 1 deletion plugins/_lib/nativeBridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const parseDasha = invoke("parseDasha");
export const requestJson = invoke("requestJson");
export const hash = invoke("hash");
export const voidTrack = invoke("voidTrack");
export const downloadTrackStream = invoke("downloadTrackStream");
export const startTrackDownload = invoke("startTrackDownload");
export const saveDialog = invoke("saveDialog");
export const openDialog = invoke("openDialog");
export const getDownloadProgress = invoke("getDownloadProgress");
25 changes: 21 additions & 4 deletions plugins/_lib/nativeBridge/native/downloadTrack.native.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import { requestTrackStream } from "./request/requestTrack.native";
import type { ExtendedPlayackInfo } from "../../Caches/PlaybackInfoTypes";
import type { DownloadProgress } from "./request/helpers.native";
import { requestTrackStream } from "./request/requestTrack.native";
import { createWriteStream } from "fs";

export const downloadTrackStream = async (extPlaybackInfo: ExtendedPlayackInfo, filePath: string): Promise<void> => {
const stream = await requestTrackStream(extPlaybackInfo);
return new Promise((res) => stream.pipe(createWriteStream(filePath)).on("finish", res));
export type { DownloadProgress } from "./request/helpers.native";

const downloadStatus: Record<string, DownloadProgress> = {};

export const startTrackDownload = async (extPlaybackInfo: ExtendedPlayackInfo, filePath: string): Promise<void> => {
if (downloadStatus[filePath] !== undefined) throw new Error(`Something is already downloading to ${filePath}`);
try {
const stream = await requestTrackStream(extPlaybackInfo, { onProgress: (progress) => (downloadStatus[filePath] = progress) });
return new Promise((res) =>
stream.pipe(createWriteStream(filePath)).on("finish", () => {
delete downloadStatus[filePath];
res();
})
);
} catch (err) {
delete downloadStatus[filePath];
throw err;
}
};
export const getDownloadProgress = (filePath: string) => downloadStatus[filePath];
3 changes: 2 additions & 1 deletion plugins/_lib/nativeBridge/native/request/helpers.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { Readable } from "stream";
import type { TidalManifest } from "../../../Caches/PlaybackInfoTypes";
import { ExtendedRequestOptions } from "./requestStream.native";

export type OnProgress = (info: { total: number; downloaded: number; percent: number }) => void;
export type DownloadProgress = { total: number; downloaded: number; percent: number };
type OnProgress = (progress: DownloadProgress) => void;
export interface FetchyOptions {
onProgress?: OnProgress;
bytesWanted?: number;
Expand Down

0 comments on commit 86480c1

Please sign in to comment.