From d086f9dfab745e9adcdd52c23ab2d58d03e92070 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 13 May 2018 21:27:46 +0200 Subject: [PATCH] Generate a ZIP file with orig JSON file --- assets/js/components/account/export/ui.js | 16 ++++ assets/js/components/account/export/worker.js | 80 ++++++++++++++++++- 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/assets/js/components/account/export/ui.js b/assets/js/components/account/export/ui.js index a03a723b..6eb557d8 100644 --- a/assets/js/components/account/export/ui.js +++ b/assets/js/components/account/export/ui.js @@ -49,6 +49,13 @@ ComunicWeb.components.account.export.ui = { }); this.updateProgress(1); + //Add message + this._exportModal.messageContainer = createElem2({ + appendTo: this._exportModal.modalBody, + type: "p", + innerHTML: "Starting..." + }); + //Create close modal function var closeModal = function(){ $(modal).modal('hide'); @@ -72,6 +79,15 @@ ComunicWeb.components.account.export.ui = { this._exportModal.progress.style.width = progress + "%"; }, + /** + * Update the message shown on the screen + * + * @param {String} message The new message + */ + updateMessage: function(message){ + this._exportModal.messageContainer.innerHTML = message; + }, + /** * Display an error that prevent the success of the operation * diff --git a/assets/js/components/account/export/worker.js b/assets/js/components/account/export/worker.js index 9f955fd1..f042dc22 100644 --- a/assets/js/components/account/export/worker.js +++ b/assets/js/components/account/export/worker.js @@ -22,6 +22,7 @@ ComunicWeb.components.account.export.worker = { } //Update progress + ComunicWeb.components.account.export.ui.updateMessage("Got text data"); ComunicWeb.components.account.export.ui.updateProgress(10); //Parse data @@ -31,15 +32,92 @@ ComunicWeb.components.account.export.worker = { }, /** - * Parse account text data + * Parse account text data into ZIP file * * @param {Object} data Text data about the account */ parse: function(data){ + //Get UI shorcut + var ui = ComunicWeb.components.account.export.ui; + + var Promise = window.Promise; + if (!Promise) { + Promise = JSZip.external.Promise; + } + + /** + * Fetch the content and return the associated promise. + * @param {String} url the url of the content to fetch. + * @return {Promise} the promise containing the data. + */ + function urlToPromise(url) { + return new Promise(function(resolve, reject) { + JSZipUtils.getBinaryContent(url, function (err, data) { + if(err) { + reject(err); + } else { + resolve(data); + } + }); + }); + } + + /** + * Transform an URL into a path in the archive + * + * @param {String} url The URL to transform + * @return {String} Generated file path + */ + function urlToPath(url) { + var path = url.replace("://", "/"); + return "files/" + path; + } + //Determine the list of files to download var files_list = this._generate_files_list(data); + //Create zip file + var zip = new JSZip(); + + //Add raw json file + zip.file("source.json", JSON.stringify(data)); + + //Add the files to download + /*files_list.forEach(function(url){ + var path = urlToPath(url); + zip.file(path, urlToPromise(url), {binary:true}); + });*/ + + //Generated zip archive + zip.generateAsync({type:"blob"}, function updateCallback(metadata) { + var msg = "progression : " + metadata.percent.toFixed(2) + " %"; + if(metadata.currentFile) { + msg += ", current file = " + metadata.currentFile; + } + ui.updateMessage(msg); + ui.updateProgress(metadata.percent.toFixed(2)); + }) + + //Trigger download + .then(function callback(blob) { + + //Download file + saveAs(blob, "accountData.zip"); + + //Update progress + ui.updateProgress(100); + ui.updateMessage("Done !"); + + }, function (e) { + //In case of error + ComunicWeb.components.account.export.ui.exportFatalError(e); + + //Update progress + ui.updateProgress(100); + ui.updateMessage("Error !"); + }); + }, /**