Skip to content

Commit

Permalink
lint: update bin and lib folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Lesterpig committed Oct 8, 2015
1 parent 5b452e6 commit dbd8eb1
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 65 deletions.
4 changes: 2 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"node": true,
"mocha": true,
}
"mocha": true
}
13 changes: 9 additions & 4 deletions bin/youtransfer
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/usr/bin/env node

var path = require("path");
var fs = require("fs-extra");
'use strict';

var path = require('path');
var fs = require('fs-extra');
var program = require('commander');

program.version('1.0.4')
.arguments('<init>', 'Initialise a YouTransfer project')
.action(function(cmd) {
if(cmd == "init") {
fs.copy(path.join(__dirname,"../"), process.cwd(), function(err) {
if(cmd == 'init') {
fs.copy(path.join(__dirname,'../'), process.cwd(), function(err) {
if(err) {
return console.error('Unable to initialize YouTransfer\n' + err);
}
console.log('YouTransfer project initialised succesfully!');
console.log('Make sure to run "npm init" and "npm install" to setup your project.');
console.log('Afterwards, you can start the project by running "npm start"');
Expand Down
6 changes: 3 additions & 3 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ Errors.prototype.register = function(error, code) {
} else if(_.isObject(item)) {
err = _.assign(err, item);
} else {
throw new Error("Invalid argument exception");
throw new Error('Invalid argument exception');
}

err.code = item.code || (_.isNaN(parseInt(key)) ? key : "default");
err.code = item.code || (_.isNaN(parseInt(key)) ? key : 'default');
self.registered[err.code] = err;
});
} else {
Expand All @@ -49,7 +49,7 @@ Errors.prototype.register = function(error, code) {
code: code || (error.code ? error.code : 'default')
});
} else {
throw new Error("Invalid argument exception");
throw new Error('Invalid argument exception');
}

self.registered[error.code] = error;
Expand Down
14 changes: 7 additions & 7 deletions lib/localstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// ------------------------------------------------------------------------------------------ Dependencies

require('date-utils');
var fs = require("fs");
var del = require("del");
var fs = require('fs');
var del = require('del');
var path = require('path');
var mime = require('mime');
var _ = require("lodash");
var _ = require('lodash');
var archiver = require('archiver');
var crypto = require('crypto');

Expand All @@ -28,7 +28,7 @@ function LocalFileStorage(options) {
this.encryptionEnabled = options.encryptionEnabled || false;
this.encryptionKey = options.encryptionKey;
} else {
throw new Error("Invalid options provided");
throw new Error('Invalid options provided');
}
}

Expand Down Expand Up @@ -122,10 +122,10 @@ LocalFileStorage.prototype.archive = function(token, res, next) {
archive.pipe(res);
archive.finalize();
});

_.each(bundle.files, function(file) {

var filePath = path.join(basedir, file.id + '.binary')
var filePath = path.join(basedir, file.id + '.binary');
if (filePath.indexOf(basedir) !== 0) {
throw new Error('Invalid token provided');
}
Expand Down Expand Up @@ -207,7 +207,7 @@ LocalFileStorage.prototype.download = function(token, res, next) {
}
});
} else {
throw new Error("invalid token exception");
throw new Error('invalid token exception');
}
} catch (err) {
next(err);
Expand Down
32 changes: 16 additions & 16 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@
var _ = require('lodash');
var url = require('url');
var nconf = require('nconf');
var nunjucks = require("nunjucks");
var nunjucks = require('nunjucks');
var youtransfer = require('./youtransfer');

// ------------------------------------------------------------------------------------------ Module definition

module.exports = function(req, res, next) {

// Fix for missing redirect function in Restify
res.redirect = function(addr) {
res.header('Location', addr);
res.send(302);
res.redirect = function(addr) {
res.header('Location', addr);
res.send(302);
};

// Add XmlHttpRequest property to request object
req.isXMLHttpRequest = (req.headers['x-requested-with'] !== null && req.headers['x-requested-with'] === 'XMLHttpRequest');

// Initializing Nunjucks template engine + adding it to Restify
var viewEngine = nunjucks.configure([
'./src/views/',
'./src/views/partials',
'./src/views/pages',
'./src/views/errors',
'./src/views/',
'./src/views/partials',
'./src/views/pages',
'./src/views/errors',
'./src/templates/'
],
],
{
autoescape: true,
watch: (nconf.get('NODE_ENV') !== "production")
watch: (nconf.get('NODE_ENV') !== 'production')
}
);

Expand Down Expand Up @@ -90,18 +90,18 @@ module.exports = function(req, res, next) {
res.writeHead(200);
res.end(output);
} else {
throw new Error("The selected template is not a page, throwing 'template not found' error for proper handling");
throw new Error('The selected template is not a page, throwing "template not found" error for proper handling');
}
} catch (err) {
if(err.message.match(/template not found/)) {
try {
var output = nunjucks.render("404.html", context);
var output = nunjucks.render('404.html', context);
res.setHeader('Content-type', 'text/html');
res.writeHead(404);
res.end(output);
} catch(err) {
res.writeHead(404);
res.end("Resource not found");
res.end('Resource not found');
}
} else {
res.writeHead(500);
Expand All @@ -126,16 +126,16 @@ module.exports = function(req, res, next) {
res.renderTemplate(name, context, function(err, output) {
context.output = output;
res.json(context);
if(callback) {
callback();
if(callback) {
callback();
}
});
} else {
res.render(name, context, callback);
}
}

};
};

next();
};
33 changes: 17 additions & 16 deletions lib/routes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';

// ------------------------------------------------------------------------------------------ Dependencies

var fs = require('fs');
var _ = require("lodash");
var _ = require('lodash');
var uuid = require('uuid');
var nconf = require('nconf');
var nstatic = require('node-static');
var validator = require('validator');
var youtransfer = require("./youtransfer");
var youtransfer = require('./youtransfer');

// ------------------------------------------------------------------------------------------ Module Exposure

Expand All @@ -26,7 +27,7 @@ function Router(options) {
} else if(_.isObject(this.options)) {
this.fileServer = options.fileServer || new nstatic.Server('./dist');
} else {
throw "Invalid options provided";
throw 'Invalid options provided';
}
}

Expand Down Expand Up @@ -76,11 +77,11 @@ Router.prototype.uploadBundle = function() {
var bundle = JSON.parse(req.params.bundle);
youtransfer.bundle(bundle, function(err) {
req.errors.parse(err);
res.process("index.html", null, next);
res.process('index.html', null, next);
});
} catch (err) {
req.errors.parse(err);
res.process("index.html", null, next);
res.process('index.html', null, next);
}
};
};
Expand All @@ -90,7 +91,7 @@ Router.prototype.send = function() {
req.errors.register(new Error('An error occurred while sending your message'));
youtransfer.send(req, res, function(err) {
req.errors.parse(err);
res.process("index.html", null, next);
res.process('index.html', null, next);
});
};
};
Expand All @@ -101,18 +102,18 @@ Router.prototype.download = function() {
req.errors.register([
{
code: 'ENOENT',
message: "Oh no... there is no file or archive to match this token! Are you sure you've entered the right one?",
html: "Oh no... there is no file or archive to match this token!<br />Are you sure you've entered the right one?"
message: 'Oh no... there is no file or archive to match this token! Are you sure you\'ve entered the right one?',
html: 'Oh no... there is no file or archive to match this token!<br />Are you sure you\'ve entered the right one?'
},
{
message: "Oh my... something went terribly wrong!",
message: 'Oh my... something went terribly wrong!',
}
]);

var token = req.params.token;
var callback = function(err) {
req.errors.parse(err);
res.process("download.html", null, next);
res.process('download.html', null, next);
};

if(validator.isUUID(token)) {
Expand All @@ -132,7 +133,7 @@ Router.prototype.settingsRedirect = function() {

Router.prototype.settingsFinalise = function() {
return function(req, res, next) {
req.errors.register(new Error("An unknown error occurred while finalising the settings. Please try again."));
req.errors.register(new Error('An unknown error occurred while finalising the settings. Please try again.'));

var settings = _.assign(req.params.settings, {
finalised: true
Expand All @@ -141,7 +142,7 @@ Router.prototype.settingsFinalise = function() {
youtransfer.settings.push(settings, function(err) {
if(err) {
req.errors.parse(err);
res.process("settings.finalise.html", null, next);
res.process('settings.finalise.html', null, next);
} else {
res.redirect('/');
}
Expand All @@ -162,7 +163,7 @@ Router.prototype.settingsUnlock = function() {
req.errors.parse(err);
try {
if(settings.unlockCode === req.params.unlockCode) {
youtransfer.settings.push({
youtransfer.settings.push({
finalised: false,
unlockCode: false
}, function(err) {
Expand Down Expand Up @@ -295,7 +296,7 @@ Router.prototype.settingsGetByName = function() {
Router.prototype.settingsSaveByName = function() {
var self = this;
return function(req, res, next) {

if(req.params.name === 'template') {
self.settingsSaveTemplate()(req, res, next);
} else {
Expand Down Expand Up @@ -333,7 +334,7 @@ Router.prototype.settingsSaveByName = function() {

Router.prototype.staticFiles = function() {
var self = this;
return function(req, res) {
return function(req, res) {
self.fileServer.serveFile('/' + req.params[1] + '/' + req.params[2], 200, { server: 'youtransfer.io', 'Cache-Control': 'max-age=' + nconf.get('CACHE_MAX_AGE') }, req, res);
};
};
Expand All @@ -342,6 +343,6 @@ Router.prototype.default = function() {
return function(req, res, next) {
var page = req.params[0] || 'index';
res.setHeader('Cache-Control', 'private, max-age=0, proxy-revalidate, no-store, no-cache, must-revalidate');
res.process(page + ".html", null, next);
res.process(page + '.html', null, next);
};
};
16 changes: 8 additions & 8 deletions lib/s3storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// ------------------------------------------------------------------------------------------ Dependencies

require('date-utils');
var fs = require("fs");
var _ = require("lodash");
var fs = require('fs');
var _ = require('lodash');
var zlib = require('zlib');
var aws = require('aws-sdk');
var archiver = require('archiver');
Expand Down Expand Up @@ -32,7 +32,7 @@ function S3Storage(options) {
}
});
} else {
throw "Invalid options provided";
throw 'Invalid options provided';
}
}

Expand Down Expand Up @@ -69,7 +69,7 @@ S3Storage.prototype.upload = function(file, context, next) {
Metadata: {
json: JSON.stringify(context)
},
}, function(err) {
}, function(err) {
next(err, context);
});
} catch(err) {
Expand All @@ -83,7 +83,7 @@ S3Storage.prototype.bundle = function(bundle, next) {
s3obj.upload({
Key: bundle.id,
Body: JSON.stringify(bundle)
}, function(err) {
}, function(err) {
next(err);
});
} catch(err) {
Expand All @@ -109,7 +109,7 @@ S3Storage.prototype.archive = function(token, res, next) {
throw new Error('The requested bundle is no longer available.');
}
}

if(bundle.files) {
res.setHeader('Content-disposition', 'attachment; filename="bundle.zip"');
res.setHeader('Content-type', 'application/octet-stream');
Expand All @@ -126,7 +126,7 @@ S3Storage.prototype.archive = function(token, res, next) {
archive.append(s3obj.getObject({
Key: file.id
}).createReadStream().pipe(gunzip), { name: file.name});
completed();
completed();
});
} else {
throw 'Invalid bundle data';
Expand Down Expand Up @@ -210,7 +210,7 @@ S3Storage.prototype.purge = function(next) {
});
});
});
} catch(err) {
} catch(err) {
next(err, filesToDelete);
}
};
6 changes: 3 additions & 3 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

// ------------------------------------------------------------------------------------------ Dependencies

var fs = require("fs");
var fs = require('fs');
var path = require('path');
var _ = require("lodash");
var _ = require('lodash');
var nconf = require('nconf');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
Expand All @@ -25,7 +25,7 @@ function Settings(opt) {
encoding: 'utf-8'
}, opt);
} else {
throw "Invalid options provided";
throw 'Invalid options provided';
}
}

Expand Down
Loading

0 comments on commit dbd8eb1

Please sign in to comment.