generated from uwu/neptune-template
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SongDownloader - Working native download progress
- Loading branch information
Showing
4 changed files
with
35 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters