Skip to content

Commit

Permalink
fix(utils-rework): Changes exported module + corrects bugs in media
Browse files Browse the repository at this point in the history
  • Loading branch information
roux1max committed Oct 7, 2017
1 parent 507cc8d commit 651c569
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 39 deletions.
27 changes: 19 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
'use strict';

module.exports = config => {
const main = (module.exports = {});
const validate = require('./lib/validate');
const media = require('./lib/media');
const crypto = require('./lib/crypto');
const Mailer = require('./lib/mailer');
const templater = require('./lib/templater');
const Hoek = require('hoek');

main.validate = require('./lib/validate');
main.media = require('./lib/media')(config.media);
main.crypto = require('./lib/crypto')(config.algorythm);
main.Mailer = require('./lib/mailer');
main.templater = require('./lib/templater');
let initialized = false;

return main;
module.exports = {
init(config) {
Hoek.assert(initialized === false, 'You should call init() only once.');

this.validate = validate;
this.media = media(config.media);
this.crypto = crypto(config.algorythm);
this.Mailer = Mailer;
this.templater = templater;

initialized = true;
},
};
7 changes: 4 additions & 3 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
const crypto = require('crypto');
const Promise = require('bluebird');

module.exports = algorithm => {
module.exports = algo => {
const randomBytesAsync = Promise.promisify(crypto.randomBytes);

return {
/**
* Generates a cryptographic hash.
* @param {INTEGER} seedSize Size of the seed used for the hash generation.
* @param {string} data The data to be added to the hash.
* @return {promise} The promise containing the a hash string
* @param {string} algorithm - The algorithm to use for creating the hash.
* @return {promise} The promise containing the hash string
* in hexadecimal format if resolved.
*/
getSecuredHash(seedSize, data) {
getSecuredHash(seedSize, data, algorithm = algo) {
return randomBytesAsync(seedSize).then(seed => {
const hash = crypto
.createHash(algorithm)
Expand Down
3 changes: 1 addition & 2 deletions lib/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const nodemailer = require('nodemailer');
module.exports = Mailer;

function Mailer(transOptions) {
let transporter;

let transporter = null;
this.sendMail = sendMail;

init();
Expand Down
61 changes: 35 additions & 26 deletions lib/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@ const fse = require('fs-extra');
const Q = require('q');

module.exports = config => {
return {
createImage(stream, filePath) {
const mediaObj = {
createImage(stream, filename, filePath, options = {}) {
const defaultOpts = { addTimestamp: true, ext: null };
const mergedOpts = Object.assign(defaultOpts, options);
const deferred = Q.defer();
const timestamp = new Date().getTime().toString();
const fileExtension = path.extname(stream.hapi.filename);
const fileName = slug(
stream.hapi.filename.substr(0, stream.hapi.filename.length - fileExtension.length)
);
const fileExtension = path.extname(filename);
const file = slug(filename.substr(0, filename.length - fileExtension.length));
const directoryBase = `${config.publicImgFolder}/${filePath}`;
let imgPath = `${directoryBase}/${timestamp + fileName}`;
let imgPath = `${directoryBase}/`;

if (path.extname(fileName) === '' && stream.hapi.headers['content-type']) {
const [, ext] = stream.hapi.headers['content-type'].split('/');
imgPath = `${imgPath}.${ext}`;
if (mergedOpts.addTimestamp === true) {
const timestamp = new Date().getTime().toString();
imgPath = `${imgPath}${timestamp}-${file}`;
} else {
imgPath = `${imgPath}${file}`;
}

if (fileExtension === '' && mergedOpts.ext !== null) {
imgPath = `${imgPath}.${mergedOpts.ext}`;
} else {
imgPath = `${imgPath}${fileExtension}`;
}

// make sure the path exists so we can write to it
Expand All @@ -44,25 +51,24 @@ module.exports = config => {
return deferred.promise;
},

createFile(stream, directoryPath, addTimestamp = true) {
createFile(stream, filename, directoryPath, options = {}) {
const defaultOpts = { addTimestamp: true, ext: null };
const mergedOpts = Object.assign(defaultOpts, options);
const deferred = Q.defer();
let fileExtension = path.extname(stream.hapi.filename);
const fileName = slug(
stream.hapi.filename.substr(0, stream.hapi.filename.length - fileExtension.length)
);
const fileExtension = path.extname(filename);
const file = slug(filename.substr(0, filename.length - fileExtension.length));
const directoryBase = `${config.publicFileFolder}/${directoryPath}`;
let filePath = `${directoryBase}/`;

if (addTimestamp === true) {
if (mergedOpts.addTimestamp === true) {
const timestamp = new Date().getTime().toString();
filePath = `${filePath}${timestamp}-${fileName}`;
filePath = `${filePath}${timestamp}-${file}`;
} else {
filePath = `${filePath}${fileName}`;
filePath = `${filePath}${file}`;
}

if (fileExtension === '' && stream.hapi.headers['content-type']) {
const [, ext] = stream.hapi.headers['content-type'].split('/');
filePath = `${filePath}.${ext}`;
if (fileExtension === '' && mergedOpts.ext !== null) {
filePath = `${filePath}.${mergedOpts.ext}`;
} else {
filePath = `${filePath}${fileExtension}`;
}
Expand Down Expand Up @@ -104,10 +110,11 @@ module.exports = config => {
/**
* Create an archive from a list of path
* @param {string} archiveName Archive name
* @param {string} directoryPath Path to the directory where to create the archive
* @param {array of string} paths File paths array
* @param {object} request Hapi's request object for log matter
* @param {function} log logger function
*/
createArchive(archiveName, directoryPath, paths, request) {
createArchive(archiveName, directoryPath, paths, log) {
const archiveDest = path.resolve(config.publicFileFolder, directoryPath);
const zipPath = path.resolve(archiveDest, archiveName);
const archive = archiver('zip', { zlib: { level: 9 } });
Expand All @@ -126,14 +133,14 @@ module.exports = config => {
});

archive.on('end', () => {
request.log(['log'], `Archive "${archiveName}" of ${archive.pointer()} bytes written`);
log(['log'], `Archive "${archiveName}" of ${archive.pointer()} bytes written`);
resolve(zipPath);
});

// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', err => {
if (err.code === 'ENOENT') {
return request.log(['warning', 'archive'], err);
return log(['warning', 'archive'], err);
}

output.close();
Expand All @@ -159,4 +166,6 @@ module.exports = config => {
});
},
};

return mediaObj;
};

0 comments on commit 651c569

Please sign in to comment.