2018-09-16 23:46:39 +02:00
|
|
|
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')
|
2018-09-19 21:49:02 +02:00
|
|
|
const Resize = require('../libs/resize')
|
2018-09-16 23:46:39 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:49:02 +02:00
|
|
|
_createOne (req, callback) {
|
|
|
|
pictures.create({
|
|
|
|
order: req.body.order,
|
|
|
|
url: req.body.url,
|
|
|
|
vegetableId: req.params.vegetablesId
|
|
|
|
})
|
|
|
|
.then(item => {
|
|
|
|
console.log('ici')
|
|
|
|
callback(null, item)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
console.log(e)
|
|
|
|
callback(e, null)
|
|
|
|
})
|
2019-03-12 23:00:37 +01:00
|
|
|
fs.unlink(req.file.path, () => { })
|
2018-09-19 21:49:02 +02:00
|
|
|
}
|
|
|
|
|
2018-09-16 23:46:39 +02:00
|
|
|
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) {
|
2018-09-19 21:49:02 +02:00
|
|
|
const key = uuid()
|
2018-09-16 23:46:39 +02:00
|
|
|
let aws = new Aws()
|
2018-09-19 21:49:02 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-03-12 23:00:37 +01:00
|
|
|
fs.unlink(large.output, () => { })
|
|
|
|
|
2018-09-19 21:49:02 +02:00
|
|
|
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)
|
2019-03-12 23:00:37 +01:00
|
|
|
fs.unlink(file.output, () => { })
|
2018-09-19 21:49:02 +02:00
|
|
|
})
|
2018-09-16 23:46:39 +02:00
|
|
|
})
|
2018-09-19 21:49:02 +02:00
|
|
|
})
|
2018-09-16 23:46:39 +02:00
|
|
|
})
|
|
|
|
} 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()
|
|
|
|
|
2018-09-19 21:49:02 +02:00
|
|
|
aws.deleteObjects([item.url, item.url.replace('_large.', '_tumb.')], (err) => {
|
2018-09-16 23:46:39 +02:00
|
|
|
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
|