Skip to content

Commit

Permalink
Separate attachment-from-file function
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Jun 1, 2017
1 parent cbb310a commit 3e52b4f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
24 changes: 24 additions & 0 deletions src/lib/attachment-from-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require('./sync-fs');

module.exports = path => {
const data = fs.readBinary(path);
const mime = mimeTypeFor(path);
return {
content_type: mime,
data: new Buffer(data),
};
};

function mimeTypeFor(fileName) {
const extensionStart = fileName.indexOf('.');
const extension = extensionStart === -1 ?
fileName :
fileName.substring(extensionStart+1);

switch(extension) {
case 'json': return 'application/json';
case 'png' : return 'image/png';
case 'xml' : return 'application/xml';
default: throw new Error(`Unrecongised file extension: ${extension}`);
}
}
22 changes: 2 additions & 20 deletions src/lib/attachments-from-dir.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const attachmentFromFile = require('./attachment-from-file');
const fs = require('./sync-fs');
const warn = require('./log').warn;

Expand All @@ -8,26 +9,7 @@ module.exports = dir => {
fs.readdir(dir)
.forEach(fileName => {
const filePath = `${dir}/${fileName}`;
const data = fs.readBinary(filePath);
const mime = mimeTypeFor(fileName);
attachments[fileName] = {
content_type: mime,
data: new Buffer(data),
};
attachments[fileName] = attachmentFromFile(filePath);
});
return attachments;
};

function mimeTypeFor(fileName) {
const extensionStart = fileName.indexOf('.');
const extension = extensionStart === -1 ?
fileName :
fileName.substring(extensionStart+1);

switch(extension) {
case 'json': return 'application/json';
case 'png' : return 'image/png';
case 'xml' : return 'application/xml';
default: throw new Error(`Unrecongised file extension: ${extension}`);
}
}

0 comments on commit 3e52b4f

Please sign in to comment.