From a09daa21ab4770ed525c3c746ddd5c6e6f56c1e6 Mon Sep 17 00:00:00 2001 From: Master92 Date: Fri, 4 Dec 2020 09:15:58 +0100 Subject: [PATCH] Add batch packing and downloading function --- .../static/js/app/viewmodels/timelapse.js | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/octoprint/static/js/app/viewmodels/timelapse.js b/src/octoprint/static/js/app/viewmodels/timelapse.js index 01009c230..6c7ccebdb 100644 --- a/src/octoprint/static/js/app/viewmodels/timelapse.js +++ b/src/octoprint/static/js/app/viewmodels/timelapse.js @@ -377,6 +377,85 @@ $(function () { ); }; + self.downloadMarkedFiles = function () { + if ( + !self.loginState.hasPermission(self.access.permissions.TIMELAPSE_DOWNLOAD) + ) + return; + + var files = self.markedForFileDeletion(); + var zip = new JSZip(); + + var handler = function (filename) { + let curTimelapse = self.listHelper.allItems.find( + (item) => item.name === _.escape(filename) + ); + return fetch(curTimelapse.url) + .then( + function (content) { + zip.file(_.escape(filename), content.blob()); + deferred.notify( + _.sprintf(gettext("Packed %(filename)s"), { + filename: _.escape(filename) + }), + true + ); + }, + function (reason) { + var short = _.sprintf( + gettext("Packing of %(filename)s failed, continuing..."), + {filename: _.escape(filename)} + ); + var long = _.sprintf( + gettext("Packing of %(filename)s failed: %(error)s"), + { + filename: _.escape(filename), + error: _.escape(reason.responseText) + } + ); + deferred.notify(short, long, false); + } + ) + .finally(function () { + var d = $.Deferred(); + d.resolve.apply(d, arguments); + }); + }; + + var deferred = $.Deferred(); + var promise = deferred.promise(); + + var options = { + title: gettext("Packing files"), + max: files.length, + output: true + }; + var dialog = showProgressModal(options, promise); + + var requests = []; + _.each(files, function (filename) { + let request = handler(filename); + requests.push(request); + }); + + $.when.apply($, requests).done(function () { + deferred.resolve(); + self.requestData(); + }); + + promise.then(function () { + zip.generateAsync({ + type: "blob", + compression: "STORE" + }).then(function (blob) { + saveAs(blob, "OctoPrint-Timelapses.zip"); + dialog.modal("toggle"); + }); + }); + + return promise; + }; + self.markUnrenderedOnPage = function () { self.markedForUnrenderedDeletion( _.uniq(