Updated Vegetables pictures

This commit is contained in:
dbroqua 2018-09-19 22:08:11 +02:00
parent 405d908a09
commit e4f06185fe

View file

@ -4,6 +4,7 @@ const uuid = require('uuid/v4')
const multer = require('multer') const multer = require('multer')
const path = require('path') const path = require('path')
const fs = require('fs') const fs = require('fs')
const Resize = require('../libs/resize')
const Aws = require('../libs/aws') const Aws = require('../libs/aws')
class Vegetables { class Vegetables {
@ -75,7 +76,7 @@ class Vegetables {
} }
} }
aws.deleteObjects([path], callback) aws.deleteObjects([path, path.replace('_large.', '_thumb.')], callback)
} }
static getAll (req, callback) { static getAll (req, callback) {
@ -114,14 +115,53 @@ class Vegetables {
} }
if (req.file) { if (req.file) {
let uploaded = 0
let aws = new Aws() let aws = new Aws()
aws.upload({ let thumb = new Resize()
path: req.file.path, let large = new Resize()
filename: `main_${uuid()}.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}` const key = uuid()
}, (err, res) => { let _create = function () {
if (!err) { req.body.mainPicture = res.file } if (uploaded === 2) {
this._createItem(req, callback) this._createItem(req, callback)
fs.unlink(req.file.path, () => {}) fs.unlink(req.file.path, () => {})
}
}
large.createLargeImage(req.file.path, (err, file) => {
if (err) {
callback(err, null)
return false
}
aws.upload({
path: file.output,
filename: `main_${key}_large.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}`
}, (err, res) => {
if (!err) {
req.body.mainPicture = res.file
uploaded += 1
_create()
fs.unlink(file.output, () => {})
}
})
})
thumb.createLargeImage(req.file.path, (err, file) => {
if (err) {
callback(err, null)
return false
}
aws.upload({
path: file.output,
filename: `main_${key}_thumb.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}`
}, (err, res) => {
if (!err) {
uploaded += 1
_create()
fs.unlink(file.output, () => {})
}
})
}) })
} else { } else {
this._createItem(req, callback) this._createItem(req, callback)
@ -166,21 +206,58 @@ class Vegetables {
let values = req.body let values = req.body
if (req.file) { if (req.file) {
let uploaded = 0
let aws = new Aws() let aws = new Aws()
let thumb = new Resize()
let large = new Resize()
const key = uuid()
if (item.mainPicture !== null && item.mainPicture !== '') { if (item.mainPicture !== null && item.mainPicture !== '') {
this._deleteMedias(item.mainPicture) this._deleteMedias(item.mainPicture)
} }
aws.upload({ let _patch = function () {
path: req.file.path, if (uploaded === 2) {
filename: `main_${req.params.vegetablesId}${path.extname(req.file.originalname).toLowerCase()}` this._patchOne(item, values, callback)
}, (err, res) => { fs.unlink(req.file.path, () => {})
if (!err) {
values.mainPicture = res.file
} }
this._patchOne(item, values, callback) }
fs.unlink(req.file.path, () => {})
large.createLargeImage(req.file.path, (err, file) => {
if (err) {
callback(err, null)
return false
}
aws.upload({
path: file.output,
filename: `main_${key}_large.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}`
}, (err, res) => {
if (!err) {
values.mainPicture = res.file
uploaded += 1
_patch()
fs.unlink(file.output, () => {})
}
})
})
thumb.createLargeImage(req.file.path, (err, file) => {
if (err) {
callback(err, null)
return false
}
aws.upload({
path: file.output,
filename: `main_${key}_thumb.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}`
}, (err, res) => {
if (!err) {
uploaded += 1
_patch()
fs.unlink(file.output, () => {})
}
})
}) })
} else { } else {
this._patchOne(item, values, callback) this._patchOne(item, values, callback)
@ -197,7 +274,6 @@ class Vegetables {
} }
if (item.mainPicture !== null && item.mainPicture !== '') { if (item.mainPicture !== null && item.mainPicture !== '') {
const aws = new Aws()
if (!callback) { if (!callback) {
callback = (e) => { callback = (e) => {
if (e) { if (e) {