Skip to content

Commit

Permalink
Merge pull request #505 from spelcaster/i504
Browse files Browse the repository at this point in the history
Attachment helper for base64 encoding
  • Loading branch information
thinkingserious authored Nov 29, 2018
2 parents 95dfe06 + 34fcd13 commit 5587afb
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 10 deletions.
53 changes: 46 additions & 7 deletions packages/helpers/classes/attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
const toCamelCase = require('../helpers/to-camel-case');
const toSnakeCase = require('../helpers/to-snake-case');
const deepClone = require('../helpers/deep-clone');
const fs = require('fs');
const path = require('path');

/**
* Attachment class
Expand Down Expand Up @@ -39,30 +41,67 @@ class Attachment {
data = toCamelCase(data);

//Extract properties from data
const {content, filename, type, disposition, contentId} = data;
const {
content,
filename,
type,
disposition,
contentId,
filePath
} = data;

if ((typeof content !== 'undefined') && (typeof filePath !== 'undefined')) {
throw new Error(
`The props 'content' and 'filePath' cannot be used together.`
);
}

//Set data
this.setContent(content);
this.setFilename(filename);
this.setType(type);
this.setDisposition(disposition);
this.setContentId(contentId);
this.setContent(filePath ? this.readFile(filePath) : content);
}

/**
* Read a file and return its content as base64
*/
readFile(filePath) {
return fs.readFileSync(path.resolve(filePath));
}

/**
* Set content
*/
setContent(content) {
//Duck type check toString on content if it's a Buffer as that's the method that will be called.
if (typeof content === 'string') {
if (typeof content === 'string') {
this.content = content;
} else if (content instanceof Buffer && content.toString !== undefined) {
return;
} else if (content instanceof Buffer && content.toString !== undefined) {
this.content = content.toString();
} else {
throw new Error('`content` expected to be either Buffer or string');

if (this.disposition == 'attachment') {
this.content = content.toString('base64');
}

return;
}

throw new Error('`content` expected to be either Buffer or string');
}

/**
* Set content
*/
setFileContent(content) {
if (content instanceof Buffer && content.toString !== undefined) {
this.content = content.toString('base64');
return;
}

return;
throw new Error('`content` expected to be Buffer');
}

/**
Expand Down
58 changes: 55 additions & 3 deletions packages/helpers/classes/attachment.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const fs = require('fs');
const path = require('path');

/**
* Dependencies
Expand All @@ -18,15 +19,15 @@ describe('Attachment', function() {

//Set content as string
describe('setContent(), string', function() {
it('should set string as content', function() {
it('should set string as content', function() {
attachment.setContent("Just a string.");
expect(attachment.content).to.equal('Just a string.');
});
});

//Set content as stream
describe('setContent(), stream', function() {
it('should convert stream to string and set as content', function() {
it('should convert stream to string and set as content', function() {
const fileData = fs.readFileSync('./packages/helpers/attachment.txt');
attachment.setContent(fileData);
expect(attachment.content).to.equal('Just a little file for testing attachments.');
Expand All @@ -35,9 +36,60 @@ describe('Attachment', function() {

//Set content as wrong type
describe('setContent(), wrong type', function() {
it('should not allow setting content of wrong type', function() {
it('should not allow setting content of wrong type', function() {
expect(() => attachment.setContent(null)).to.throw('`content` expected to be either Buffer or string');
});
});

//Constructor
describe('constructor(data)', function() {
it('should not accept both content and filePath', function() {
expect(function() {
attachment = new Attachment({
filename: 'attachment.txt',
type: 'plain/text',
disposition: 'attachment',
contentId: 'myattachment',
content: '',
filePath: ''
});
}).to.throw(Error);
});
});
});

//Set content
describe('setContent()', function() {
let attachment;

beforeEach(function() {
attachment = new Attachment({
filename: 'attachment.txt',
type: 'plain/text',
disposition: 'attachment',
contentId: 'myattachment',
content: 'SGVsbG8gV29ybGQK',
});
});

it('should set the given value', function() {
expect(attachment.content).to.equal('SGVsbG8gV29ybGQK');
});

it('should accept a buffer', function() {
attachment.setContent(new Buffer("Hello World\n"));
expect(attachment.content).to.equal('SGVsbG8gV29ybGQK');
});

it('should accept a raw file', function() {
attachment = new Attachment({
filename: 'attachment.txt',
type: 'plain/text',
disposition: 'attachment',
contentId: 'myattachment',
filePath: path.resolve(__dirname + '/attachment.js')
});

expect(attachment.content).to.be.a('string');
});
});

0 comments on commit 5587afb

Please sign in to comment.