From cc8693be60a9b5790a42b9014ea9a186d927fab0 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 2 Jul 2015 10:58:28 -0400 Subject: [PATCH] feature(cloudcmd) jag -> jaguar: add ability to pack couple files in one directory --- lib/client/dom.js | 82 ++++++++++++++++------------- lib/client/menu.js | 10 +--- lib/server/rest.js | 126 +++++++++++++++++++++++++++++++++------------ package.json | 2 +- 4 files changed, 140 insertions(+), 80 deletions(-) diff --git a/lib/client/dom.js b/lib/client/dom.js index 3d5e4638..44280ff9 100644 --- a/lib/client/dom.js +++ b/lib/client/dom.js @@ -460,46 +460,57 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; }); } - function twopack(current, operation) { + function twopack(operation) { var op, - nameDir = '', - nameFile = '', RESTful = DOM.RESTful, Images = DOM.Images, - name = Cmd.getCurrentName(current), - dir = Cmd.getCurrentDirPath(), - path = dir + name, - fileFrom = { - from : path - }; + Info = DOM.CurrentInfo, + name = Info.name, + path = Info.path, + dirPath = Info.dirPath, + activeFiles = DOM.getActiveFiles(), + names = DOM.getSelectedNames(activeFiles), + fileFrom; Util.check(arguments, ['operation']); - if (name === '..') { + if (!names.length) { Dialog.alert('No files selected!'); } else { - if (operation.pack) { - op = RESTful.pack; - nameDir = name + '.tar.gz'; - nameFile = name + '.gz'; - } else if (operation.unpack) { + switch(operation) { + case 'unpack': op = RESTful.unpack; - nameDir = name.replace('.tar.gz', ''); - nameFile = name.replace('.gz', ''); + + fileFrom = { + from : path, + to : dirPath + }; + + name = name.replace(/\.tar\.gz$/, ''); + + break; + + case 'pack': + op = RESTful.pack; + name += '.tar.gz'; + + fileFrom = { + from : dirPath, + to : dirPath, + names : names + }; + break; } - Images.show.load(); + Images.show.load('top'); - if (name && name !== '..') - op(fileFrom, function() { - CloudCmd.refresh(null, function() { - var byName = DOM.getCurrentByName, - dir = byName(nameDir), - file = byName(nameFile); - - DOM.setCurrentFile(dir || file); - }); + op(fileFrom, function() { + CloudCmd.refresh(null, function() { + var file = DOM.getCurrentByName(name); + + DOM.setCurrentFile(file); }); + }); } } @@ -507,20 +518,16 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; * zip file * */ - this.pack = function(current) { - twopack(current, { - pack: true - }); + this.pack = function() { + twopack('pack'); }; /** * unzip file * */ - this.unpack = function(current) { - twopack(current, { - unpack: true - }); + this.unpack = function() { + twopack('unpack'); }; /** @@ -622,9 +629,10 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog; this.getActiveFiles = function() { var current = DOM.getCurrentFile(), files = DOM.getSelectedFiles(), - selected = ~files.indexOf(current); + selected = ~files.indexOf(current), + name = DOM.getCurrentName(current); - if (!selected) + if (!selected && name !== '..') files.push(current); return files; diff --git a/lib/client/menu.js b/lib/client/menu.js index 5e3adbe1..67ad4968 100644 --- a/lib/client/menu.js +++ b/lib/client/menu.js @@ -171,8 +171,8 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO; setTimeout(DOM.renameCurrent, 100); }, 'Delete' : Operation.show.bind(null, 'delete'), - 'Pack' : getActiveFunc(DOM.pack), - 'Unpack' : getActiveFunc(DOM.unpack), + 'Pack' : DOM.pack, + 'Unpack' : DOM.unpack, 'Upload To' : {}, 'Download' : download, 'Cut' : Buffer.cut, @@ -305,12 +305,6 @@ var CloudCmd, Util, DOM, CloudFunc, MenuIO; }); } - function getActiveFunc(callback) { - return function() { - DOM.getActiveFiles().forEach(callback); - }; - } - function getCurrentPosition() { var current = Info.element, rect = current.getBoundingClientRect(); diff --git a/lib/server/rest.js b/lib/server/rest.js index 19884c41..5e412ae4 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -23,7 +23,7 @@ markdown = require(DIR + 'rest/markdown'), github = require('faust'), - packer = require('jag'), + jaguar = require('jaguar'), flop = require('flop'), pipe = require('pipe-io'), @@ -173,7 +173,7 @@ * @param pParams {command, method, body, requrest, response} */ function onPUT(name, body, callback) { - var cmd, files, data, from, to, msg; + var cmd, files, data, msg; check .type('callback', callback, 'function') @@ -237,43 +237,17 @@ break; case 'pack': - if (!files.from) { + if (!files.from) callback(body); - } else { - from = root(files.from); - - if (files.to) - to = root(files.to); - else - to = from + '.gz'; - - packer.pack(from, to, function(error) { - var name = path.basename(files.from), - msg = formatMsg('pack', name); - - callback(error, msg); - }); - } + else + pack(files.from, files.to, files.names, callback); break; case 'unpack': - if (!files.from) { + if (!files.from) callback(body); - } else { - from = root(files.from); - - if (files.to) - to = root(files.to); - else - to = files.from.replace(/(\.gz|\.tar\.gz)$/, ''); - - packer.unpack(from, to, function(error) { - var name = path.basename(files.from), - data = formatMsg('unpack', name); - - callback(error, data); - }); - } + else + unpack(files.from, files.to, callback); break; @@ -283,6 +257,90 @@ } } + function pack(from, to, names, fn) { + var name; + + from = root(from); + + if (to) + to = root(to); + else + to = from; + + if (names.length > 1) { + name = path.basename(to); + } else { + name = names[0]; + } + + to = path.join(to, name); + + if (!/\.tar\.gz$/.test(to)) { + to += '.tar.gz'; + } + + if (!names) { + names = [ + path.basename(from) + ]; + + from = path.dirname(from); + } + + operation('pack', from, to, names, fn); + } + + function unpack(from, to, fn) { + from = root(from); + + if (to) + to = root(to); + else + to = from.replace(/\.tar\.gz$/, ''); + + operation('extract', from, to, fn); + } + + function operation(op, from, to, names, fn) { + var packer, wasError; + + if (!fn) { + fn = names; + names = [ + path.basename(from) + ]; + } + + console.log(from, to, names); + + packer = jaguar[op](from, to, names); + + packer.on('error', function(error) { + wasError = true; + fn(error); + }); + + packer.on('progress', function(count) { + process.stdout.write(rendy('\r{{ operation }} "{{ name }}": {{ count }}%', { + operation : op, + name : names[0], + count : count + })); + }); + + packer.on('end', function() { + var name, msg; + + process.stdout.write('\n'); + + if (!wasError) { + name = path.basename(from), + msg = formatMsg(op, name); + fn(null, msg); + } + }); + } + function copy(from, to, names, fn) { var error, tmpl = '\r copy {{ from }} {{ to }} {{ count }}%', diff --git a/package.json b/package.json index 55e29003..8b85b039 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "flop": "~1.3.0", "format-io": "~0.9.6", "http-auth": "~2.2.3", - "jag": "~1.0.0", + "jaguar": "~1.0.2", "join-io": "~1.4.0", "jonny": "~1.0.0", "markdown-it": "~4.3.0",