192 lines
4.5 KiB
JavaScript
192 lines
4.5 KiB
JavaScript
|
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 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) {
|
||
|
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) {
|
||
|
let aws = new Aws()
|
||
|
aws.upload({
|
||
|
path: req.file.path,
|
||
|
filename: `picture_${req.params.vegetablesId}_${uuid()}.${req.file.originalname.split('.')[req.file.originalname.split('.').length - 1]}`
|
||
|
}, (err, res) => {
|
||
|
if (err) { callback(err, null) }
|
||
|
|
||
|
pictures.create({
|
||
|
order: order,
|
||
|
url: res.file,
|
||
|
vegetableId: req.params.vegetablesId
|
||
|
})
|
||
|
.then(item => {
|
||
|
callback(null, item)
|
||
|
})
|
||
|
.catch(e => {
|
||
|
callback(e, null)
|
||
|
})
|
||
|
fs.unlink(req.file.path, () => {})
|
||
|
})
|
||
|
} 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], (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
|