-
Notifications
You must be signed in to change notification settings - Fork 784
/
Copy pathattachment.js
200 lines (171 loc) · 4.14 KB
/
attachment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
'use strict';
/**
* Dependencies
*/
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
*/
class Attachment {
/**
* Constructor
*/
constructor(data) {
//Create from data
if (data) {
this.fromData(data);
}
}
/**
* From data
*/
fromData(data) {
//Expecting object
if (typeof data !== 'object') {
throw new Error('Expecting object for Mail data');
}
//Convert to camel case to make it workable, making a copy to prevent
//changes to the original objects
data = deepClone(data);
data = toCamelCase(data);
//Extract properties from 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.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') {
this.content = content;
return;
} else if (content instanceof Buffer && content.toString !== undefined) {
this.content = content.toString();
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;
}
throw new Error('`content` expected to be Buffer');
}
/**
* Set filename
*/
setFilename(filename) {
if (typeof filename === 'undefined') {
return;
}
if (filename && typeof filename !== 'string') {
throw new Error('String expected for `filename`');
}
this.filename = filename;
}
/**
* Set type
*/
setType(type) {
if (typeof type === 'undefined') {
return;
}
if (typeof type !== 'string') {
throw new Error('String expected for `type`');
}
this.type = type;
}
/**
* Set disposition
*/
setDisposition(disposition) {
if (typeof disposition === 'undefined') {
return;
}
if (typeof disposition !== 'string') {
throw new Error('String expected for `disposition`');
}
this.disposition = disposition;
}
/**
* Set content ID
*/
setContentId(contentId) {
if (typeof contentId === 'undefined') {
return;
}
if (typeof contentId !== 'string') {
throw new Error('String expected for `contentId`');
}
this.contentId = contentId;
}
/**
* To JSON
*/
toJSON() {
//Extract properties from self
const {content, filename, type, disposition, contentId} = this;
//Initialize with mandatory properties
const json = {content, filename};
//Add whatever else we have
if (typeof type !== 'undefined') {
json.type = type;
}
if (typeof disposition !== 'undefined') {
json.disposition = disposition;
}
if (typeof contentId !== 'undefined') {
json.contentId = contentId;
}
//Return
return toSnakeCase(json);
}
static create(data) {
if(Array.isArray(data)) {
return data.filter(Boolean).map(item => this.create(item));
}
//Already instance of Attachment class?
if (data instanceof Attachment) {
return data;
}
//Create instance
return new Attachment(data);
}
}
//Export class
module.exports = Attachment;