Server can serve individual assets from projects (fixes #212, fixes #219)

This commit is contained in:
Zach Rispoli 2017-06-01 00:08:11 -04:00
parent 8e1a65daed
commit 61afce46ed
2 changed files with 31 additions and 0 deletions

View file

@ -123,6 +123,32 @@ export function deleteProject(req, res) {
}); });
} }
export function getProjectAsset(req, res) {
Project.findById(req.params.project_id)
.populate('user', 'username')
.exec((err, project) => {
if (err) {
return res.status(404).send({ message: 'Project with that id does not exist' });
}
var assetURL = null;
var seekFilename = req.params.asset_path;
project.files.forEach((file) => {
if(file.name === seekFilename) {
assetURL = file.url;
}
});
if(!assetURL) {
return res.status(404).send({ message: 'Asset does not exist' });
} else {
request({ method: 'GET', url: assetURL, encoding: null }, (err, response, body) => {
res.send(body);
});
}
});
}
export function getProjects(req, res) { export function getProjects(req, res) {
if (req.user) { if (req.user) {
Project.find({ user: req.user._id }) // eslint-disable-line no-underscore-dangle Project.find({ user: req.user._id }) // eslint-disable-line no-underscore-dangle

View file

@ -2,6 +2,7 @@ import { Router } from 'express';
import { renderIndex } from '../views/index'; import { renderIndex } from '../views/index';
import { get404Sketch } from '../views/404Page'; import { get404Sketch } from '../views/404Page';
import { userExists } from '../controllers/user.controller'; import { userExists } from '../controllers/user.controller';
import { getProjectAsset } from '../controllers/project.controller';
const router = new Router(); const router = new Router();
@ -24,6 +25,10 @@ router.route('/:username/sketches/:project_id').get((req, res) => {
res.send(renderIndex()); res.send(renderIndex());
}); });
router.route('/:username/sketches/:project_id/:asset_path').get((req, res) => {
getProjectAsset(req,res);
});
// router.route('/full/:project_id').get((req, res) => { // router.route('/full/:project_id').get((req, res) => {
// res.send(renderIndex()); // res.send(renderIndex());
// }); // });