diff --git a/www/common/sframe-common-file.js b/www/common/sframe-common-file.js index 099176afd..d374737ca 100644 --- a/www/common/sframe-common-file.js +++ b/www/common/sframe-common-file.js @@ -447,14 +447,26 @@ 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; @@ -522,6 +534,76 @@ 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, + size: 0, + name: pData.title, + }); + }; + return File; }; @@ -565,5 +647,95 @@ define([ }; }; + + 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/drive/inner.js b/www/drive/inner.js index 0fdcd4491..4726d7633 100644 --- a/www/drive/inner.js +++ b/www/drive/inner.js @@ -3555,11 +3555,14 @@ define([ refresh(); } else if ($(this).hasClass('cp-app-drive-context-download')) { + console.error('DOWNLOAD'); 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)) { @@ -3586,6 +3589,12 @@ 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'); + }); } } }