chore(rest) es2015-ify

This commit is contained in:
coderaiser 2016-12-19 17:59:22 +02:00
parent cffc96ca8a
commit b72f5d06b6

View file

@ -1,28 +1,25 @@
'use strict';
var DIR = './',
DIR_COMMON = DIR + '../../common/',
path = require('path'),
root = require(DIR + 'root'),
config = require(DIR + 'config'),
CloudFunc = require(DIR_COMMON + 'cloudfunc'),
markdown = require(DIR + 'markdown'),
jaguar = require('jaguar/legacy'),
onezip = require('onezip/legacy'),
flop = require('flop'),
pullout = require('pullout/legacy'),
ponse = require('ponse'),
rendy = require('rendy'),
copymitter = require('copymitter'),
json = require('jonny'),
check = require('checkup'),
isWin32 = process.platform === 'win32';
const DIR = './';
const DIR_COMMON = DIR + '../../common/';
const path = require('path');
const root = require(DIR + 'root');
const config = require(DIR + 'config');
const CloudFunc = require(DIR_COMMON + 'cloudfunc');
const markdown = require(DIR + 'markdown');
const jaguar = require('jaguar/legacy');
const onezip = require('onezip/legacy');
const flop = require('flop');
const pullout = require('pullout/legacy');
const ponse = require('ponse');
const rendy = require('rendy');
const copymitter = require('copymitter');
const json = require('jonny');
const check = require('checkup');
const isWin32 = process.platform === 'win32';
/**
* rest interface
@ -31,12 +28,11 @@ var DIR = './',
* @param response
* @param callback
*/
module.exports = function(request, response, next) {
var apiURL, name, is, regExp,
params = {
request : request,
response : response
};
module.exports = (request, response, next) => {
const params = {
request : request,
response : response
};
check
.type('next', next, 'function')
@ -45,39 +41,38 @@ module.exports = function(request, response, next) {
response: response
});
apiURL = CloudFunc.apiURL;
name = ponse.getPathName(request);
regExp = RegExp('^' + apiURL);
is = regExp.test(name);
const apiURL = CloudFunc.apiURL;
const name = ponse.getPathName(request);
const regExp = RegExp('^' + apiURL);
const is = regExp.test(name);
if (!is) {
next();
} else {
params.name = name.replace(apiURL, '') || '/';
if (!is)
return next();
params.name = name.replace(apiURL, '') || '/';
sendData(params, (error, options, data) => {
params.gzip = !error;
sendData(params, function(error, options, data) {
params.gzip = !error;
if (!data) {
data = options;
options = {};
}
if (options.name)
params.name = options.name;
if (options.gzip !== undefined)
params.gzip = options.gzip;
if (options.query)
params.query = options.query;
if (error)
ponse.sendError(error, params);
else
ponse.send(data, params);
});
}
if (!data) {
data = options;
options = {};
}
if (options.name)
params.name = options.name;
if (options.gzip !== undefined)
params.gzip = options.gzip;
if (options.query)
params.query = options.query;
if (error)
return ponse.sendError(error, params);
ponse.send(data, params);
});
};
/**
@ -86,32 +81,33 @@ module.exports = function(request, response, next) {
* @param params {name, method, body, requrest, response}
*/
function sendData(params, callback) {
var p = params,
isMD = RegExp('^/markdown').test(p.name);
const p = params;
const isMD = RegExp('^/markdown').test(p.name);
if (isMD)
markdown(p.name, p.request, function(error, data) {
return markdown(p.name, p.request, (error, data) => {
callback(error, data);
});
else
switch(p.request.method) {
case 'GET':
onGET(params, callback);
break;
switch(p.request.method) {
case 'GET':
onGET(params, callback);
break;
case 'PUT':
pullout(p.request, 'string', (error, body) => {
if (error)
return callback(error);
case 'PUT':
pullout(p.request, 'string', function(error, body) {
if (error)
callback(error);
else
onPUT(p.name, body, callback);
});
break;
}
onPUT(p.name, body, callback);
});
break;
}
}
function onGET(params, callback) {
var cmd, p = params;
let cmd;
const p = params;
if (p.name[0] === '/')
cmd = p.name.replace('/', '');
@ -119,22 +115,23 @@ function onGET(params, callback) {
if (/^pack/.test(cmd)) {
cmd = cmd.replace(/^pack/, '');
streamPack(root(cmd), p.response);
} else {
switch(cmd) {
case '':
p.data = json.stringify({
info: 'Cloud Commander API v1'
});
callback(null, {name: 'api.json'}, p.data);
break;
return;
}
switch(cmd) {
case '':
p.data = json.stringify({
info: 'Cloud Commander API v1'
});
default:
callback({
message: 'Not Found'
});
break;
}
callback(null, {name: 'api.json'}, p.data);
break;
default:
callback({
message: 'Not Found'
});
break;
}
}
@ -146,23 +143,18 @@ function getPackReg() {
}
function streamPack(cmd, response) {
var noop = function() {};
var filename = cmd.replace(getPackReg(), '');
var dir = path.dirname(filename);
var names = [
const noop = () => {};
const filename = cmd.replace(getPackReg(), '');
const dir = path.dirname(filename);
const names = [
path.basename(filename)
];
operation('pack', dir, response, names, noop);
}
/**
* process data on PUT request
*
* @param pParams {command, method, body, requrest, response}
*/
function onPUT(name, body, callback) {
var cmd, files, data, msg;
let cmd;
check
.type('callback', callback, 'function')
@ -174,61 +166,56 @@ function onPUT(name, body, callback) {
if (name[0] === '/')
cmd = name.replace('/', '');
files = json.parse(body);
const files = json.parse(body);
switch(cmd) {
case 'mv':
if (!files.from || !files.to) {
callback(body);
} else if (isRootAll([files.to, files.from])) {
callback(getWin32RootMsg());
} else {
files.from = root(files.from);
files.to = root(files.to);
if (!files.from || !files.to)
return callback(body);
if (isRootAll([files.to, files.from]))
return callback(getWin32RootMsg());
if (files.names)
data = files.names.slice();
else
data = files;
copyFiles(files, flop.move, function(error) {
var msg = formatMsg('move', data);
callback(error, msg);
});
}
files.from = root(files.from);
files.to = root(files.to);
copyFiles(files, flop.move, (error) => {
const data = !files.names ? files : files.names.slice();
const msg = formatMsg('move', data);
callback(error, msg);
});
break;
case 'cp':
if (!files.from || !files.names || !files.to) {
callback(body);
} else if (isRootAll([files.to, files.from])) {
callback(getWin32RootMsg());
} else {
files.from = root(files.from);
files.to = root(files.to);
if (!files.from || !files.names || !files.to)
return callback(body);
if (isRootAll([files.to, files.from]))
return callback(getWin32RootMsg());
msg = formatMsg('copy', files.names);
copy(files.from, files.to, files.names, function(error) {
callback(error, msg);
});
}
files.from = root(files.from);
files.to = root(files.to);
copy(files.from, files.to, files.names, (error) => {
const msg = formatMsg('copy', files.names);
callback(error, msg);
});
break;
case 'pack':
if (!files.from)
callback(body);
else
pack(files.from, files.to, files.names, callback);
return callback(body);
pack(files.from, files.to, files.names, callback);
break;
case 'extract':
if (!files.from)
callback(body);
else
extract(files.from, files.to, callback);
return callback(body);
extract(files.from, files.to, callback);
break;
@ -279,13 +266,13 @@ function operation(op, from, to, names, fn) {
];
}
var packer = getPacker()[op](from, to, names);
const packer = getPacker()[op](from, to, names);
packer.on('error', function(error) {
packer.on('error', error => {
fn(error);
});
packer.on('progress', function(count) {
packer.on('progress', (count) => {
process.stdout.write(rendy('\r{{ operation }} "{{ name }}": {{ count }}%', {
operation : op,
name : names[0],
@ -293,68 +280,64 @@ function operation(op, from, to, names, fn) {
}));
});
packer.on('end', function() {
packer.on('end', () => {
process.stdout.write('\n');
var name = path.basename(from);
var msg = formatMsg(op, name);
const name = path.basename(from);
const msg = formatMsg(op, name);
fn(null, msg);
});
}
function copy(from, to, names, fn) {
var error;
var tmpl = '\r copy {{ from }} {{ to }} {{ count }}%';
var cp = copymitter(from, to, names);
let error;
const cp = copymitter(from, to, names);
cp.on('error', function(e) {
cp.on('error', e => {
error = e;
cp.abort();
});
cp.on('progress', function(count) {
process.stdout.write(rendy(tmpl, {
to: to,
from: from,
count: count
}));
cp.on('progress', (count) => {
process.stdout.write(`\r copy ${from} ${to} ${count}%`);
});
cp.on('end', function() {
cp.on('end', () => {
process.stdout.write('\n');
fn(error);
});
}
function copyFiles(files, processFunc, callback) {
var names = files.names,
let names = files.names;
const copy = () => {
let isLast;
let name;
let from = files.from;
let to = files.to;
copy = function() {
var isLast, name,
from = files.from,
to = files.to;
if (names) {
isLast = !names.length;
name = names.shift();
from += name;
to += name;
} else {
isLast = false;
names = [];
}
if (isLast)
return callback();
processFunc(from, to, error => {
if (error)
return callback(error);
if (names) {
isLast = !names.length;
name = names.shift();
from += name;
to += name;
} else {
isLast = false;
names = [];
}
if (isLast)
callback();
else
processFunc(from, to, function(error) {
if (error)
callback(error);
else
copy();
});
};
copy();
});
};
check
.type('callback', callback, 'function')
@ -367,38 +350,34 @@ function copyFiles(files, processFunc, callback) {
}
function isRootWin32(path) {
var isRoot = path === '/',
isConfig = config('root') === '/';
const isRoot = path === '/';
const isConfig = config('root') === '/';
return isWin32 && isRoot && isConfig;
}
function isRootAll(names) {
var is = names.some(function(name) {
return names.some((name) => {
return isRootWin32(name);
});
return is;
}
function getWin32RootMsg() {
var message = 'Could not copy from/to root on windows!',
error = Error(message);
const message = 'Could not copy from/to root on windows!';
const error = Error(message);
return error;
}
function formatMsg(msgParam, dataParam, status) {
var msg, data,
isObj = typeof dataParam === 'object';
let data;
const isObj = typeof dataParam === 'object';
if (isObj)
data = json.stringify(dataParam);
else
data = dataParam;
msg = CloudFunc.formatMsg(msgParam, data, status);
return msg;
return CloudFunc.formatMsg(msgParam, data, status);
}