const pictures = require('../models').vegetablePictures const multer = require('multer') const Vegetables = require('./Vegetables') const path = require('path') const fs = require('fs') const Aws = require('../libs/aws') const Resize = require('../libs/resize') const uuid = require('uuid/v4') class Pictures { constructor () { this._upload = multer({ storage: multer.diskStorage({}), fileFilter: function (req, file, cb) { const filetypes = /jpg|jpeg|png|JPG|JPEG|PNG/ const mimetype = filetypes.test(file.mimetype) const extname = filetypes.test(path.extname(file.originalname).toLowerCase()) if (mimetype && extname) { return cb(null, true) } cb(new Error('Error: File upload only supports the following filetypes - ' + filetypes)) } }).single('picture') } _reOrderPictures (position, vegetableId) { pictures.findAll({ where: { vegetableId: vegetableId } }) .then(items => { items.forEach((item) => { let itemPosition = item.order if (itemPosition >= position) { item.update({ order: itemPosition - 1 }) .catch((e) => { console.log(e) }) } }) }) .catch((error) => { console.log(error) }) } static getAll (req, callback) { Vegetables.getOne(req, (err, item) => { if (err) { callback(err, item) return false } pictures.findAndCountAll({ where: { vegetableId: req.params.vegetablesId }, order: [ ['order', 'ASC'] ] }) .then(items => { if (!items) { callback(new Error('No picture found'), 204) return false } callback(null, items) }) .catch((e) => { callback(e, null) }) }) } static getOne (req, callback) { Vegetables.getOne(req, (err, item) => { if (err) { callback(err, item) return false } pictures.find({ where: { id: req.params.pictureId, vegetableId: req.params.vegetablesId }, order: [ ['order', 'ASC'] ] }) .then(item => { if (!item) { callback(new Error('Picture not found'), 404) return false } callback(null, item) }) .catch((e) => { callback(e, null) }) }) } _createOne (req, callback) { pictures.create({ order: req.body.order, url: req.body.url, vegetableId: req.params.vegetablesId }) .then(item => { callback(null, item) }) .catch(e => { console.log(e) callback(e, null) }) fs.unlink(req.file.path, () => { }) } createOne (req, callback) { Vegetables.getOne(req, (err, item) => { if (err) { callback(err, item) return false } Pictures.getAll(req, (err, items) => { if (err) { callback(err, item) return false } const order = items.count + 1 this._upload(req, req.body, (err) => { if (err) { callback(err, null) return false } if (req.file) { const key = uuid() let aws = new Aws() let resize = new Resize() resize.createLargeImage(req.file.path, (err, large) => { if (err) { callback(err, null) return false } aws.upload({ path: large.output, filename: `picture_${req.params.vegetablesId}_${key}_large.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}` }, (err, res) => { if (err) { callback(err, null) } req.body = { order: order, url: res.file } fs.unlink(large.output, () => { }) resize.createThumbnail(req.file.path, (err, file) => { if (err) { callback(err, null) return false } aws.upload({ path: file.output, filename: `picture_${req.params.vegetablesId}_${key}_thumb.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}` }, (err, res) => { if (err) { callback(err, null) } this._createOne(req, callback) fs.unlink(file.output, () => { }) }) }) }) }) } else { callback(new Error('No file sent'), 406) } }) }) }) } deleteOne (req, callback) { Pictures.getOne(req, (err, item) => { if (err) { callback(err, item) return false } const aws = new Aws() aws.deleteObjects([item.url, item.url.replace('_large.', '_tumb.')], (err) => { if (err) { callback(err, null) return false } pictures.destroy({ where: { id: req.params.pictureId } }) .then(deleted => { if (deleted === 0) { callback(new Error('Error when trying to delete item')) return false } callback(null, null) }) .catch(e => { callback(e, null) }) }) }) } } module.exports = Pictures