correctly estimate upload size
This commit is contained in:
parent
a993ab6616
commit
22efde87d5
@ -8,14 +8,9 @@ define([
|
|||||||
var cypherChunkLength = 131088;
|
var cypherChunkLength = 131088;
|
||||||
|
|
||||||
var computeEncryptedSize = function (bytes, meta) {
|
var computeEncryptedSize = function (bytes, meta) {
|
||||||
var metasize = Nacl.util.decodeUTF8(meta).length + 18;
|
var metasize = Nacl.util.decodeUTF8(JSON.stringify(meta)).length;
|
||||||
var chunks = Math.ceil(bytes / plainChunkLength);
|
var chunks = Math.ceil(bytes / plainChunkLength);
|
||||||
console.log({
|
return metasize + 18 + (chunks * 16) + bytes;
|
||||||
metasize: metasize,
|
|
||||||
chunks: chunks,
|
|
||||||
bytes: bytes,
|
|
||||||
});
|
|
||||||
return metasize + (chunks * 16) + bytes;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var encodePrefix = function (p) {
|
var encodePrefix = function (p) {
|
||||||
@ -142,23 +137,15 @@ define([
|
|||||||
|
|
||||||
var i = 0;
|
var i = 0;
|
||||||
|
|
||||||
/*
|
|
||||||
0: metadata
|
|
||||||
1: u8
|
|
||||||
2: done
|
|
||||||
*/
|
|
||||||
|
|
||||||
var state = 0;
|
var state = 0;
|
||||||
|
|
||||||
var next = function (cb) {
|
var next = function (cb) {
|
||||||
|
if (state === 2) { return void cb(); }
|
||||||
|
|
||||||
var start;
|
var start;
|
||||||
var end;
|
var end;
|
||||||
var part;
|
var part;
|
||||||
var box;
|
var box;
|
||||||
|
|
||||||
// DONE
|
|
||||||
if (state === 2) { return void cb(); }
|
|
||||||
|
|
||||||
if (state === 0) { // metadata...
|
if (state === 0) { // metadata...
|
||||||
part = new Uint8Array(plaintext);
|
part = new Uint8Array(plaintext);
|
||||||
box = Nacl.secretbox(part, nonce, key);
|
box = Nacl.secretbox(part, nonce, key);
|
||||||
@ -171,10 +158,6 @@ define([
|
|||||||
.concat(slice(box)));
|
.concat(slice(box)));
|
||||||
state++;
|
state++;
|
||||||
|
|
||||||
// TODO verify that each box is the expected size
|
|
||||||
|
|
||||||
console.log(part.length, prefixed.length);
|
|
||||||
|
|
||||||
return void cb(void 0, prefixed);
|
return void cb(void 0, prefixed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,8 +170,6 @@ define([
|
|||||||
increment(nonce);
|
increment(nonce);
|
||||||
i++;
|
i++;
|
||||||
|
|
||||||
console.log(part.length, box.length);
|
|
||||||
|
|
||||||
// regular data is done
|
// regular data is done
|
||||||
if (i * plainChunkLength >= u8.length) { state = 2; }
|
if (i * plainChunkLength >= u8.length) { state = 2; }
|
||||||
|
|
||||||
|
|||||||
@ -36,6 +36,7 @@ define([
|
|||||||
var key = Nacl.randomBytes(32);
|
var key = Nacl.randomBytes(32);
|
||||||
var next = FileCrypto.encrypt(u8, metadata, key);
|
var next = FileCrypto.encrypt(u8, metadata, key);
|
||||||
|
|
||||||
|
var estimate = FileCrypto.computeEncryptedSize(blob.byteLength, metadata);
|
||||||
var chunks = [];
|
var chunks = [];
|
||||||
|
|
||||||
var sendChunk = function (box, cb) {
|
var sendChunk = function (box, cb) {
|
||||||
@ -47,15 +48,21 @@ define([
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var actual = 0;
|
||||||
var again = function (err, box) {
|
var again = function (err, box) {
|
||||||
if (err) { throw new Error(err); }
|
if (err) { throw new Error(err); }
|
||||||
if (box) {
|
if (box) {
|
||||||
|
actual += box.length;
|
||||||
return void sendChunk(box, function (e) {
|
return void sendChunk(box, function (e) {
|
||||||
if (e) { return console.error(e); }
|
if (e) { return console.error(e); }
|
||||||
next(again);
|
next(again);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (actual !== estimate) {
|
||||||
|
console.error('Estimated size does not match actual size');
|
||||||
|
}
|
||||||
|
|
||||||
// if not box then done
|
// if not box then done
|
||||||
Cryptpad.rpc.send('UPLOAD_COMPLETE', '', function (e, res) {
|
Cryptpad.rpc.send('UPLOAD_COMPLETE', '', function (e, res) {
|
||||||
if (e) { return void console.error(e); }
|
if (e) { return void console.error(e); }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user