-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmedia.js
More file actions
438 lines (371 loc) · 12.9 KB
/
Copy pathmedia.js
File metadata and controls
438 lines (371 loc) · 12.9 KB
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
const fileType = require('file-type')
const fs = require('fs').promises
const isSvg = require('is-svg')
const sharp = require('sharp')
const mediaConfig = oboRequire('server/config').media
const logger = oboRequire('server/logger')
const db = oboRequire('server/db')
const oboEvents = oboRequire('server/obo_events')
const MODE_INSERT_ORIGINAL_IMAGE = 'modeInsertOriginalImage'
const MODE_INSERT_RESIZED_IMAGE = 'modeInsertResizedImage'
const MIMETYPE_SVG = 'image/svg+xml'
const MIMETYPE_GIF = 'image/gif'
class Media {
// Return true if both dimensions are supplied or if the requested targetNewSize is smaller than
// the original size:
static async shouldResizeMedia(originalMediaBinaryData, targetDimensions) {
const targetNewSize = Media.getDimensionValues(targetDimensions)
if (targetNewSize.width && targetNewSize.height) return true
const originalImageMetadata = await sharp(originalMediaBinaryData).metadata()
return (
targetNewSize.width < originalImageMetadata.width ||
targetNewSize.height < originalImageMetadata.height
)
}
// Convinence method to take the metadata produced by sharp and turn it into a valid mimetype
static getMimeTypeFromMetadata(metadata) {
return `image/${metadata.format}`
}
static isMimeTypeResizable(mimeType) {
switch (mimeType) {
case MIMETYPE_SVG:
case MIMETYPE_GIF:
return false
default:
return true
}
}
static parseCustomImageDimensions(dimensionsAsString) {
let width
let height
const parsedDimensions = dimensionsAsString.split('x')
const finalDimensions = {}
if (parsedDimensions.length !== 2) {
const message =
'Invalid dimension string provided. Expecting {width}x{height} (i.e. 1200x900).'
logger.error(message + ` Got ${dimensionsAsString} instead.`)
throw new Error(message)
}
width = parsedDimensions[0]
height = parsedDimensions[1]
if (width === '*' && height === '*') {
const message =
'Must provide a height, width, or height and width. *.* is an invalid image dimension string.'
logger.error(message)
throw new Error(message)
}
if (width !== '*') {
width = parseInt(width, 10)
if (isNaN(width)) {
const message = 'Invalid type specified for image width. Integer or * wildcard expected.'
logger.error(message + ` Got ${dimensionsAsString} instead.`)
throw new Error(message)
}
// Enforce min and max width
finalDimensions['width'] = Math.max(
mediaConfig.minImageSize,
Math.min(mediaConfig.maxImageSize, width)
)
}
if (height !== '*') {
height = parseInt(height, 10)
if (isNaN(height)) {
const message = 'Invalid type specified for image height. Integer or * wildcard expected.'
logger.error(message + ` Got ${dimensionsAsString} instead.`)
throw new Error(message)
}
// enforce min and max height
finalDimensions['height'] = Math.max(
mediaConfig.minImageSize,
Math.min(mediaConfig.maxImageSize, height)
)
}
return finalDimensions
}
static getDimensionValues(dimensionsAsString) {
switch (dimensionsAsString) {
case 'small':
return mediaConfig.presetDimensions.small
case 'medium':
return mediaConfig.presetDimensions.medium
case 'large':
return mediaConfig.presetDimensions.large
default:
return Media.parseCustomImageDimensions(dimensionsAsString)
}
}
static resize(mediaBinary, { width, height }) {
const fit = width && height ? 'fill' : 'cover'
return sharp(mediaBinary)
.resize({
fit,
width,
height
})
.toBuffer()
}
// targetDimensions can either be a label ['small', 'medium', 'large', 'original'] or a string
// of specific dimensions (such as "640x*", "*x480" or "640x480").
// If the requested dimension has already been processed and saved it is retrieved without any
// additional processing. If a single dimension is requested (such as one of the labels like
// 'small' which simply is a width or a specific dimension with a wildcard like '640x*') then
// the image will be scaled proportionally up until that original image's native size. In this
// case the image will not be upscaled (so, for example, for an image that is originally
// 640x480 if you request the size '1080x*' you'll simply get back the original 640x480 size).
// However, if you request two dimensions (like '640x480) the image will distort and will
// potentially be upscaled if needed to ensure that the resulting image matches the requested
// size.
static async fetchByIdAndDimensions(mediaId, targetDimensions = 'large') {
const dimensionOriginal = mediaConfig.originalMediaTag
let originalMedia = null
const medias = await db.manyOrNone(
`
SELECT *
FROM media_binaries M
JOIN binaries B
ON M.binary_id = B.id
WHERE M.media_id = $[mediaId]
AND (M.dimensions = $[targetDimensions]
OR M.dimensions = $[dimensionOriginal])
ORDER BY M.dimensions
`,
{ mediaId, targetDimensions, dimensionOriginal }
)
if (!medias || !medias.length) throw new Error('Image not found')
// Create a map to retrieve the resulting media objects by their dimension label
const mediaByDimensions = new Map(medias.map(m => [m.dimensions, m]))
originalMedia = mediaByDimensions.get(dimensionOriginal)
if (!originalMedia) throw new Error('Original image size not found')
// Return the requested size (if we have it)
const targetMedia = mediaByDimensions.get(targetDimensions)
if (targetMedia) {
return {
binaryData: targetMedia.blob,
mimeType: targetMedia.mime_type
}
}
// If the media type is not resizable of if the new size we want is larger than the
// original size then we won't resize it, instead we'll just return the original
// size (ignoring whatever the requested size was)
if (
!Media.isMimeTypeResizable(originalMedia.mime_type) ||
!(await Media.shouldResizeMedia(originalMedia.blob, targetDimensions))
) {
return {
binaryData: originalMedia.blob,
mimeType: originalMedia.mime_type
}
}
// If we are here then we didn't find the media at the size we were looking for.
// Now we need to resize the image, store the new dimension in the database
// for future retrieval, then return the newly resized image data.
const resizedInfo = await Media.saveImageAtNewSize(
mediaId,
originalMedia.blob,
targetDimensions
)
return {
binaryData: resizedInfo.binary,
mimeType: Media.getMimeTypeFromMetadata(resizedInfo.metadata)
}
}
static fetchByUserId(userId, start, count) {
if (!userId || !Number.isInteger(start) || !Number.isInteger(count)) {
throw new Error('Invalid argument.')
}
return db
.manyOrNone(
`
SELECT
id,
file_name as "fileName",
created_at as "createdAt"
FROM
media
WHERE
user_id = $[userId]
ORDER BY
created_at DESC
LIMIT $[count]
OFFSET $[start]
`,
{
userId,
start,
count: count + 1 // ask for 1 more then we need to determine if there are more
}
)
.then(res => {
const hasMore = res.length > count
// remove the extra result if it's present
const media = hasMore ? res.splice(0, count) : res
return {
media,
hasMore
}
})
.catch(err => {
logger.error(err)
throw err
})
}
static storeImageInDb({ filename, binary, size, mimetype, dimensions, mode, mediaId, userId }) {
let mediaBinaryId = null
if (!binary || !size || !mimetype || !dimensions || !mode) {
throw new Error('Inserting an image, but one or more required arguments not provided.')
}
if (mode === MODE_INSERT_ORIGINAL_IMAGE && !userId) {
throw new Error('Inserting an original image, but no user id provided')
}
if (mode === MODE_INSERT_ORIGINAL_IMAGE && !filename) {
throw new Error('Inserting an original image, but no filename provided')
}
if (mode === MODE_INSERT_RESIZED_IMAGE && !mediaId) {
throw new Error('Inserting a resized image, but no media id of the original image provided')
}
return db
.tx(transactionDb => {
// The following query takes an image binary and metadata, and stores it in the binaries database table.
// The binaries database table holds the binaries for images uploaded by a user, and stores resized
// images derived from the orginal user uploads. There is no difference between storing an original
// or resized image here. All entries are independent.
return transactionDb
.one(
`
INSERT INTO binaries
(blob, file_size, mime_type)
VALUES
($[binary], $[size], $[mimetype])
RETURNING id`,
{ binary, size, mimetype }
)
.then(insertBinaryResult => {
mediaBinaryId = insertBinaryResult.id
// If an orginal image is being stored, this query will create a record that the user uploaded an
// image. From this record, the user's original image and resized images derived from the original
// can be found. All resized images derived from an original will have the same mediaId as the
// original, so it is not necessary to store a reference to the resized images in the media table.
if (mode === MODE_INSERT_ORIGINAL_IMAGE) {
return transactionDb
.one(
`
INSERT INTO media
(user_id, file_name)
VALUES
($[userId], $[filename])
RETURNING id`,
{ userId, filename }
)
.then(insertMediaResult => {
// update the mediaId now that we have it
// if we're resizing, we already have it
mediaId = insertMediaResult.id
})
}
})
.then(() => {
// The media_binaries table links media records to their respective binary information. This
// query creates that link. Both original and resized images must be linked. Since resized
// images have the same id as the original they are derived from, a dimensions field is
// stored to describe the binary the link is pointing to.
return transactionDb.none(
`
INSERT INTO media_binaries
(media_id, binary_id, dimensions)
VALUES
($[mediaId], $[mediaBinaryId], $[dimensions])
`,
{ mediaId, mediaBinaryId, dimensions }
)
})
})
.then(() => ({
media_id: mediaId,
filename,
mode,
mimetype,
dimensions
}))
}
static async saveImageAtNewSize(originalImageId, originalImageBinary, targetDimensions) {
const newSize = Media.getDimensionValues(targetDimensions)
const resizedBinary = await Media.resize(originalImageBinary, newSize)
const imageMetadata = await sharp(resizedBinary).metadata()
await Media.storeImageInDb({
binary: resizedBinary,
size: imageMetadata.size,
mimetype: Media.getMimeTypeFromMetadata(imageMetadata),
dimensions: targetDimensions,
mode: MODE_INSERT_RESIZED_IMAGE,
mediaId: originalImageId,
userId: null
})
oboEvents.emit(Media.EVENT_RESIZED_IMAGE_CREATED, { originalImageId, targetDimensions })
return {
metadata: imageMetadata,
binary: resizedBinary
}
}
static async isValidFileType(file) {
const allowedFileTypes = new RegExp(mediaConfig.allowedMimeTypesRegex)
// fileType looks into the file to find the true file type using magic numbers
const fileTypeInfo = await fileType.fromBuffer(file)
// fileType does not support SVGs because the whole buffer must be read to determine if the
// buffer is an SVG.
return (fileTypeInfo && allowedFileTypes.test(fileTypeInfo.ext)) || isSvg(file)
}
static async createAndSave(userId, fileInfo) {
let file
try {
if (fileInfo.buffer) {
// allow fileInfo to already contain a loaded buffer
file = fileInfo.buffer
} else {
// load the file from disk
file = await fs.readFile(fileInfo.path)
}
} catch (error) {
// calling methods expect a thenable object to be returned
logger.logError('Error Reading media file', error)
throw error
}
const isValid = await Media.isValidFileType(file, fileInfo.originalname, fileInfo.mimetype)
if (!isValid) {
// Delete the temporary media stored by Multer
if (fileInfo.path) await fs.unlink(fileInfo.path)
throw new Error(
`File upload only supports the following filetypes: ${mediaConfig.allowedMimeTypesRegex
.split('|')
.join(', ')}`
)
}
try {
const mediaRecord = await Media.storeImageInDb({
filename: fileInfo.originalname,
binary: file,
size: fileInfo.size,
mimetype: fileInfo.mimetype,
dimensions: mediaConfig.originalMediaTag,
mode: MODE_INSERT_ORIGINAL_IMAGE,
mediaId: null,
userId
})
// Delete the temporary media stored by Multer
if (fileInfo.path) await fs.unlink(fileInfo.path)
oboEvents.emit(Media.EVENT_IMAGE_CREATED, {
userId,
fileSize: fileInfo.size,
mimeType: fileInfo.mimetype,
originalName: fileInfo.originalname
})
// ID of the user media, not the binary data
return mediaRecord
} catch (error) {
logger.logError('Error saving media file', error)
throw error
}
}
}
Media.EVENT_RESIZED_IMAGE_CREATED = 'EVENT_RESIZED_IMAGE_CREATED'
Media.EVENT_IMAGE_CREATED = 'EVENT_IMAGE_CREATED'
module.exports = Media