1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-04 01:59:15 +02:00

Surface metadata from zip files to clients

This commit is contained in:
codedread 2020-01-19 09:33:58 -08:00
parent 2916cf0926
commit 58ac1aed25
2 changed files with 12 additions and 5 deletions

View file

@ -88,8 +88,12 @@ bitjs.archive.UnarchiveStartEvent = class extends bitjs.archive.UnarchiveEvent {
* Finish event. * Finish event.
*/ */
bitjs.archive.UnarchiveFinishEvent = class extends bitjs.archive.UnarchiveEvent { bitjs.archive.UnarchiveFinishEvent = class extends bitjs.archive.UnarchiveEvent {
constructor() { /**
* @param {Object} metadata A collection fo metadata about the archive file.
*/
constructor(metadata = {}) {
super(bitjs.archive.UnarchiveEvent.Type.FINISH); super(bitjs.archive.UnarchiveEvent.Type.FINISH);
this.metadata = metadata;
} }
} }
@ -281,6 +285,7 @@ bitjs.archive.Unarchiver = class {
/** /**
* Adds more bytes to the unarchiver's Worker thread. * Adds more bytes to the unarchiver's Worker thread.
* @param {ArrayBuffer} ab The ArrayBuffer with more bytes in it.
*/ */
update(ab) { update(ab) {
if (this.worker_) { if (this.worker_) {

View file

@ -574,7 +574,6 @@ function unzip() {
} }
// read all file headers // read all file headers
// TODO: This structure tells us the intended order of the files in the zip.
while (bstream.peekNumber(4) == zCentralFileHeaderSignature) { while (bstream.peekNumber(4) == zCentralFileHeaderSignature) {
bstream.readNumber(4); // signature bstream.readNumber(4); // signature
const cdfh = { const cdfh = {
@ -618,6 +617,7 @@ function unzip() {
bstream.readString(sizeOfSignature); // digital signature data bstream.readString(sizeOfSignature); // digital signature data
} }
let metadata = {};
if (bstream.peekNumber(4) == zEndOfCentralDirSignature) { if (bstream.peekNumber(4) == zEndOfCentralDirSignature) {
bstream.readNumber(4); // signature bstream.readNumber(4); // signature
const eocds = { const eocds = {
@ -636,11 +636,15 @@ function unzip() {
console.log(` ${field} = ${eocds[field]}`); console.log(` ${field} = ${eocds[field]}`);
} }
} }
metadata.comment = eocds.comment;
} }
postProgress(); postProgress();
bytestream = bstream.tee(); bytestream = bstream.tee();
unarchiveState = UnarchiveState.FINISHED;
postMessage(new bitjs.archive.UnarchiveFinishEvent(metadata));
} }
// event.data.file has the first ArrayBuffer. // event.data.file has the first ArrayBuffer.
@ -677,8 +681,6 @@ onmessage = function(event) {
unarchiveState === UnarchiveState.WAITING) { unarchiveState === UnarchiveState.WAITING) {
try { try {
unzip(); unzip();
unarchiveState = UnarchiveState.FINISHED;
postMessage(new bitjs.archive.UnarchiveFinishEvent());
} catch (e) { } catch (e) {
if (typeof e === 'string' && e.startsWith('Error! Overflowed')) { if (typeof e === 'string' && e.startsWith('Error! Overflowed')) {
// Overrun the buffer. // Overrun the buffer.