fetch and decrypt metadata with range requests

pull/1/head
ansuz 8 years ago
parent a99080fb74
commit 0b6c587e2b

@ -61,6 +61,43 @@ define([
return new Blob(chunks);
};
var concatBuffer = function (a, b) { // TODO make this not so ugly
return new Uint8Array(slice(a).concat(slice(b)));
};
var fetchMetadata = function (src, cb) {
var done = false;
var CB = function (err, res) {
if (done) { return; }
done = true;
cb(err, res);
};
var xhr = new XMLHttpRequest();
xhr.open("GET", src, true);
xhr.setRequestHeader('Range', 'bytes=0-1');
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
if (/^4/.test('' + this.status)) { return CB('XHR_ERROR'); }
var res = new Uint8Array(xhr.response);
var size = decodePrefix(res);
var xhr2 = new XMLHttpRequest();
xhr2.open("GET", src, true);
xhr2.setRequestHeader('Range', 'bytes=2-' + (size + 2));
xhr2.responseType = 'arraybuffer';
xhr2.onload = function () {
if (/^4/.test('' + this.status)) { return CB('XHR_ERROR'); }
var res2 = new Uint8Array(xhr2.response);
var all = concatBuffer(res, res2);
CB(void 0, all);
};
xhr2.send(null);
};
xhr.send(null);
};
var decryptMetadata = function (u8, key) {
var prefix = u8.subarray(0, 2);
var metadataLength = decodePrefix(prefix);
@ -74,6 +111,13 @@ define([
catch (e) { return null; }
};
var fetchDecryptedMetadata = function (src, key, cb) {
fetchMetadata(src, function (e, buffer) {
if (e) { return cb(e); }
cb(void 0, decryptMetadata(buffer, key));
});
};
var decrypt = function (u8, key, done, progress) {
var MAX = u8.length;
var _progress = function (offset) {
@ -212,5 +256,7 @@ define([
joinChunks: joinChunks,
computeEncryptedSize: computeEncryptedSize,
decryptMetadata: decryptMetadata,
fetchMetadata: fetchMetadata,
fetchDecryptedMetadata: fetchDecryptedMetadata,
};
});

@ -255,19 +255,21 @@ define([
if (myFile) { return void exportFile(); }
var src = Cryptpad.getBlobPathFromHex(hexFileName);
var cryptKey = secret.keys && secret.keys.fileKeyStr;
var key = Nacl.util.decodeBase64(cryptKey);
/* return FileCrypto.fetchDecryptedMetadata(src, key, function (e, metadata) {
if (e) { return console.error(e); }
console.log(metadata);
});*/
return Cryptpad.fetch(src, function (e, u8) {
if (e) { return void Cryptpad.alert(e); }
// now decrypt the u8
var cryptKey = secret.keys && secret.keys.fileKeyStr;
var key = Nacl.util.decodeBase64(cryptKey);
if (!u8 || !u8.length) {
return void Cryptpad.errorLoadingScreen(e);
}
return console.error(FileCrypto.decryptMetadata(u8, key));
FileCrypto.decrypt(u8, key, function (e, data) {
if (e) {
return console.error(e);

Loading…
Cancel
Save