Preview compatible fragmented MP4 transfers while saving the verified copy
This commit is contained in:
@@ -27,11 +27,13 @@ async function run(m) {
|
||||
else if (m.op === 'finish') {
|
||||
await close();
|
||||
if (pos !== claim.size || hash.hex() !== claim.cid) { await dir.removeEntry(name); throw Error('File verification failed; retry starts from zero'); }
|
||||
const final = `${claim.id}.mp4`;
|
||||
const extension = /^(mp4|m4a|webm|ogg|opus|mkv|mp3|wav|aac)$/.test(claim.extension) ? claim.extension : 'mp4';
|
||||
const final = `${claim.id}.${extension}`;
|
||||
// Copy only after verification; existing saved playback remains intact until then.
|
||||
const out = await (await dir.getFileHandle(final, { create: true })).createSyncAccessHandle();
|
||||
try { const f = await handle.getFile(); for (let at = 0; at < f.size; at += 1024 * 1024) out.write(new Uint8Array(await f.slice(at, at + 1024 * 1024).arrayBuffer()), { at }); out.truncate(f.size); out.flush(); } finally { out.close(); }
|
||||
await dir.removeEntry(name);
|
||||
for await (const [old, entry] of dir.entries()) { if (entry.kind === 'file' && old !== final && old.startsWith(claim.id + '.') && /\.(mp4|m4a|webm|ogg|opus|mkv|mp3|wav|aac)$/.test(old)) await dir.removeEntry(old); }
|
||||
self.postMessage({ op: 'done', file: claim });
|
||||
}
|
||||
} catch (e) { await close(); self.postMessage({ op: 'error', error: e.message }); }
|
||||
|
||||
Reference in New Issue
Block a user