/
attachment.js
456 lines (384 loc) · 14.3 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import fs from 'fs'
import { execFile } from 'child_process';
import { promisify, promisifyAll } from 'bluebird'
import gm from 'gm'
import meta from 'musicmetadata'
import mime from 'mime-types'
import mmm from 'mmmagic'
import readChunk from 'read-chunk'
import fileType from 'file-type'
import _ from 'lodash'
import mv from 'mv';
import gifsicle from 'gifsicle';
import probe from 'probe-image-size';
import { getS3 } from '../support/s3';
import { load as configLoader } from '../../config/config'
const config = configLoader()
promisifyAll(fs)
const mvAsync = promisify(mv);
const mimeMagic = new mmm.Magic(mmm.MAGIC_MIME_TYPE)
const detectMime = promisify(mimeMagic.detectFile, { context: mimeMagic })
const magic = new mmm.Magic()
const detectFile = promisify(magic.detectFile, { context: magic })
async function mimeTypeDetect(fileName, filePath) {
const file = `${filePath}/${fileName}`;
// The file type is detected by checking the magic number of the buffer.
// It only needs the first 4100 bytes.
const buffer = await readChunk(filePath, 0, 4100)
const info = fileType(buffer)
if (info && info.mime && info.mime !== 'application/octet-stream') {
return info.mime;
}
// legacy mmmagic based detection
let mimeType = 'application/octet-stream';
try {
mimeType = await detectMime(filePath)
if (mimeType === 'application/octet-stream') {
const fileType = await detectFile(filePath)
if (fileType.startsWith('Audio file with ID3')) {
mimeType = 'audio/mpeg'
}
}
} catch (e) {
if (_.isEmpty(mimeType)) {
throw e
}
}
// otherwise, we'll use the fallback to content-type detected with a file extension provided by the user
if (mimeType === 'application/octet-stream') {
mimeType = mime.lookup(file) || mimeType
}
return mimeType
}
const execFileAsync = promisify(execFile);
export function addModel(dbAdapter) {
/**
* @constructor
*/
const Attachment = function (params) {
this.id = params.id
this.file = params.file // FormData File object
this.fileName = params.fileName // original file name, e.g. 'cute-little-kitten.jpg'
this.fileSize = params.fileSize // file size in bytes
this.mimeType = params.mimeType // used as a fallback, in case we can't detect proper one
this.fileExtension = params.fileExtension // jpg|png|gif etc.
this.mediaType = params.mediaType // image | audio | general
this.noThumbnail = params.noThumbnail // if true, image thumbnail URL == original URL
this.imageSizes = params.imageSizes || {} // pixel sizes of thumbnail(s) and original image, e.g. {t: {w: 200, h: 175}, o: {w: 600, h: 525}}
this.artist = params.artist // filled only for audio
this.title = params.title // filled only for audio
this.userId = params.userId
this.postId = params.postId
if (parseInt(params.createdAt, 10)) {
this.createdAt = params.createdAt;
}
if (parseInt(params.updatedAt, 10)) {
this.updatedAt = params.updatedAt;
}
}
Attachment.className = Attachment
Attachment.namespace = 'attachment'
Reflect.defineProperty(Attachment.prototype, 'imageSizes', {
get: function () { return this.imageSizes_ },
set: function (newValue) {
if (_.isString(newValue)) {
newValue = JSON.parse(newValue)
}
this.imageSizes_ = newValue
}
})
Attachment.prototype.validate = function () {
const valid = this.file
&& Object.keys(this.file).length > 0
&& this.file.path
&& this.file.path.length > 0
&& this.userId
&& this.userId.length > 0
if (!valid) {
throw new Error('Invalid');
}
}
Attachment.prototype.create = async function () {
this.createdAt = new Date().getTime()
this.updatedAt = new Date().getTime()
this.postId = this.postId || ''
await this.validate()
this.id = await dbAdapter.createAttachment({
postId: this.postId,
createdAt: this.createdAt.toString(),
updatedAt: this.updatedAt.toString()
})
this.fileName = this.file.name
this.fileSize = this.file.size
this.mimeType = this.file.type
// Determine initial file extension
// (it might be overridden later when we know MIME type from its contents)
// TODO: extract to config
const supportedExtensions = /\.(jpe?g|png|gif|mp3|m4a|ogg|wav|txt|pdf|docx?|pptx?|xlsx?)$/i
if (this.fileName && this.fileName.match(supportedExtensions) !== null) {
this.fileExtension = this.fileName.match(supportedExtensions)[1].toLowerCase()
} else {
this.fileExtension = ''
}
await this.handleMedia()
// Save record to DB
const params = {
fileName: this.fileName,
fileSize: this.fileSize,
mimeType: this.mimeType,
mediaType: this.mediaType,
fileExtension: this.fileExtension,
noThumbnail: this.noThumbnail,
imageSizes: JSON.stringify(this.imageSizes),
userId: this.userId,
postId: this.postId,
createdAt: this.createdAt.toString(),
updatedAt: this.updatedAt.toString()
}
if (this.mediaType === 'audio') {
params.artist = this.artist
params.title = this.title
}
await dbAdapter.updateAttachment(this.id, params)
return this
}
Reflect.defineProperty(Attachment.prototype, 'url', {
get: function () {
return config.attachments.url + config.attachments.path + this.getFilename()
}
})
Reflect.defineProperty(Attachment.prototype, 'thumbnailUrl', {
get: function () {
if (this.noThumbnail === '1') {
return this.url
}
return this.getResizedImageUrl('t')
}
})
// Get user who created the attachment (via Promise, for serializer)
Attachment.prototype.getCreatedBy = function () {
return dbAdapter.getUserById(this.userId)
}
// Get public URL of attachment (via Promise, for serializer)
Attachment.prototype.getUrl = function () {
return config.attachments.url + config.attachments.path + this.getFilename()
}
// Get public URL of attachment's thumbnail (via Promise, for serializer)
Attachment.prototype.getThumbnailUrl = function () {
if (this.noThumbnail === '1') {
return this.getUrl()
}
return this.getResizedImageUrl('t')
}
// Get public URL of resized image attachment
Attachment.prototype.getResizedImageUrl = function (sizeId) {
return config.attachments.url + config.attachments.imageSizes[sizeId].path + this.getFilename()
}
// Get local filesystem path for original file
Attachment.prototype.getPath = function () {
return config.attachments.storage.rootDir + config.attachments.path + this.getFilename()
}
// Get local filesystem path for resized image file
Attachment.prototype.getResizedImagePath = function (sizeId) {
return config.attachments.storage.rootDir + config.attachments.imageSizes[sizeId].path + this.getFilename()
}
// Get file name
Attachment.prototype.getFilename = function () {
if (this.fileExtension) {
return `${this.id}.${this.fileExtension}`
}
return this.id
}
// Store the file and process its thumbnail, if necessary
Attachment.prototype.handleMedia = async function () {
const tmpAttachmentFile = this.file.path
const tmpAttachmentFileName = this.file.name
const supportedImageTypes = {
'image/jpeg': 'jpg',
'image/png': 'png',
'image/gif': 'gif',
'image/svg+xml': 'svg'
}
const supportedAudioTypes = {
'audio/mpeg': 'mp3',
'audio/x-m4a': 'm4a',
'audio/mp4': 'm4a',
'audio/ogg': 'ogg',
'audio/x-wav': 'wav'
}
this.mimeType = await mimeTypeDetect(tmpAttachmentFileName, tmpAttachmentFile)
if (supportedImageTypes[this.mimeType]) {
// Set media properties for 'image' type
this.mediaType = 'image';
this.fileExtension = supportedImageTypes[this.mimeType];
this.noThumbnail = '1'; // this may be overriden below
await this.handleImage(tmpAttachmentFile);
} else if (supportedAudioTypes[this.mimeType]) {
// Set media properties for 'audio' type
this.mediaType = 'audio'
this.fileExtension = supportedAudioTypes[this.mimeType]
this.noThumbnail = '1'
// Analyze metadata to get Artist & Title
const readStream = fs.createReadStream(tmpAttachmentFile)
const asyncMeta = promisify(meta)
const metadata = await asyncMeta(readStream)
this.title = metadata.title
if (_.isArray(metadata.artist)) {
[this.artist] = metadata.artist;
} else {
this.artist = metadata.artist;
}
} else {
// Set media properties for 'general' type
this.mediaType = 'general'
this.noThumbnail = '1'
}
// Store an original attachment
if (config.attachments.storage.type === 's3') {
await this.uploadToS3(tmpAttachmentFile, config.attachments.path)
await fs.unlinkAsync(tmpAttachmentFile)
} else {
await mvAsync(tmpAttachmentFile, this.getPath(), {})
}
}
const fitIntoBounds = (size, bounds) => {
let width, height;
if (size.width * bounds.height > size.height * bounds.width) {
width = bounds.width; // eslint-disable-line prefer-destructuring
height = Math.max(1, Math.round(size.height * bounds.width / size.width));
} else {
width = Math.max(1, Math.round(size.width * bounds.height / size.height));
height = bounds.height; // eslint-disable-line prefer-destructuring
}
return { width, height };
}
/**
* @param {string} originalFile
*/
Attachment.prototype.handleImage = async function (originalFile) {
const tmpResizedFile = (sizeId) => `${this.file.path}.resized.${sizeId}`;
// Store original image size
let originalSize = await getImageSize(originalFile);
this.imageSizes.o = {
w: originalSize.width,
h: originalSize.height,
url: await this.getUrl(),
}
if (this.mimeType === 'image/svg+xml') {
return;
}
// Reserved for GM-style object
let originalImage = null;
// Fix EXIF orientation for original image, if JPEG
if (this.mimeType === 'image/jpeg') {
originalImage = promisifyAll(gm(originalFile));
// orientation() returns a string. Possible values are:
// unknown, Unknown, TopLeft, TopRight, BottomRight, BottomLeft, LeftTop, RightTop, RightBottom, LeftBottom
// The first three options are fine, the rest should be fixed.
const orientation = await originalImage.orientationAsync();
if (!['unknown', 'Unknown', 'TopLeft'].includes(orientation)) {
const img = originalImage
.profile(`${__dirname}/../../lib/assets/sRGB.icm`)
.autoOrient()
.quality(95);
await img.writeAsync(originalFile);
originalImage = promisifyAll(gm(originalFile));
originalSize = await getImageSize(originalFile);
this.imageSizes.o.w = originalSize.width;
this.imageSizes.o.h = originalSize.height;
}
}
const thumbIds = [];
for (const sizeId of Object.keys(config.attachments.imageSizes)) {
const { bounds } = config.attachments.imageSizes[sizeId];
if (originalSize.width <= bounds.width && originalSize.height <= bounds.height) {
continue;
}
const size = fitIntoBounds(originalSize, bounds);
this.imageSizes[sizeId] = {
w: size.width,
h: size.height,
url: this.getResizedImageUrl(sizeId),
}
thumbIds.push(sizeId);
}
if (thumbIds.length === 0) {
// No thumbnails
return;
}
this.noThumbnail = '0';
if (this.mimeType === 'image/gif') {
// Resize gif using gifsicle
await Promise.all(thumbIds.map(async (sizeId) => {
const { w, h } = this.imageSizes[sizeId];
await execFileAsync(gifsicle, [
'--resize', `${w}x${h}`,
'--resize-colors', '128',
'--no-background',
'-o', tmpResizedFile(sizeId),
originalFile,
]);
}));
} else {
// Iterate over image sizes old-fashioned (and very synchronous) way
// because gm is acting up weirdly when writing files in parallel mode
if (originalImage === null) {
originalImage = promisifyAll(gm(originalFile));
}
for (const sizeId of thumbIds) {
const { w, h } = this.imageSizes[sizeId];
await originalImage // eslint-disable-line no-await-in-loop
.resizeExact(w, h)
.profile(`${__dirname}/../../lib/assets/sRGB.icm`)
.autoOrient()
.quality(95)
.writeAsync(tmpResizedFile(sizeId));
}
}
// Save image (permanently)
if (config.attachments.storage.type === 's3') {
await Promise.all(thumbIds.map(async (sizeId) => {
const { path } = config.attachments.imageSizes[sizeId];
const file = tmpResizedFile(sizeId);
await this.uploadToS3(file, path);
await fs.unlinkAsync(file);
}));
} else {
await Promise.all(thumbIds.map(async (sizeId) => {
const file = tmpResizedFile(sizeId);
await mvAsync(file, this.getResizedImagePath(sizeId), {})
}));
}
}
// Upload original attachment or its thumbnail to the S3 bucket
Attachment.prototype.uploadToS3 = async function (sourceFile, destPath) {
const s3 = getS3(config.attachments.storage);
await s3.upload({
ACL: 'public-read',
Bucket: config.attachments.storage.bucket,
Key: destPath + this.getFilename(),
Body: fs.createReadStream(sourceFile),
ContentType: this.mimeType,
ContentDisposition: this.getContentDisposition()
}).promise();
};
// Get cross-browser Content-Disposition header for attachment
Attachment.prototype.getContentDisposition = function () {
// Old browsers (IE8) need ASCII-only fallback filenames
const fileNameAscii = this.fileName.replace(/[^\x00-\x7F]/g, '_');
// Modern browsers support UTF-8 filenames
const fileNameUtf8 = encodeURIComponent(this.fileName)
// Inline version of 'attfnboth' method (http://greenbytes.de/tech/tc2231/#attfnboth)
return `inline; filename="${fileNameAscii}"; filename*=utf-8''${fileNameUtf8}`
}
return Attachment
}
async function getImageSize(fileName) {
const input = fs.createReadStream(fileName);
try {
const { width, height } = await probe(input);
return { width, height };
} finally {
input.destroy();
}
}