From 08578eef0b96e00179308907ab925efc42ca1e2f Mon Sep 17 00:00:00 2001 From: 0d0 <0d0acre@esiliati.org> Date: Sat, 19 Apr 2025 17:58:13 +0200 Subject: [PATCH] Update download method --- src/lib/client/downloader.ts | 21 +++++++ src/lib/components/DownloadManager.svelte | 68 ++++++++++++++++++++++- src/lib/server/ytdlp.ts | 15 +++-- src/routes/+page.svelte | 17 +++++- src/routes/download/+server.ts | 30 +++++----- 5 files changed, 128 insertions(+), 23 deletions(-) create mode 100644 src/lib/client/downloader.ts diff --git a/src/lib/client/downloader.ts b/src/lib/client/downloader.ts new file mode 100644 index 0000000..cb2d821 --- /dev/null +++ b/src/lib/client/downloader.ts @@ -0,0 +1,21 @@ +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 2ca9b46..761f47f 100644 --- a/src/lib/components/DownloadManager.svelte +++ b/src/lib/components/DownloadManager.svelte @@ -1,3 +1,69 @@ + +{#if visible} +
+ Downloading {filename} +
+{progress}%
+