Skip to content

Commit 651c569

Browse files
committed
fix(utils-rework): Changes exported module + corrects bugs in media
1 parent 507cc8d commit 651c569

File tree

4 files changed

+59
-39
lines changed

4 files changed

+59
-39
lines changed

index.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
'use strict';
22

3-
module.exports = config => {
4-
const main = (module.exports = {});
3+
const validate = require('./lib/validate');
4+
const media = require('./lib/media');
5+
const crypto = require('./lib/crypto');
6+
const Mailer = require('./lib/mailer');
7+
const templater = require('./lib/templater');
8+
const Hoek = require('hoek');
59

6-
main.validate = require('./lib/validate');
7-
main.media = require('./lib/media')(config.media);
8-
main.crypto = require('./lib/crypto')(config.algorythm);
9-
main.Mailer = require('./lib/mailer');
10-
main.templater = require('./lib/templater');
10+
let initialized = false;
1111

12-
return main;
12+
module.exports = {
13+
init(config) {
14+
Hoek.assert(initialized === false, 'You should call init() only once.');
15+
16+
this.validate = validate;
17+
this.media = media(config.media);
18+
this.crypto = crypto(config.algorythm);
19+
this.Mailer = Mailer;
20+
this.templater = templater;
21+
22+
initialized = true;
23+
},
1324
};

lib/crypto.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
const crypto = require('crypto');
44
const Promise = require('bluebird');
55

6-
module.exports = algorithm => {
6+
module.exports = algo => {
77
const randomBytesAsync = Promise.promisify(crypto.randomBytes);
88

99
return {
1010
/**
1111
* Generates a cryptographic hash.
1212
* @param {INTEGER} seedSize Size of the seed used for the hash generation.
1313
* @param {string} data The data to be added to the hash.
14-
* @return {promise} The promise containing the a hash string
14+
* @param {string} algorithm - The algorithm to use for creating the hash.
15+
* @return {promise} The promise containing the hash string
1516
* in hexadecimal format if resolved.
1617
*/
17-
getSecuredHash(seedSize, data) {
18+
getSecuredHash(seedSize, data, algorithm = algo) {
1819
return randomBytesAsync(seedSize).then(seed => {
1920
const hash = crypto
2021
.createHash(algorithm)

lib/mailer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ const nodemailer = require('nodemailer');
55
module.exports = Mailer;
66

77
function Mailer(transOptions) {
8-
let transporter;
9-
8+
let transporter = null;
109
this.sendMail = sendMail;
1110

1211
init();

lib/media.js

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,27 @@ const fse = require('fs-extra');
77
const Q = require('q');
88

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

21-
if (path.extname(fileName) === '' && stream.hapi.headers['content-type']) {
22-
const [, ext] = stream.hapi.headers['content-type'].split('/');
23-
imgPath = `${imgPath}.${ext}`;
20+
if (mergedOpts.addTimestamp === true) {
21+
const timestamp = new Date().getTime().toString();
22+
imgPath = `${imgPath}${timestamp}-${file}`;
23+
} else {
24+
imgPath = `${imgPath}${file}`;
25+
}
26+
27+
if (fileExtension === '' && mergedOpts.ext !== null) {
28+
imgPath = `${imgPath}.${mergedOpts.ext}`;
29+
} else {
30+
imgPath = `${imgPath}${fileExtension}`;
2431
}
2532

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

47-
createFile(stream, directoryPath, addTimestamp = true) {
54+
createFile(stream, filename, directoryPath, options = {}) {
55+
const defaultOpts = { addTimestamp: true, ext: null };
56+
const mergedOpts = Object.assign(defaultOpts, options);
4857
const deferred = Q.defer();
49-
let fileExtension = path.extname(stream.hapi.filename);
50-
const fileName = slug(
51-
stream.hapi.filename.substr(0, stream.hapi.filename.length - fileExtension.length)
52-
);
58+
const fileExtension = path.extname(filename);
59+
const file = slug(filename.substr(0, filename.length - fileExtension.length));
5360
const directoryBase = `${config.publicFileFolder}/${directoryPath}`;
5461
let filePath = `${directoryBase}/`;
5562

56-
if (addTimestamp === true) {
63+
if (mergedOpts.addTimestamp === true) {
5764
const timestamp = new Date().getTime().toString();
58-
filePath = `${filePath}${timestamp}-${fileName}`;
65+
filePath = `${filePath}${timestamp}-${file}`;
5966
} else {
60-
filePath = `${filePath}${fileName}`;
67+
filePath = `${filePath}${file}`;
6168
}
6269

63-
if (fileExtension === '' && stream.hapi.headers['content-type']) {
64-
const [, ext] = stream.hapi.headers['content-type'].split('/');
65-
filePath = `${filePath}.${ext}`;
70+
if (fileExtension === '' && mergedOpts.ext !== null) {
71+
filePath = `${filePath}.${mergedOpts.ext}`;
6672
} else {
6773
filePath = `${filePath}${fileExtension}`;
6874
}
@@ -104,10 +110,11 @@ module.exports = config => {
104110
/**
105111
* Create an archive from a list of path
106112
* @param {string} archiveName Archive name
113+
* @param {string} directoryPath Path to the directory where to create the archive
107114
* @param {array of string} paths File paths array
108-
* @param {object} request Hapi's request object for log matter
115+
* @param {function} log logger function
109116
*/
110-
createArchive(archiveName, directoryPath, paths, request) {
117+
createArchive(archiveName, directoryPath, paths, log) {
111118
const archiveDest = path.resolve(config.publicFileFolder, directoryPath);
112119
const zipPath = path.resolve(archiveDest, archiveName);
113120
const archive = archiver('zip', { zlib: { level: 9 } });
@@ -126,14 +133,14 @@ module.exports = config => {
126133
});
127134

128135
archive.on('end', () => {
129-
request.log(['log'], `Archive "${archiveName}" of ${archive.pointer()} bytes written`);
136+
log(['log'], `Archive "${archiveName}" of ${archive.pointer()} bytes written`);
130137
resolve(zipPath);
131138
});
132139

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

139146
output.close();
@@ -159,4 +166,6 @@ module.exports = config => {
159166
});
160167
},
161168
};
169+
170+
return mediaObj;
162171
};

0 commit comments

Comments
 (0)