diff --git a/www/common/make-backup.js b/www/common/make-backup.js index 38a47f308..0d50dfde4 100644 --- a/www/common/make-backup.js +++ b/www/common/make-backup.js @@ -1,11 +1,13 @@ define([ '/common/cryptget.js', + '/file/file-crypto.js', '/common/common-hash.js', - '/common/sframe-common-file.js', + '/common/common-util.js', '/bower_components/nthen/index.js', '/bower_components/saferphore/index.js', '/bower_components/jszip/dist/jszip.min.js', -], function (Crypt, Hash, SFCFile, nThen, Saferphore, JsZip) { +], function (Crypt, FileCrypto, Hash, Util, nThen, Saferphore, JsZip) { + var saveAs = window.saveAs; var sanitize = function (str) { return str.replace(/[\\/?%*:|"<>]/gi, '_')/*.toLowerCase()*/; @@ -21,22 +23,16 @@ define([ }; var transform = function (ctx, type, sjson, cb) { - console.error("backup - transform"); - console.log('type', type); - console.log('sjson', sjson); - var result = { data: sjson, ext: '.json', }; - console.log('result', result); var json; try { json = JSON.parse(sjson); } catch (e) { return void cb(result); } - console.log('json', json); var path = '/' + type + '/export.js'; require([path], function (Exporter) { Exporter.main(json, function (data) { @@ -49,11 +45,86 @@ define([ }); }; + + var _downloadFile = function (ctx, fData, cb, updateProgress) { + var cancelled = false; + var cancel = function () { + cancelled = true; + }; + var parsed = Hash.parsePadUrl(fData.href || fData.roHref); + var hash = parsed.hash; + var name = fData.filename || fData.title; + var secret = Hash.getSecrets('file', hash, fData.password); + var src = Hash.getBlobPathFromHex(secret.channel); + var key = secret.keys && secret.keys.cryptKey; + Util.fetch(src, function (err, u8) { + if (cancelled) { return; } + if (err) { return void cb('E404'); } + FileCrypto.decrypt(u8, key, function (err, res) { + if (cancelled) { return; } + if (err) { return void cb(err); } + if (!res.content) { return void cb('EEMPTY'); } + var dl = function () { + saveAs(res.content, name || res.metadata.name); + }; + cb(null, { + metadata: res.metadata, + content: res.content, + download: dl + }); + }, updateProgress && updateProgress.progress2); + }, updateProgress && updateProgress.progress); + return { + cancel: cancel + }; + + }; + + + var _downloadPad = function (ctx, pData, cb, updateProgress) { + var cancelled = false; + var cancel = function () { + cancelled = true; + }; + + var parsed = Hash.parsePadUrl(pData.href || pData.roHref); + var name = pData.filename || pData.title; + var opts = { + password: pData.password + }; + updateProgress.progress(0); + ctx.get({ + hash: parsed.hash, + opts: opts + }, function (err, val) { + if (cancelled) { return; } + if (err) { return; } + if (!val) { return; } + updateProgress.progress(1); + + transform(ctx, parsed.type, val, function (res) { + if (cancelled) { return; } + if (!res.data) { return; } + updateProgress.progress2(1); + var dl = function () { + saveAs(res.data, Util.fixFileName(name)); + }; + cb(null, { + metadata: res.metadata, + content: res.data, + download: dl + }); + }); + }); + return { + cancel: cancel + }; + + }; + // Add a file to the zip. We have to cryptget&transform it if it's a pad // or fetch&decrypt it if it's a file. var addFile = function (ctx, zip, fData, existingNames) { - console.error('backup - addFile'); - console.log('fData', fData); if (!fData.href && !fData.roHref) { return void ctx.errors.push({ error: 'EINVAL', @@ -62,7 +133,6 @@ define([ } var parsed = Hash.parsePadUrl(fData.href || fData.roHref); - console.log('parsed', parsed); if (['pad', 'file'].indexOf(parsed.hashData.type) === -1) { return; } // waitFor is used to make sure all the pads and files are process before downloading the zip. @@ -135,7 +205,7 @@ define([ // Files (mediatags...) var todoFile = function () { var it; - var dl = SFCFile.downloadFile(fData, function (err, res) { + var dl = _downloadFile(ctx, fData, function (err, res) { if (it) { clearInterval(it); } if (err) { return void error(err); } var opts = { @@ -232,7 +302,12 @@ define([ }; }; + + return { - create: create + create: create, + downloadFile: _downloadFile, + downloadPad: _downloadPad, + }; }); diff --git a/www/common/sframe-common-file.js b/www/common/sframe-common-file.js index d374737ca..da26531c1 100644 --- a/www/common/sframe-common-file.js +++ b/www/common/sframe-common-file.js @@ -1,6 +1,7 @@ define([ 'jquery', '/file/file-crypto.js', + '/common/make-backup.js', '/common/common-thumbnail.js', '/common/common-interface.js', '/common/common-ui-elements.js', @@ -11,9 +12,8 @@ define([ '/bower_components/file-saver/FileSaver.min.js', '/bower_components/tweetnacl/nacl-fast.min.js', -], function ($, FileCrypto, Thumb, UI, UIElements, Util, Hash, h, Messages) { +], function ($, FileCrypto, MakeBackup, Thumb, UI, UIElements, Util, Hash, h, Messages) { var Nacl = window.nacl; - var saveAs = window.saveAs; var module = {}; var blobToArrayBuffer = function (blob, cb) { @@ -446,88 +446,73 @@ define([ createUploader(config.dropArea, config.hoverArea, config.body); - File.downloadFile = function (fData, cb) { - console.error("SFC FILE -DOWNLOAD FILE"); - console.log("fData", fData); - var parsed = Hash.parsePadUrl(fData.href || fData.roHref); - var hash = parsed.hash; - var name = fData.filename || fData.title; - var secret = Hash.getSecrets('file', hash, fData.password); - var src = Hash.getBlobPathFromHex(secret.channel); - var key = secret.keys && secret.keys.cryptKey; - console.log('key', key); - console.log('src', src); - console.log('secret', secret); - console.log('name', name); - console.log('hash', hash); - console.log('parsed', parsed); - common.getFileSize(secret.channel, function (e, data) { - console.warn("GET FILE SIZE"); - console.log("data", data); - var todo = function (file) { - console.warn("TODO"); - console.log("file", file); - if (queue.inProgress) { return; } - queue.inProgress = true; - var id = file.id; - - var $row = $table.find('tr[id="'+id+'"]'); - var $pv = $row.find('.cp-fileupload-table-progress-value'); - var $pb = $row.find('.cp-fileupload-table-progress-container'); - var $pc = $row.find('.cp-fileupload-table-progress'); - var $link = $row.find('.cp-fileupload-table-link'); - - var done = function () { - $row.find('.cp-fileupload-table-cancel').text('-'); - queue.inProgress = false; - queue.next(); - }; + var updateProgressbar = function (file, data, downloadFunction, cb) { + if (queue.inProgress) { return; } + queue.inProgress = true; + var id = file.id; - var updateDLProgress = function (progressValue) { - var text = Math.round(progressValue*100) + '%'; - text += ' ('+ Messages.download_step1 +'...)'; - $pv.text(text); - $pb.css({ - width: progressValue * $pc.width()+'px' - }); - }; - var updateProgress = function (progressValue) { - var text = Math.round(progressValue*100) + '%'; - text += progressValue === 1 ? '' : ' ('+ Messages.download_step2 +'...)'; - $pv.text(text); - $pb.css({ - width: progressValue * $pc.width()+'px' - }); - }; + var $row = $table.find('tr[id="'+id+'"]'); + var $pv = $row.find('.cp-fileupload-table-progress-value'); + var $pb = $row.find('.cp-fileupload-table-progress-container'); + var $pc = $row.find('.cp-fileupload-table-progress'); + var $link = $row.find('.cp-fileupload-table-link'); - var dl = module.downloadFile(fData, function (err, obj) { - $link.prepend($('', {'class': 'fa fa-external-link'})) - .attr('href', '#') - .click(function (e) { - e.preventDefault(); - obj.download(); - }); - done(); - if (obj) { obj.download(); } - cb(err, obj); - }, { - src: src, - key: key, - name: name, - progress: updateDLProgress, - progress2: updateProgress, - }); + var done = function () { + $row.find('.cp-fileupload-table-cancel').text('-'); + queue.inProgress = false; + queue.next(); + }; - var $cancel = $('', {'class': 'cp-fileupload-table-cancel-button fa fa-times'}).click(function () { - dl.cancel(); - $cancel.remove(); - $row.find('.cp-fileupload-table-progress-value').text(Messages.upload_cancelled); - done(); - }); - $row.find('.cp-fileupload-table-cancel').html('').append($cancel); - }; + var updateDLProgress = function (progressValue) { + var text = Math.round(progressValue * 100) + '%'; + text += ' (' + Messages.download_step1 + '...)'; + $pv.text(text); + $pb.css({ + width: progressValue * $pc.width() + 'px' + }); + }; + var updateProgress = function (progressValue) { + var text = Math.round(progressValue*100) + '%'; + text += progressValue === 1 ? '' : ' (' + Messages.download_step2 + '...)'; + $pv.text(text); + $pb.css({ + width: progressValue * $pc.width()+'px' + }); + }; + + var ctx = { + get: common.getPad, + }; + var dl = downloadFunction(ctx, data, function (err, obj) { + $link.prepend($('', {'class': 'fa fa-external-link'})) + .attr('href', '#') + .click(function (e) { + e.preventDefault(); + obj.download(); + }); + done(); + if (obj) { obj.download(); } + cb(err, obj); + }, { + progress: updateDLProgress, + progress2: updateProgress, + }); + + var $cancel = $('', {'class': 'cp-fileupload-table-cancel-button fa fa-times'}).click(function () { + dl.cancel(); + $cancel.remove(); + $row.find('.cp-fileupload-table-progress-value').text(Messages.upload_cancelled); + done(); + }); + $row.find('.cp-fileupload-table-cancel').html('').append($cancel); + }; + + + File.downloadFile = function (fData, cb) { + var name = fData.filename || fData.title; + common.getFileSize(fData.channel, function (e, data) { queue.push({ - dl: todo, + dl: function (file) { updateProgressbar(file, fData, MakeBackup.downloadFile, cb); }, size: data, name: name }); @@ -535,70 +520,8 @@ define([ }; File.downloadPad = function (pData, cb) { - console.error("SFC FILE -DOWNLOAD PAD"); - console.log("pData", pData); - var todo = function (file) { - console.warn("TODO"); - console.log("file", file); - if (queue.inProgress) { return; } - queue.inProgress = true; - var id = file.id; - - var $row = $table.find('tr[id="'+id+'"]'); - var $pv = $row.find('.cp-fileupload-table-progress-value'); - var $pb = $row.find('.cp-fileupload-table-progress-container'); - var $pc = $row.find('.cp-fileupload-table-progress'); - var $link = $row.find('.cp-fileupload-table-link'); - - var done = function () { - $row.find('.cp-fileupload-table-cancel').text('-'); - queue.inProgress = false; - queue.next(); - }; - - var updateDLProgress = function (progressValue) { - var text = Math.round(progressValue*100) + '%'; - text += ' ('+ Messages.download_step1 +'...)'; - $pv.text(text); - $pb.css({ - width: progressValue * $pc.width()+'px' - }); - }; - var updateProgress = function (progressValue) { - var text = Math.round(progressValue*100) + '%'; - text += progressValue === 1 ? '' : ' ('+ Messages.download_step2 +'...)'; - $pv.text(text); - $pb.css({ - width: progressValue * $pc.width()+'px' - }); - }; - - var dl = module.downloadPad(pData, function (err, obj) { - $link.prepend($('', {'class': 'fa fa-external-link'})) - .attr('href', '#') - .click(function (e) { - e.preventDefault(); - obj.download(); - }); - done(); - if (obj) { obj.download(); } - cb(err, obj); - }, { - common: common, - progress: updateDLProgress, - progress2: updateProgress, - }); - - var $cancel = $('', {'class': 'cp-fileupload-table-cancel-button fa fa-times'}).click(function () { - dl.cancel(); - $cancel.remove(); - $row.find('.cp-fileupload-table-progress-value').text(Messages.upload_cancelled); - done(); - }); - $row.find('.cp-fileupload-table-cancel').html('').append($cancel); - }; queue.push({ - dl: todo, + dl: function (file) { updateProgressbar(file, pData, MakeBackup.downloadPad, cb); }, size: 0, name: pData.title, }); @@ -607,135 +530,6 @@ define([ return File; }; - module.downloadFile = function (fData, cb, obj) { - var cancelled = false; - var cancel = function () { - cancelled = true; - }; - var src, key, name; - if (obj && obj.src && obj.key && obj.name) { - src = obj.src; - key = obj.key; - name = obj.name; - } else { - var parsed = Hash.parsePadUrl(fData.href || fData.roHref); - var hash = parsed.hash; - name = fData.filename || fData.title; - var secret = Hash.getSecrets('file', hash, fData.password); - src = Hash.getBlobPathFromHex(secret.channel); - key = secret.keys && secret.keys.cryptKey; - } - Util.fetch(src, function (err, u8) { - if (cancelled) { return; } - if (err) { return void cb('E404'); } - FileCrypto.decrypt(u8, key, function (err, res) { - if (cancelled) { return; } - if (err) { return void cb(err); } - if (!res.content) { return void cb('EEMPTY'); } - var dl = function () { - saveAs(res.content, name || res.metadata.name); - }; - cb(null, { - metadata: res.metadata, - content: res.content, - download: dl - }); - }, obj && obj.progress2); - }, obj && obj.progress); - return { - cancel: cancel - }; - }; - - - var getPad = function (common, data, cb) { - var sframeChan = common.getSframeChannel(); - sframeChan.query("Q_CRYPTGET", data, function (err, obj) { - if (err) { return void cb(err); } - if (obj.error) { return void cb(obj.error); } - cb(null, obj.data); - }, { timeout: 60000 }); - }; - - var transform = function (type, sjson, cb) { - console.error("SFCfile - transform"); - console.log('type', type); - console.log('sjson', sjson); - - var result = { - data: sjson, - ext: '.json', - }; - console.log('result', result); - var json; - try { - json = JSON.parse(sjson); - } catch (e) { - return void cb(result); - } - console.log('json', json); - var path = '/' + type + '/export.js'; - require([path], function (Exporter) { - Exporter.main(json, function (data) { - result.ext = Exporter.ext || ''; - result.data = data; - cb(result); - }); - }, function () { - cb(result); - }); - }; - - module.downloadPad = function (pData, cb, obj) { - console.error("SFC file - downloadPad"); - console.log(pData, pData); - var cancelled = false; - var cancel = function () { - cancelled = true; - }; - - var parsed = Hash.parsePadUrl(pData.href || pData.roHref); - var hash = parsed.hash; - var name = pData.filename || pData.title; - var opts = { - password: pData.password - }; - console.log('parsed', parsed); - console.log('hash', hash); - console.log('name', name); - console.log('opts', opts); - obj.progress(0); - getPad(obj.common, { - hash: parsed.hash, - opts: opts - }, function (err, val) { - if (cancelled) { return; } - if (err) { return; } - if (!val) { return; } - console.log('val', val); - obj.progress(1); - - transform(parsed.type, val, function (res) { - console.error("transform callback"); - console.log('res', res); - if (cancelled) { return; } - if (!res.data) { return; } - obj.progress2(1); - var dl = function () { - saveAs(res.data, Util.fixFileName(name)); - }; - cb(null, { - metadata: res.metadata, - content: res.data, - download: dl - }); - console.log('DONE ---- ' + name); - }); - }); - return { - cancel: cancel - }; - }; return module; }); diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index f93021bd3..82c71ace2 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -459,6 +459,14 @@ define([ }); }; */ + funcs.getPad = function (data, cb) { + ctx.sframeChan.query("Q_CRYPTGET", data, function (err, obj) { + if (err) { return void cb(err); } + if (obj.error) { return void cb(obj.error); } + cb(null, obj.data); + }, { timeout: 60000 }); + }; + funcs.gotoURL = function (url) { ctx.sframeChan.event('EV_GOTO_URL', url); }; funcs.openURL = function (url) { ctx.sframeChan.event('EV_OPEN_URL', url); }; funcs.openUnsafeURL = function (url) { diff --git a/www/drive/inner.js b/www/drive/inner.js index a611a7829..b6e480a9d 100644 --- a/www/drive/inner.js +++ b/www/drive/inner.js @@ -3472,32 +3472,20 @@ define([ var downloadFolder = function (folderElement, folderName) { console.warn("downloadFolder"); var todo = function (data) { - var getPad = function (data, cb) { - sframeChan.query("Q_CRYPTGET", data, function (err, obj) { - if (err) { return void cb(err); } - if (obj.error) { return void cb(obj.error); } - cb(null, obj.data); - }, { timeout: 60000 }); - }; - data.folder = folderElement; folderName = Util.fixFileName(folderName) + '.zip'; console.log("data", data); console.log("folderName", folderName); - Backup.create(data, getPad, function (blob, errors) { + Backup.create(data, common.getPad, function (blob, errors) { console.log("blob", blob); window.saveAs(blob, folderName); console.error(errors); }, function () {}); }; - sframeChan.query("Q_SETTINGS_DRIVE_GET", "full", function (err, data) { - console.warn("sframeChan.query Q_SETTINGS_DRIVE_GET callback"); - console.log("err", err); - console.log("data", data); - if (err) { return void console.error(err); } - if (data.error) { return void console.error(data.error); } - todo(data); + todo({ + uo: proxy, + sf: folders, }); }; @@ -3594,10 +3582,8 @@ define([ if (paths.length !== 1) { return; } var path = paths[0]; el = manager.find(path.path); - console.log("paths", paths); console.log("el", el); console.log('path', path); - console.log("APP", APP); // folder if (manager.isFolder(el)) { // folder @@ -3625,7 +3611,6 @@ define([ else if (manager.isFile(el)) { // imported file if (path.element.is(".cp-border-color-file")) { - console.log("--isFile--"); data = manager.getFileData(el); APP.FM.downloadFile(data, function (err, obj) { console.log(err, obj); @@ -3634,9 +3619,7 @@ define([ } // pad else { - console.log("--isPad--"); data = manager.getFileData(el); - console.log("data:", data); APP.FM.downloadPad(data, function (err, obj) { console.log(err, obj); console.log('DONE'); diff --git a/www/drive/main.js b/www/drive/main.js index c4918cf9d..907be7451 100644 --- a/www/drive/main.js +++ b/www/drive/main.js @@ -63,31 +63,6 @@ define([ Utils.LocalStore.clearThumbnail(); window.location.reload(); }); - sframeChan.on('Q_SETTINGS_DRIVE_GET', function (d, cb) { - Cryptpad.getUserObject(function (obj) { - if (obj.error) { return void cb(obj); } - if (d === "full") { - // We want shared folders too - var result = { - uo: obj, - sf: {} - }; - if (!obj.drive || !obj.drive.sharedFolders) { return void cb(result); } - Utils.nThen(function (waitFor) { - Object.keys(obj.drive.sharedFolders).forEach(function (id) { - Cryptpad.getSharedFolder(id, waitFor(function (obj) { - result.sf[id] = obj; - })); - }); - }).nThen(function () { - cb(result); - }); - return; - } - // We want only the user object - cb(obj); - }); - }); sframeChan.on('Q_DRIVE_USEROBJECT', function (data, cb) { Cryptpad.userObjectCommand(data, cb); }); diff --git a/www/settings/inner.js b/www/settings/inner.js index 37143a617..a7a006b23 100644 --- a/www/settings/inner.js +++ b/www/settings/inner.js @@ -1082,18 +1082,9 @@ define([ var exportDrive = function () { Feedback.send('FULL_DRIVE_EXPORT_START'); var todo = function (data, filename) { - var getPad = function (data, cb) { - sframeChan.query("Q_CRYPTGET", data, function (err, obj) { - if (err) { return void cb(err); } - if (obj.error) { return void cb(obj.error); } - cb(null, obj.data); - }, { timeout: 60000 }); - }; - var ui = createExportUI(); - var bu = Backup.create(data, getPad, function (blob, errors) { - console.log(blob); + var bu = Backup.create(data, common.getPad, function (blob, errors) { saveAs(blob, filename); sframeChan.event('EV_CRYPTGET_DISCONNECT'); ui.complete(function () {