Update download logic
Some checks failed
Bump deps (only minor versions) / ci (push) Has been cancelled
Some checks failed
Bump deps (only minor versions) / ci (push) Has been cancelled
This commit is contained in:
parent
0706e42e85
commit
8e760a905f
3 changed files with 57 additions and 92 deletions
|
@ -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);
|
|
||||||
};
|
|
|
@ -1,57 +1,7 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from 'svelte';
|
let { progress, filename } = $props();
|
||||||
let { url, dismiss } = $props();
|
|
||||||
|
|
||||||
let visible = $state(false);
|
|
||||||
let progress = $state(0);
|
|
||||||
let filename = $state('');
|
|
||||||
onMount(async () => {
|
|
||||||
if (!url) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
visible = true;
|
|
||||||
|
|
||||||
const response = await fetch(url);
|
|
||||||
if (!response.ok) throw new Error('Download failed');
|
|
||||||
|
|
||||||
const contentDisposition: string = response?.headers?.get('content-disposition');
|
|
||||||
filename = contentDisposition.split('filename=')[1];
|
|
||||||
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<Uint8Array> = 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();
|
|
||||||
|
|
||||||
URL.revokeObjectURL(downloadUrl);
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
visible = false;
|
|
||||||
}, 1500); // auto-dismiss
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
visible = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if visible}
|
|
||||||
<div
|
<div
|
||||||
class="fixed inset-0 z-50 flex items-center justify-center bg-black/80 font-mono backdrop-blur-sm"
|
class="fixed inset-0 z-50 flex items-center justify-center bg-black/80 font-mono backdrop-blur-sm"
|
||||||
>
|
>
|
||||||
|
@ -70,4 +20,3 @@
|
||||||
<p class="mt-2 text-center text-pink-400">{progress}%</p>
|
<p class="mt-2 text-center text-pink-400">{progress}%</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
import { PUBLIC_VERSION } from '$env/static/public';
|
import { PUBLIC_VERSION } from '$env/static/public';
|
||||||
import supportedFormats from '$lib/common/supportedFormats.json';
|
import supportedFormats from '$lib/common/supportedFormats.json';
|
||||||
import Loader from '$lib/components/Loader.svelte';
|
import Loader from '$lib/components/Loader.svelte';
|
||||||
import { download } from '$lib/client/downloader';
|
|
||||||
import DownloadManager from '$lib/components/DownloadManager.svelte';
|
import DownloadManager from '$lib/components/DownloadManager.svelte';
|
||||||
import { mount, unmount } from 'svelte';
|
import { mount, unmount } from 'svelte';
|
||||||
|
|
||||||
let source = $state('youtube');
|
let source = $state('youtube');
|
||||||
let link = $state('');
|
let link = $state('');
|
||||||
|
let downloading = $state(false);
|
||||||
let format = $state('mp3');
|
let format = $state('mp3');
|
||||||
let showModal = $state(false);
|
let showModal = $state(false);
|
||||||
let href = $state('');
|
let href = $state('');
|
||||||
|
@ -15,6 +15,8 @@
|
||||||
let metadata = $state(false);
|
let metadata = $state(false);
|
||||||
let logs = $state('');
|
let logs = $state('');
|
||||||
let downloadManager: DownloadManager | null = null;
|
let downloadManager: DownloadManager | null = null;
|
||||||
|
let progress = $state(0);
|
||||||
|
let filename = $state('');
|
||||||
|
|
||||||
const formats = Object.keys(supportedFormats).map((f) => {
|
const formats = Object.keys(supportedFormats).map((f) => {
|
||||||
return { value: f, label: f.toUpperCase() };
|
return { value: f, label: f.toUpperCase() };
|
||||||
|
@ -28,15 +30,46 @@
|
||||||
showModal = !showModal;
|
showModal = !showModal;
|
||||||
};
|
};
|
||||||
|
|
||||||
const dismiss = () => {
|
const download = async (url: string) => {
|
||||||
unmount(downloadManager);
|
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<Uint8Array> = 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) => {
|
const onClick = async (evt) => {
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
|
|
||||||
const props = $state({ url: href });
|
downloading = true;
|
||||||
downloadManager = mount(DownloadManager, { target: document.body, props, events: { dismiss } });
|
await download(href);
|
||||||
|
downloading = false;
|
||||||
|
|
||||||
link = '';
|
link = '';
|
||||||
};
|
};
|
||||||
|
@ -187,6 +220,10 @@
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{#if downloading}
|
||||||
|
<DownloadManager {filename} {progress}></DownloadManager>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<!-- Modal -->
|
<!-- Modal -->
|
||||||
{#if showModal}
|
{#if showModal}
|
||||||
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/90 text-green-300">
|
<div class="fixed inset-0 z-50 flex items-center justify-center bg-black/90 text-green-300">
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue