From b1a1f4ba130b647249ba591c02f4f94d809545c4 Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 5 Sep 2017 10:49:26 +0200 Subject: [PATCH] add encrypted thumbnails to metadata for images. correct decryption logic --- www/common/common-file.js | 45 +++++++++++++++++++++++++++------------ www/file/file-crypto.js | 2 +- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/www/common/common-file.js b/www/common/common-file.js index f22ebdf34..0c3eadd36 100644 --- a/www/common/common-file.js +++ b/www/common/common-file.js @@ -1,12 +1,13 @@ define([ 'jquery', '/file/file-crypto.js', + '/common/common-thumbnail.js', '/bower_components/tweetnacl/nacl-fast.min.js', -], function ($, FileCrypto) { +], function ($, FileCrypto, Thumb) { var Nacl = window.nacl; var module = {}; - var blobToArrayBuffer = function (blob, cb) { + var blobToArrayBuffer = module.blobToArrayBuffer = function (blob, cb) { var reader = new FileReader(); reader.onloadend = function () { cb(void 0, this.result); @@ -263,30 +264,46 @@ define([ var handleFile = File.handleFile = function (file, e, thumbnail) { var thumb; - var finish = function (arrayBuffer) { + var file_arraybuffer; + var finish = function () { var metadata = { name: file.name, type: file.type, }; if (thumb) { metadata.thumbnail = thumb; } queue.push({ - blob: arrayBuffer, + blob: file_arraybuffer, metadata: metadata, dropEvent: e }); }; - var processFile = function () { - blobToArrayBuffer(file, function (e, buffer) { - finish(buffer); - }); - }; - - if (!thumbnail) { return void processFile(); } - blobToArrayBuffer(thumbnail, function (e, buffer) { + blobToArrayBuffer(file, function (e, buffer) { if (e) { console.error(e); } - thumb = arrayBufferToString(buffer); - processFile(); + file_arraybuffer = buffer; + if (thumbnail) { // there is already a thumbnail + return blobToArrayBuffer(thumbnail, function (e, buffer) { + if (e) { console.error(e); } + thumb = arrayBufferToString(buffer); + finish(); + }); + } + + if (!Thumb.isSupportedType(file.type)) { return finish(); } + // make a resized thumbnail from the image.. + Thumb.fromImageBlob(file, function (e, thumb_blob) { + if (e) { console.error(e); } + if (!thumb_blob) { return finish(); } + + blobToArrayBuffer(thumb_blob, function (e, buffer) { + if (e) { + console.error(e); + return finish(); + } + thumb = arrayBufferToString(buffer); + finish(); + }); + }); }); }; diff --git a/www/file/file-crypto.js b/www/file/file-crypto.js index 49ced7553..f2202c8f8 100644 --- a/www/file/file-crypto.js +++ b/www/file/file-crypto.js @@ -185,7 +185,7 @@ define([ }); } if (plaintext) { - if (i * cypherChunkLength < u8.length) { // not done + if ((2 + metadataLength + i * cypherChunkLength) < u8.length) { // not done chunks.push(plaintext); return setTimeout(again); }