framez-server/Server/API/Routers/FrameRouter.js

182 lines
4.8 KiB
JavaScript

const express = require("express");
const DBUtils = require('../../Utils/DBUtil')
const AuthUtil = require('../../Utils/AuthUtil')
const bodyParser = require('body-parser');
const router = express.Router();
router.use(bodyParser.raw({
uploadDir: '/tmp/uploads',
keepExtensions: true,
limit: '5mb',
type: 'image/*'
}))
router.get('/:frameId', (req, res) => { // by Frame Id
const token = req.get('token');
const frameId = req.params.frameId;
AuthUtil.getAccountByToken(token)
.then((account) => {
if(account.frames.indexOf(frameId) >= 0) {
DBUtils.Models.Frame.findOne({_id: frameId}, (err, doc) => {
if(err) {
res.status(400).json({
message: err.message
});
return;
} else if(!doc) {
res.status(400).json({
message: 'Unable to find a Frame with id: ' + frameId
});
return;
}
const frame = doc.toObject();
/// lets get all images ids...
DBUtils.Models.Photo.find({frame_id: frameId}, (err, docs)=>{
if(err) {
res.status(400).json({
message: err.message
});
return;
}
console.log(docs);
frame.photos = docs.map(p => p._id);
res.json(frame);
})
});
}else{
res.status(403).json({
message: 'Account has no access to frame with id of: ' + frameId
});
}
})
.catch((reason) => {
res.status(401).json({
message: reason
});
})
})
router.post('/create', (req, res) => {
const token = req.get('token');
const body = req.body;
if(!body.name) {
res.status(400).json({
message: 'Must provide a name for your new frame'
});
return;
}
AuthUtil.getAccountByToken(token)
.then((account)=>{
// If account valid - create new frame
const frame = new DBUtils.Models.Frame({
name: body.name,
admin: account._id,
members: [account._id]
});
// save frame
frame.save((err, doc)=>{
if(err) {
res.status(400).json({
message: err.message
});
return;
}
// frame created - now add its id to the account object
account.frames.push(doc._id);
account.save((err)=>{
if( err ) throw err;
res.status(201).json(frame);
})
})
})
.catch((reason)=>{
res.status(401).json({
message: reason
});
})
});
router.post('/:frameId/upload/photo', (req, res) => {
const token = req.get('token');
AuthUtil.getAccountByToken(token)
.then(account => {
const frameId = req.params.frameId;
if(account.frames.indexOf(frameId) >= 0) {
// User can upload image to the frame
AuthUtil.getUserByAccountId(account._id)
.then((user)=>{
// Upload Photo...
const photo = new DBUtils.Models.Photo({
frame_id: frameId,
photo: req.body,
timestamp: Date.now(),
contentType: req.get('Content-Type'),
user:user.id});
// Save photo
photo.save((err) => {
if(err) {
res.status(400).json({
message: err.message
});
return;
}
res.status(201).json(photo)
});
})
.catch(reason => {
res.status(500).json({
message: 'Unexpected error: ' + reason
});
})
} else {
res.status(403).json({
message: 'Account has no access to frame with id of: ' + frameId
});
}
})
.catch(reason => {
res.status(401).json({
message: reason
});
})
});
router.get('/:frameId/download/photo/:photoId', (req, res) => {
const token = req.get('token');
const photoId = req.params.photoId;
const frameId = req.params.frameId;
AuthUtil.getAccountByToken(token)
.then((account) => {
if(account.frames.indexOf(frameId) >= 0) {
DBUtils.Models.Photo.findOne({_id: photoId}, (err, doc) => {
if(err) {
res.status(400).json({
message: err.message
});
return;
}
if(doc) res.contentType(doc.contentType).send(doc.photo);
else res.status(400).json({
message: 'Photo not found'
});
});
} else {
res.status(403).json({
message: 'Account has no access to frame with id of: ' + photoId
});
}
})
.catch(reason => {
res.status(401).json({
message: reason
});
})
})
module.exports = router;