From 8e760a905f423b0f405ed41b8bb891cb81233101 Mon Sep 17 00:00:00 2001 From: 0d0 <0d0acre@esiliati.org> Date: Tue, 22 Apr 2025 20:37:21 +0200 Subject: [PATCH] Update download logic --- src/lib/client/downloader.ts | 21 ------ src/lib/components/DownloadManager.svelte | 81 +++++------------------ src/routes/+page.svelte | 47 +++++++++++-- 3 files changed, 57 insertions(+), 92 deletions(-) delete mode 100644 src/lib/client/downloader.ts diff --git a/src/lib/client/downloader.ts b/src/lib/client/downloader.ts deleted file mode 100644 index c75c651..0000000 --- a/src/lib/client/downloader.ts +++ /dev/null @@ -1,21 +0,0 @@ -const createAnchorElement = (url: string, filename: string): HTMLAnchorElement => { - const anchor = document.createElement('a'); - anchor.href = url; - anchor.download = filename; - return anchor; -}; -export const download = async (url: string, filename: string) => { - const response = await fetch(url); - - if (!response.ok) { - throw new Error('Network response was not ok'); - } - - const blob = await response.blob(); - const objectURL = window.URL.createObjectURL(blob); - const anchor = createAnchorElement(url, filename); - document.body.appendChild(anchor); - anchor.click(); - anchor.remove(); - window.URL.revokeObjectURL(objectURL); -}; diff --git a/src/lib/components/DownloadManager.svelte b/src/lib/components/DownloadManager.svelte index efe3bc0..ffa1444 100644 --- a/src/lib/components/DownloadManager.svelte +++ b/src/lib/components/DownloadManager.svelte @@ -1,73 +1,22 @@ -{#if visible} +
-
-

- Downloading {filename} -

-
-
-
-

{progress}%

+

+ Downloading {filename} +

+
+
+

{progress}%

-{/if} +
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index eb2fc45..ac7dcae 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -2,12 +2,12 @@ import { PUBLIC_VERSION } from '$env/static/public'; import supportedFormats from '$lib/common/supportedFormats.json'; import Loader from '$lib/components/Loader.svelte'; - import { download } from '$lib/client/downloader'; import DownloadManager from '$lib/components/DownloadManager.svelte'; import { mount, unmount } from 'svelte'; let source = $state('youtube'); let link = $state(''); + let downloading = $state(false); let format = $state('mp3'); let showModal = $state(false); let href = $state(''); @@ -15,6 +15,8 @@ let metadata = $state(false); let logs = $state(''); let downloadManager: DownloadManager | null = null; + let progress = $state(0); + let filename = $state(''); const formats = Object.keys(supportedFormats).map((f) => { return { value: f, label: f.toUpperCase() }; @@ -28,15 +30,46 @@ showModal = !showModal; }; - const dismiss = () => { - unmount(downloadManager); + const download = async (url: string) => { + const response = await fetch(url); + + if (!response.ok) { + throw new Error('Network response was not ok'); + } + + const contentDisposition: string | null = response?.headers?.get('content-disposition'); + filename = contentDisposition?.split('filename=')[1] || 'noname'; + const contentLength: number = Number(response?.headers?.get('content-length')); + const reader = response?.body?.getReader(); + const chunks: Uint8Array[] = []; + let receivedLength = 0; + + while (true) { + const { done, value }: ReadableStreamReadResult = await reader!.read(); + if (done) break; + if (value) { + chunks.push(value); + receivedLength += value.length; + progress = Math.round((receivedLength / contentLength) * 100); + } + } + + const blob = new Blob(chunks); + const downloadUrl = URL.createObjectURL(blob); + + const a = document.createElement('a'); + a.href = downloadUrl; + a.download = filename; + a.click(); + window.URL.revokeObjectURL(downloadUrl); }; const onClick = async (evt) => { evt.preventDefault(); - const props = $state({ url: href }); - downloadManager = mount(DownloadManager, { target: document.body, props, events: { dismiss } }); + downloading = true; + await download(href); + downloading = false; link = ''; }; @@ -187,6 +220,10 @@
+{#if downloading} + +{/if} + {#if showModal}