feat(Medias): use sharp and proper multer s3 setup to resize medias

This commit is contained in:
Matthieu Bessat 2020-08-10 18:04:50 +02:00
parent 7edeb393c4
commit 7d7083a576
3 changed files with 386 additions and 17 deletions

View file

@ -1,7 +1,9 @@
import Utils from './Utils'
import aws from 'aws-sdk'
import multer from 'multer'
import multerS3 from 'multer-s3'
// @ts-ignore
import multerS3 from 'multer-s3-with-transforms'
import sharp from 'sharp'
export default class MediaService {
@ -49,16 +51,36 @@ export default class MediaService {
bucket: MediaService.getBucket(),
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
transforms: () => {
let transformOptions = {}
let quality = 80
if (type === 'media') {
transformOptions = { width: 953, withoutEnlargement: true, fit: 'cover' }
}
if (type === 'thumbnail') {
quality = 100
transformOptions = { width: 250, withoutEnlargement: true, fit: 'cover' }
}
if (type === 'cover') {
quality = 90
transformOptions = { height: 550, withoutEnlargement: true, fit: 'cover' }
}
console.log('transform called', transformOptions)
return sharp().resize(transformOptions).jpeg({
progressive: true,
quality,
force: false
})
},
key: (_: any, file: any, cb: any) => {
console.log(file)
cb(null, Date.now().toString() + '_' + type)
// generate a random id for this image
let r = Math.random().toString(36).substring(4).toUpperCase()
console.log(r, file)
cb(null, r + '_' + type)
}
})
})
if (multiple) {
return instance.array('file')
}
return instance.single('file')
return multiple ? instance.array('file') : instance.single('file')
}
static buildMedia(file: any, type: any) {