From 9a6626aca67b298ff9504eee4cbf057dfd756316 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 19 Jun 2014 09:58:08 -0400 Subject: [PATCH] feature(flop) add write --- lib/server/flop.js | 57 ++++++++++++++++++++++++++++++++++-- lib/server/rest.js | 23 +++++---------- lib/server/rest/fs/delete.js | 3 +- lib/server/rest/fs/put.js | 9 ++---- 4 files changed, 65 insertions(+), 27 deletions(-) diff --git a/lib/server/flop.js b/lib/server/flop.js index 54bca2c9..27a61ba9 100644 --- a/lib/server/flop.js +++ b/lib/server/flop.js @@ -96,8 +96,32 @@ } }; - exports.write = function() { + exports.write = function(path, data, option, callback) { + var optionsPipe, + type = Util.getType(data); + Util.checkArgs(arguments, ['path', 'data', 'callback']); + + if (!callback) + callback = option; + + if (!type) + type = ''; + + switch(type) { + case 'string': + fs.writeFile(path, data, callback); + break; + + case 'object': + optionsPipe = { + gunzip : option === 'unzip', + gzip : option === 'zip' + }; + + pipe.create(data, path, optionsPipe, callback); + break; + } }; exports.delete = function(path, callback) { @@ -127,8 +151,35 @@ }); }; - exports.cp = function(path, callback) { - (ncp || pipe.create)(path, callback); + exports.cp = function(from, to, type, callback) { + var options; + + if (!callback) { + callback = type; + type = null; + } + + switch(type) { + default: + (ncp || pipe.create)(from, to, callback); + break; + + case 'zip': + options = { + gzip : true + }; + + pipe.create(from, to, options, callback); + break; + + case 'unzip': + options = { + gunzip : true + }; + + pipe.create(from, to, options, callback); + break; + } }; function tryRequire(name) { diff --git a/lib/server/rest.js b/lib/server/rest.js index fa9fe278..88485d5f 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -251,7 +251,7 @@ * @param pParams {command, method, body, requrest, response} */ function onPUT(params) { - var p, cmd, files, name, json, config, data, read, write, options, + var p, cmd, files, name, json, config, data, from, to, options, ret = main.checkParams(params, ['body']); if (ret) { @@ -320,14 +320,10 @@ if (!files.from) { sendError(params, p.data); } else { - read = files.from, - write = files.to || files.from + '.zip'; + from = files.from; + to = files.to || from + '.zip'; - options = { - gzip: true - }; - - pipe.create(read, write, options, function(error) { + flop.cp(from, to, 'zip', function(error) { var name = path.basename(files.from); if (error) @@ -342,14 +338,11 @@ if (!files.from) { sendError(params, p.data); } else { - read = files.from; - write = files.to || Util.rmStrOnce(files.from, ['.zip', '.gzip']); - options = { - gunzip: true - }, + from = files.from; + to = files.to || Util.rmStrOnce(files.from, ['.zip', '.gzip']); - pipe.create(read, write, options, function(error) { - var name = path.basename(read); + flop.cp(from, to, 'unzip', function(error) { + var name = path.basename(files.from); if (error) sendError(params, error); diff --git a/lib/server/rest/fs/delete.js b/lib/server/rest/fs/delete.js index 24f22a48..cb94b39a 100644 --- a/lib/server/rest/fs/delete.js +++ b/lib/server/rest/fs/delete.js @@ -1,8 +1,7 @@ (function(){ 'use strict'; - var - DIR = '../../../', + var DIR = '../../../', DIR_SERVER = DIR + 'server/', Util = require(DIR + 'util'), flop = require(DIR_SERVER + 'flop'); diff --git a/lib/server/rest/fs/put.js b/lib/server/rest/fs/put.js index 2b53e33d..3318fc1f 100644 --- a/lib/server/rest/fs/put.js +++ b/lib/server/rest/fs/put.js @@ -15,8 +15,7 @@ exports.onPut = onPut; function onPut(name, query, readStream, callback) { - var options, - func = Util.exec.ret(callback), + var func = Util.exec.ret(callback), baseName = path.basename(name); switch(query) { @@ -33,11 +32,7 @@ break; default: - options = { - gunzip : query === 'unzip' - }; - - pipe.create(readStream, name, options, function(error) { + flop.write(name, readStream, 'unzip', function(error) { var msg; if (!error)