api/libs/aws.js

129 lines
2.9 KiB
JavaScript
Raw Normal View History

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:51:32 +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:51:32 +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:51:32 +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) => {
2019-03-12 23:03:03 +01:00
console.log('_compress => copyFile')
2018-09-08 00:00:47 +02:00
if (err) {
callback(err, null)
return false
}
2019-03-12 22:50:05 +01:00
try {
(async () => {
2019-03-12 23:08:50 +01:00
// const file = await imagemin([newFile], '/tmp', {
// plugins: [
// imageminJpegtran(),
// imageminPngquant({
// quality: '65-80'
// })
// ]
// })
2019-03-12 22:50:05 +01:00
2019-03-12 23:08:50 +01:00
// console.log('Après imagemin')
2019-03-12 23:03:03 +01:00
2019-03-12 22:50:05 +01:00
this._send({
2019-03-12 23:08:50 +01:00
path: newFile,
// path: file[0].path,
2019-03-12 22:50:05 +01:00
filename: params.filename
}, (err, res) => {
callback(err, res)
2019-03-12 23:08:50 +01:00
// fs.unlink(file[0].path, () => { })
2019-03-12 22:50:05 +01:00
})
})()
2019-03-12 22:51:32 +01:00
} catch (e) {
2019-03-12 23:03:03 +01:00
console.log('Error:', e)
2019-03-12 22:51:32 +01:00
callback(e)
2019-03-12 22:50:05 +01:00
}
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:51:32 +01:00
upload (params, callback) {
2018-09-08 00:00:47 +02:00
fs.readFile(params.path, (err, data) => {
2019-03-12 23:03:03 +01:00
console.log('après upload -> readfile')
2018-09-08 00:00:47 +02:00
if (err) {
callback(err, null)
return false
}
this._compress(params, callback)
})
}
2019-03-12 22:51:32 +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