From e4f06185fe9a480366b58b923e0f0dfa428c8cb2 Mon Sep 17 00:00:00 2001 From: dbroqua Date: Wed, 19 Sep 2018 22:08:11 +0200 Subject: [PATCH] Updated Vegetables pictures --- middleware/Vegetables.js | 110 +++++++++++++++++++++++++++++++++------ 1 file changed, 93 insertions(+), 17 deletions(-) diff --git a/middleware/Vegetables.js b/middleware/Vegetables.js index e511475..84f7024 100644 --- a/middleware/Vegetables.js +++ b/middleware/Vegetables.js @@ -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) {