Added catch on imagemin

This commit is contained in:
dbroqua 2019-03-12 22:50:05 +01:00
parent 3e466925d6
commit cd11c01c9b

View file

@ -6,14 +6,14 @@ const imageminJpegtran = require('imagemin-jpegtran')
const imageminPngquant = require('imagemin-pngquant') const imageminPngquant = require('imagemin-pngquant')
class Aws { class Aws {
constructor () { constructor() {
AWS.config.update({ AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID, accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
}) })
} }
_send (params, callback) { _send(params, callback) {
fs.readFile(params.path, (err, data) => { fs.readFile(params.path, (err, data) => {
if (err) { if (err) {
callback(err, null) callback(err, null)
@ -40,7 +40,7 @@ class Aws {
}) })
} }
_compress (params, callback) { _compress(params, callback) {
const newFile = params.path + '.' + params.filename.split('.')[1] const newFile = params.path + '.' + params.filename.split('.')[1]
fs.copyFile(params.path, newFile, (err, res) => { fs.copyFile(params.path, newFile, (err, res) => {
@ -49,24 +49,28 @@ class Aws {
return false return false
} }
(async () => { try {
const file = await imagemin([newFile], '/tmp', {
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: '65-80'
})
]
})
this._send({ (async () => {
path: file[0].path, const file = await imagemin([newFile], '/tmp', {
filename: params.filename plugins: [
}, (err, res) => { imageminJpegtran(),
callback(err, res) imageminPngquant({
fs.unlink(file[0].path, () => {}) quality: '65-80'
}) })
})() ]
})
this._send({
path: file[0].path,
filename: params.filename
}, (err, res) => {
callback(err, res)
fs.unlink(file[0].path, () => { })
})
})()
}
catch (callback)
}) })
} }
@ -75,7 +79,7 @@ class Aws {
* @param {Object} params {path: String, filename: String} * @param {Object} params {path: String, filename: String}
* @param {Function} callback * @param {Function} callback
*/ */
upload (params, callback) { upload(params, callback) {
fs.readFile(params.path, (err, data) => { fs.readFile(params.path, (err, data) => {
if (err) { if (err) {
callback(err, null) callback(err, null)
@ -86,7 +90,7 @@ class Aws {
}) })
} }
deleteObjects (files, callback) { deleteObjects(files, callback) {
const s3 = new AWS.S3() const s3 = new AWS.S3()
const basePath = process.env.AWS_URL + process.env.AWS_BASEFOLDER const basePath = process.env.AWS_URL + process.env.AWS_BASEFOLDER
let items = [] let items = []