Put stream in downloading map on click rather than on dl start
prevents network issue race conditions
This commit is contained in:
@@ -18,32 +18,36 @@ if (browser) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function download(stream: StreamSummary) {
|
export async function download(stream: StreamSummary) {
|
||||||
const res = await fetch(`/media/tracks/${stream.filename}`);
|
if (downloading.has(stream.id) || cached.has(stream.id)) return;
|
||||||
if (!res.ok || !res.body) throw new Error('Download failed');
|
|
||||||
|
|
||||||
const total = Number(res.headers.get('content-length') || 0);
|
|
||||||
const reader = res.body.getReader();
|
|
||||||
const chunks: Uint8Array[] = [];
|
|
||||||
let received = 0;
|
|
||||||
|
|
||||||
downloading.set(stream.id, 0);
|
downloading.set(stream.id, 0);
|
||||||
|
|
||||||
while (true) {
|
try {
|
||||||
const { done, value } = await reader.read();
|
const res = await fetch(`/media/tracks/${stream.filename}`);
|
||||||
if (done) break;
|
if (!res.ok || !res.body) throw new Error('Download failed');
|
||||||
chunks.push(value);
|
|
||||||
received += value.length;
|
const total = Number(res.headers.get('content-length') || 0);
|
||||||
if (total) downloading.set(stream.id, Math.round((received / total) * 100));
|
const reader = res.body.getReader();
|
||||||
|
const chunks: Uint8Array[] = [];
|
||||||
|
let received = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) break;
|
||||||
|
chunks.push(value);
|
||||||
|
received += value.length;
|
||||||
|
if (total) downloading.set(stream.id, Math.round((received / total) * 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
const root = await navigator.storage.getDirectory();
|
||||||
|
const handle = await root.getFileHandle(stream.id, { create: true });
|
||||||
|
const writable = await handle.createWritable();
|
||||||
|
await writable.write(new Blob(chunks));
|
||||||
|
await writable.close();
|
||||||
|
|
||||||
|
cached.add(stream.id);
|
||||||
|
} finally {
|
||||||
|
downloading.delete(stream.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
const root = await navigator.storage.getDirectory();
|
|
||||||
const handle = await root.getFileHandle(stream.id, { create: true });
|
|
||||||
const writable = await handle.createWritable();
|
|
||||||
await writable.write(new Blob(chunks));
|
|
||||||
await writable.close();
|
|
||||||
|
|
||||||
downloading.delete(stream.id);
|
|
||||||
cached.add(stream.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function remove(streamId: string) {
|
export async function remove(streamId: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user