delete done parts during assembly to prevent needing double the stream size as available disk space
This commit is contained in:
@@ -67,8 +67,6 @@ export async function download(stream: StreamSummary) {
|
|||||||
export async function remove(streamId: string) {
|
export async function remove(streamId: string) {
|
||||||
cached.delete(streamId);
|
cached.delete(streamId);
|
||||||
await discard(streamId);
|
await discard(streamId);
|
||||||
const root = await navigator.storage.getDirectory();
|
|
||||||
await root.removeEntry(streamId).catch(() => {});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUrl(streamId: string): Promise<string | null> {
|
export async function getUrl(streamId: string): Promise<string | null> {
|
||||||
@@ -140,10 +138,15 @@ async function assemble(streamId: string, total: number) {
|
|||||||
const root = await navigator.storage.getDirectory();
|
const root = await navigator.storage.getDirectory();
|
||||||
const dir = await partsDir(streamId, true);
|
const dir = await partsDir(streamId, true);
|
||||||
const handle = await root.getFileHandle(streamId, { create: true });
|
const handle = await root.getFileHandle(streamId, { create: true });
|
||||||
const writable = await handle.createWritable();
|
|
||||||
|
// track size from previous assembling attempt if there was one
|
||||||
|
const done = (await handle.getFile()).size;
|
||||||
|
|
||||||
|
const writable = await handle.createWritable({ keepExistingData: done > 0 });
|
||||||
|
await writable.seek(done);
|
||||||
|
|
||||||
// check at each step the chunk names (ranges) are coherent
|
// check at each step the chunk names (ranges) are coherent
|
||||||
let written = 0;
|
let written = done;
|
||||||
while (written < total) {
|
while (written < total) {
|
||||||
const name = String(written);
|
const name = String(written);
|
||||||
const part = await dir.getFileHandle(name).catch(() => null);
|
const part = await dir.getFileHandle(name).catch(() => null);
|
||||||
@@ -156,7 +159,8 @@ async function assemble(streamId: string, total: number) {
|
|||||||
}
|
}
|
||||||
await writable.close();
|
await writable.close();
|
||||||
|
|
||||||
await discard(streamId);
|
partials.delete(streamId);
|
||||||
|
await sweep(streamId);
|
||||||
// chunks went missing, shit's fucked...
|
// chunks went missing, shit's fucked...
|
||||||
if (written !== total) throw new Error(`Assembled ${written} of ${total} bytes`);
|
if (written !== total) throw new Error(`Assembled ${written} of ${total} bytes`);
|
||||||
}
|
}
|
||||||
@@ -168,9 +172,11 @@ async function partsDir(streamId: string, create = false) {
|
|||||||
return parts.getDirectoryHandle(streamId, { create });
|
return parts.getDirectoryHandle(streamId, { create });
|
||||||
}
|
}
|
||||||
|
|
||||||
/** discard stream's partial download state and run a sweep of leftover parts files */
|
/** discard everything on disk for a stream that is not fully downloaded */
|
||||||
async function discard(streamId: string) {
|
async function discard(streamId: string) {
|
||||||
partials.delete(streamId);
|
partials.delete(streamId);
|
||||||
|
const root = await navigator.storage.getDirectory();
|
||||||
|
await root.removeEntry(streamId).catch(() => {});
|
||||||
await sweep(streamId);
|
await sweep(streamId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user