Added routes for Types and Vegetables

This commit is contained in:
Damien Broqua 2018-09-08 00:00:47 +02:00
parent a8dc7f8323
commit e738d80579
17 changed files with 3938 additions and 304 deletions

111
libs/aws.js Normal file
View file

@ -0,0 +1,111 @@
const AWS = require('aws-sdk')
const fs = require('fs')
const path = require('path')
const imagemin = require('imagemin')
const imageminJpegtran = require('imagemin-jpegtran')
const imageminPngquant = require('imagemin-pngquant')
class Aws {
constructor () {
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
})
}
_send (params, callback) {
fs.readFile(params.path, (err, data) => {
if (err) {
callback(err, null)
return false
}
const base64data = Buffer.from(data, 'binary')
const dest = path.join(process.env.AWS_BASEFOLDER, params.filename)
const s3 = new AWS.S3()
s3.putObject({
Bucket: process.env.AWS_BUCKET,
Key: dest,
Body: base64data,
ACL: 'public-read'
}, (err, res) => {
if (err) {
callback(err, res)
} else {
callback(null, {
file: process.env.AWS_URL + dest
})
}
})
})
}
_compress (params, callback) {
const newFile = params.path + '.' + params.filename.split('.')[1]
fs.copyFile(params.path, newFile, (err, res) => {
if (err) {
callback(err, null)
return false
}
(async () => {
const file = await imagemin([newFile], '/tmp', {
plugins: [
imageminJpegtran(),
imageminPngquant({ quality: '65-80' })
]
})
this._send({
path: file[0].path,
filename: params.filename
}, (err, res) => {
callback(err, res)
fs.unlink(file[0].path, () => {})
})
})()
})
}
/**
* Upload file on s3
* @param {Object} params {path: String, filename: String}
* @param {Function} callback
*/
upload (params, callback) {
fs.readFile(params.path, (err, data) => {
if (err) {
callback(err, null)
return false
}
this._compress(params, callback)
})
}
deleteObjects (files, callback) {
const s3 = new AWS.S3()
const basePath = process.env.AWS_URL + process.env.AWS_BASEFOLDER
let items = []
files.forEach((file) => {
if (file) {
items.push({ Key: file.replace(basePath, '') })
}
})
if (items.length > 0) {
s3.deleteObjects({
Bucket: process.env.AWS_BUCKET,
Delete: { // required
Objects: items
}
}, callback)
} else {
callback(null, { code: 200, res: 'No file deleted' })
}
}
}
module.exports = Aws

67
libs/passport.js Normal file
View file

@ -0,0 +1,67 @@
const BasicAuth = require('passport-http').BasicStrategy
const bCrypt = require('bcrypt-nodejs')
const users = require('../models').Users
/**
* Compare bcrypt password
* @param {Object} user
* @param {String} password
* @returns {Boolean}
*/
let isValidPassword = function (user, password) {
return bCrypt.compareSync(password, user.password)
}
module.exports = function (passport) {
passport.serializeUser(
function (user, done) {
done(null, user.id)
}
)
passport.deserializeUser(
function (id, done) {
users.findById(id)
.then(user => {
if (!user) {
done(new Error('No user found'), user)
return false
}
done(null, user)
})
.catch(e => {
done(e, null)
})
}
)
passport.use(
'basic-auth', new BasicAuth({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function (req, email, password, done) {
users.find({
where: {
email: email
}
})
.then(user => {
if (!user) {
done(new Error('No user found'))
return false
}
if (!isValidPassword(user, password)) {
return done(null, false, {
message: 'Invalid password'
})
}
return done(null, user)
})
.catch(e => {
done(e, null)
})
})
)
}