-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Satyajit Dey
committed
May 29, 2020
1 parent
508be38
commit 933948e
Showing
23 changed files
with
817 additions
and
737 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 2, | ||
"semi": false, | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,49 @@ | ||
var debug = require('debug')('cms-backend-api:*'); | ||
var express = require('express'); | ||
var logger = require('morgan'); | ||
var debug = require('debug')('cms-backend-api:*') | ||
var express = require('express') | ||
var logger = require('morgan') | ||
|
||
const bodyParser = require('body-parser'); | ||
const bodyParser = require('body-parser') | ||
|
||
const indexRouter = require('./routes/index') | ||
const authRouter = require('./routes/auth.route'); | ||
const usersRouter = require('./routes/users.route'); | ||
const binaryRouter=require('./routes/binaries.route'); | ||
const articleRouter = require('./routes/article'); | ||
const authRouter = require('./routes/auth.route') | ||
const usersRouter = require('./routes/users.route') | ||
const binaryRouter = require('./routes/binaries.route') | ||
const articleRouter = require('./routes/article') | ||
|
||
const app = express(); | ||
const app = express() | ||
|
||
//middleware | ||
app.use(logger('dev')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(logger('dev')) | ||
app.use(express.json()) | ||
app.use(express.urlencoded({ extended: false })) | ||
|
||
app.use(function (req, res, next) { | ||
res.header('Access-Control-Allow-Origin', '*'); | ||
res.header('Access-Control-Allow-Credentials', 'true'); | ||
res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE'); | ||
res.header('Access-Control-Expose-Headers', 'Content-Length'); | ||
res.header('Access-Control-Allow-Headers', 'Accept, Authorization, Content-Type, X-Requested-With, Range'); | ||
if (req.method === 'OPTIONS') { | ||
return res.send(200); | ||
} else { | ||
return next(); | ||
} | ||
}); | ||
|
||
app.use(bodyParser.json()); | ||
res.header('Access-Control-Allow-Origin', '*') | ||
res.header('Access-Control-Allow-Credentials', 'true') | ||
res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE') | ||
res.header('Access-Control-Expose-Headers', 'Content-Length') | ||
res.header( | ||
'Access-Control-Allow-Headers', | ||
'Accept, Authorization, Content-Type, X-Requested-With, Range' | ||
) | ||
if (req.method === 'OPTIONS') { | ||
return res.send(200) | ||
} else { | ||
return next() | ||
} | ||
}) | ||
|
||
app.use(bodyParser.json()) | ||
|
||
//List all the routes | ||
app.use('/', indexRouter); | ||
app.use('/auth', authRouter); | ||
app.use(`${process.env.API_ENPOINT_BASE}/users`, usersRouter); | ||
app.use(`${process.env.API_ENPOINT_BASE}/binaries`, binaryRouter); | ||
app.use(`${process.env.API_ENPOINT_BASE}/article`, articleRouter); | ||
app.use('/', indexRouter) | ||
app.use('/auth', authRouter) | ||
app.use(`${process.env.API_ENPOINT_BASE}/users`, usersRouter) | ||
app.use(`${process.env.API_ENPOINT_BASE}/binaries`, binaryRouter) | ||
app.use(`${process.env.API_ENPOINT_BASE}/article`, articleRouter) | ||
|
||
app.get(process.env.API_ENPOINT_BASE, (req, res) => { | ||
res.send(`CMS API v1.`) | ||
}); | ||
res.send(`CMS API v1.`) | ||
}) | ||
|
||
module.exports = app; | ||
module.exports = app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const crypto = require('crypto'); | ||
const jwt = require('jsonwebtoken') | ||
const crypto = require('crypto') | ||
|
||
exports.login = (req, res) => { | ||
try { | ||
let refreshId = req.body.userId + process.env.AUTH_SECRET; | ||
let salt = crypto.randomBytes(16).toString('base64'); | ||
let hash = crypto.createHmac('sha512', salt).update(refreshId).digest("base64"); | ||
req.body.refreshKey = salt; | ||
let token = jwt.sign(req.body, process.env.AUTH_SECRET); | ||
let b = new Buffer(hash); | ||
let refresh_token = b.toString('base64'); | ||
res.status(201).send({accessToken: token, refreshToken: refresh_token}); | ||
} catch (err) { | ||
res.status(500).send({errors: err}); | ||
} | ||
}; | ||
try { | ||
let refreshId = req.body.userId + process.env.AUTH_SECRET | ||
let salt = crypto.randomBytes(16).toString('base64') | ||
let hash = crypto | ||
.createHmac('sha512', salt) | ||
.update(refreshId) | ||
.digest('base64') | ||
req.body.refreshKey = salt | ||
let token = jwt.sign(req.body, process.env.AUTH_SECRET) | ||
let b = new Buffer(hash) | ||
let refresh_token = b.toString('base64') | ||
res.status(201).send({ accessToken: token, refreshToken: refresh_token }) | ||
} catch (err) { | ||
res.status(500).send({ errors: err }) | ||
} | ||
} | ||
|
||
exports.logout = (req, res) => { | ||
res.status(204).send({accessToken: null, refreshToken: null}); | ||
}; | ||
res.status(204).send({ accessToken: null, refreshToken: null }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,93 @@ | ||
const fs = require('fs'); | ||
const AWS = require('aws-sdk'); | ||
const mime = require('mime-types'); | ||
const {v4: uuidv4} = require('uuid'); | ||
const fs = require('fs') | ||
const AWS = require('aws-sdk') | ||
const mime = require('mime-types') | ||
const { v4: uuidv4 } = require('uuid') | ||
|
||
const ImageModel = require('../models/images.model'); | ||
const ImageModel = require('../models/images.model') | ||
|
||
AWS.config.update({ | ||
accessKeyId: process.env.IAM_ACCESS_KEY, | ||
secretAccessKey: process.env.IAM_SECRET_KEY, | ||
//region: process.env.AWS_REGION, | ||
}); | ||
accessKeyId: process.env.IAM_ACCESS_KEY, | ||
secretAccessKey: process.env.IAM_SECRET_KEY, | ||
//region: process.env.AWS_REGION, | ||
}) | ||
|
||
const s3 = new AWS.S3(); | ||
const s3 = new AWS.S3() | ||
|
||
exports.uploadImage = (req, res) => { | ||
let fileInfo = { | ||
source: req.file.path, | ||
key: `${uuidv4()}---${req.file.filename}`, | ||
caption: req.body.caption, | ||
contentType: mime.lookup(req.file.path), | ||
author: req.jwt.name, | ||
authorEmail: req.jwt.email | ||
}; | ||
uploadFile(fileInfo, res); | ||
}; | ||
let fileInfo = { | ||
source: req.file.path, | ||
key: `${uuidv4()}---${req.file.filename}`, | ||
caption: req.body.caption, | ||
contentType: mime.lookup(req.file.path), | ||
author: req.jwt.name, | ||
authorEmail: req.jwt.email, | ||
} | ||
uploadFile(fileInfo, res) | ||
} | ||
|
||
exports.retrieveImage = (req, res) => { | ||
let key = `${req.params.id}---${req.params.filename}`; | ||
retrieveFile(key, res); | ||
}; | ||
let key = `${req.params.id}---${req.params.filename}` | ||
retrieveFile(key, res) | ||
} | ||
|
||
let uploadFile = (fileInfo, res) => { | ||
console.log(fileInfo) | ||
|
||
fs.readFile(fileInfo.source, (err, fileData) => { | ||
if (err) { | ||
return res.status(500).send({error: err}); | ||
} | ||
|
||
const params = { | ||
Bucket: process.env.AWS_S3_BUCKET_BINARIES, | ||
Key: fileInfo.key, | ||
Body: fileData, | ||
ContentType: fileInfo.contentType, | ||
Metadata: { | ||
'Author': fileInfo.author, | ||
'Email': fileInfo.authorEmail | ||
} | ||
}; | ||
|
||
//Upload image to S3 bucket | ||
s3.upload(params, (err, data) => { | ||
if (err) { | ||
return res.status(500).send({error: err}); | ||
} | ||
|
||
let keys = fileInfo.key.split('---'); | ||
const imageData = { | ||
_id: keys[0], | ||
fileName: keys[1], | ||
caption: fileInfo.caption, | ||
author: fileInfo.author | ||
}; | ||
|
||
//After successful upload save the image info to Image Mongo model | ||
ImageModel.saveImage(imageData); | ||
|
||
//After successful upload delete the local image | ||
fs.unlink(fileInfo.source, (err) => { | ||
if (err) throw err; | ||
console.log(`${fileInfo.source} deleted.`) | ||
}); | ||
|
||
return res.send(data); | ||
}); | ||
}); | ||
}; | ||
console.log(fileInfo) | ||
|
||
fs.readFile(fileInfo.source, (err, fileData) => { | ||
if (err) { | ||
return res.status(500).send({ error: err }) | ||
} | ||
|
||
const params = { | ||
Bucket: process.env.AWS_S3_BUCKET_BINARIES, | ||
Key: fileInfo.key, | ||
Body: fileData, | ||
ContentType: fileInfo.contentType, | ||
Metadata: { | ||
Author: fileInfo.author, | ||
Email: fileInfo.authorEmail, | ||
}, | ||
} | ||
|
||
//Upload image to S3 bucket | ||
s3.upload(params, (err, data) => { | ||
if (err) { | ||
return res.status(500).send({ error: err }) | ||
} | ||
|
||
let keys = fileInfo.key.split('---') | ||
const imageData = { | ||
_id: keys[0], | ||
fileName: keys[1], | ||
caption: fileInfo.caption, | ||
author: fileInfo.author, | ||
} | ||
|
||
//After successful upload save the image info to Image Mongo model | ||
ImageModel.saveImage(imageData) | ||
|
||
//After successful upload delete the local image | ||
fs.unlink(fileInfo.source, (err) => { | ||
if (err) throw err | ||
console.log(`${fileInfo.source} deleted.`) | ||
}) | ||
|
||
return res.send(data) | ||
}) | ||
}) | ||
} | ||
|
||
let retrieveFile = (key, res) => { | ||
const getParams = { | ||
Bucket: process.env.AWS_S3_BUCKET_BINARIES, | ||
Key: key | ||
}; | ||
|
||
s3.getObject(getParams, function (err, data) { | ||
if (err) { | ||
return res.status(400).send({success: false, err: err}); | ||
} else { | ||
return res.send(data); | ||
} | ||
}); | ||
}; | ||
|
||
|
||
const getParams = { | ||
Bucket: process.env.AWS_S3_BUCKET_BINARIES, | ||
Key: key, | ||
} | ||
|
||
s3.getObject(getParams, function (err, data) { | ||
if (err) { | ||
return res.status(400).send({ success: false, err: err }) | ||
} else { | ||
return res.send(data) | ||
} | ||
}) | ||
} |
Oops, something went wrong.