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