2018-09-08 00:00:47 +02:00
|
|
|
const AWS = require('aws-sdk')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const imagemin = require('imagemin')
|
|
|
|
const imageminJpegtran = require('imagemin-jpegtran')
|
|
|
|
const imageminPngquant = require('imagemin-pngquant')
|
|
|
|
|
|
|
|
class Aws {
|
2019-03-12 22:50:05 +01:00
|
|
|
constructor() {
|
2018-09-08 00:00:47 +02:00
|
|
|
AWS.config.update({
|
|
|
|
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
|
|
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:50:05 +01:00
|
|
|
_send(params, callback) {
|
2018-09-08 00:00:47 +02:00
|
|
|
fs.readFile(params.path, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
callback(err, null)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const base64data = Buffer.from(data, 'binary')
|
|
|
|
const dest = path.join(process.env.AWS_BASEFOLDER, params.filename)
|
|
|
|
const s3 = new AWS.S3()
|
|
|
|
|
|
|
|
s3.putObject({
|
|
|
|
Bucket: process.env.AWS_BUCKET,
|
|
|
|
Key: dest,
|
|
|
|
Body: base64data,
|
|
|
|
ACL: 'public-read'
|
|
|
|
}, (err, res) => {
|
|
|
|
if (err) {
|
|
|
|
callback(err, res)
|
|
|
|
} else {
|
|
|
|
callback(null, {
|
|
|
|
file: process.env.AWS_URL + dest
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:50:05 +01:00
|
|
|
_compress(params, callback) {
|
2018-09-08 00:00:47 +02:00
|
|
|
const newFile = params.path + '.' + params.filename.split('.')[1]
|
|
|
|
|
|
|
|
fs.copyFile(params.path, newFile, (err, res) => {
|
|
|
|
if (err) {
|
|
|
|
callback(err, null)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:50:05 +01:00
|
|
|
try {
|
2018-09-08 00:00:47 +02:00
|
|
|
|
2019-03-12 22:50:05 +01:00
|
|
|
(async () => {
|
|
|
|
const file = await imagemin([newFile], '/tmp', {
|
|
|
|
plugins: [
|
|
|
|
imageminJpegtran(),
|
|
|
|
imageminPngquant({
|
|
|
|
quality: '65-80'
|
|
|
|
})
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
this._send({
|
|
|
|
path: file[0].path,
|
|
|
|
filename: params.filename
|
|
|
|
}, (err, res) => {
|
|
|
|
callback(err, res)
|
|
|
|
fs.unlink(file[0].path, () => { })
|
|
|
|
})
|
|
|
|
})()
|
|
|
|
}
|
|
|
|
catch (callback)
|
2018-09-08 00:00:47 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-19 21:49:02 +02:00
|
|
|
* Upload file on s3
|
|
|
|
* @param {Object} params {path: String, filename: String}
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
2019-03-12 22:50:05 +01:00
|
|
|
upload(params, callback) {
|
2018-09-08 00:00:47 +02:00
|
|
|
fs.readFile(params.path, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
callback(err, null)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
this._compress(params, callback)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:50:05 +01:00
|
|
|
deleteObjects(files, callback) {
|
2018-09-08 00:00:47 +02:00
|
|
|
const s3 = new AWS.S3()
|
|
|
|
const basePath = process.env.AWS_URL + process.env.AWS_BASEFOLDER
|
|
|
|
let items = []
|
|
|
|
|
|
|
|
files.forEach((file) => {
|
|
|
|
if (file) {
|
2018-09-19 21:49:02 +02:00
|
|
|
items.push({
|
|
|
|
Key: file.replace(basePath, '')
|
|
|
|
})
|
2018-09-08 00:00:47 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (items.length > 0) {
|
|
|
|
s3.deleteObjects({
|
|
|
|
Bucket: process.env.AWS_BUCKET,
|
|
|
|
Delete: { // required
|
|
|
|
Objects: items
|
|
|
|
}
|
|
|
|
}, callback)
|
|
|
|
} else {
|
2018-09-19 21:49:02 +02:00
|
|
|
callback(null, {
|
|
|
|
code: 200,
|
|
|
|
res: 'No file deleted'
|
|
|
|
})
|
2018-09-08 00:00:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Aws
|