From 61afce46ed888ef4411d759f9d49315925f3ca9a Mon Sep 17 00:00:00 2001 From: Zach Rispoli Date: Thu, 1 Jun 2017 00:08:11 -0400 Subject: [PATCH 1/3] Server can serve individual assets from projects (fixes #212, fixes #219) --- server/controllers/project.controller.js | 26 ++++++++++++++++++++++++ server/routes/server.routes.js | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 84b2afa1..9dae16b5 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -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) { if (req.user) { Project.find({ user: req.user._id }) // eslint-disable-line no-underscore-dangle diff --git a/server/routes/server.routes.js b/server/routes/server.routes.js index 1b69dbda..d4f9f6cb 100644 --- a/server/routes/server.routes.js +++ b/server/routes/server.routes.js @@ -2,6 +2,7 @@ import { Router } from 'express'; import { renderIndex } from '../views/index'; import { get404Sketch } from '../views/404Page'; import { userExists } from '../controllers/user.controller'; +import { getProjectAsset } from '../controllers/project.controller'; const router = new Router(); @@ -24,6 +25,10 @@ router.route('/:username/sketches/:project_id').get((req, res) => { 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) => { // res.send(renderIndex()); // }); From 396fc701c7e18bc28900ecc53e6f0d9dd7e73e02 Mon Sep 17 00:00:00 2001 From: Zach Rispoli Date: Mon, 12 Jun 2017 13:49:45 -0400 Subject: [PATCH 2/3] Fix issue with serving assets inside folders --- client/modules/IDE/components/PreviewFrame.jsx | 5 +++-- server/controllers/project.controller.js | 4 +++- server/routes/server.routes.js | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/client/modules/IDE/components/PreviewFrame.jsx b/client/modules/IDE/components/PreviewFrame.jsx index 33af7507..55c79e7b 100644 --- a/client/modules/IDE/components/PreviewFrame.jsx +++ b/client/modules/IDE/components/PreviewFrame.jsx @@ -249,7 +249,8 @@ class PreviewFrame extends React.Component { let jsFileStrings = content.match(STRING_REGEX); jsFileStrings = jsFileStrings || []; jsFileStrings.forEach((jsFileString) => { - if (jsFileString.match(MEDIA_FILE_REGEX)) { + console.log(this.props.setBlobUrl); + /* if (jsFileString.match(MEDIA_FILE_REGEX)) { const filePath = jsFileString.substr(1, jsFileString.length - 2); const resolvedFile = resolvePathToFile(filePath, files); if (resolvedFile) { @@ -262,7 +263,7 @@ class PreviewFrame extends React.Component { newContent = newContent.replace(filePath, blobURL); } } - } + }*/ }); newContent = loopProtect(newContent); return newContent; diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 9dae16b5..baa20df9 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -132,7 +132,9 @@ export function getProjectAsset(req, res) { } var assetURL = null; - var seekFilename = req.params.asset_path; + var seekPath = req.params[0]; // req.params.asset_path; + var seekPathSplit = seekPath.split('/'); + var seekFilename = seekPathSplit[seekPathSplit.length-1]; project.files.forEach((file) => { if(file.name === seekFilename) { assetURL = file.url; diff --git a/server/routes/server.routes.js b/server/routes/server.routes.js index d4f9f6cb..d5c6064e 100644 --- a/server/routes/server.routes.js +++ b/server/routes/server.routes.js @@ -25,7 +25,7 @@ router.route('/:username/sketches/:project_id').get((req, res) => { res.send(renderIndex()); }); -router.route('/:username/sketches/:project_id/:asset_path').get((req, res) => { +router.route('/:username/sketches/:project_id/*').get((req, res) => { getProjectAsset(req,res); }); From b194a2456460f8bb79c4c165e497a7ddc6559df4 Mon Sep 17 00:00:00 2001 From: Zach Rispoli Date: Wed, 26 Jul 2017 14:04:24 -0400 Subject: [PATCH 3/3] Re-enable serving text files as blobs + Remove comments --- client/modules/IDE/components/PreviewFrame.jsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/client/modules/IDE/components/PreviewFrame.jsx b/client/modules/IDE/components/PreviewFrame.jsx index 55c79e7b..33af7507 100644 --- a/client/modules/IDE/components/PreviewFrame.jsx +++ b/client/modules/IDE/components/PreviewFrame.jsx @@ -249,8 +249,7 @@ class PreviewFrame extends React.Component { let jsFileStrings = content.match(STRING_REGEX); jsFileStrings = jsFileStrings || []; jsFileStrings.forEach((jsFileString) => { - console.log(this.props.setBlobUrl); - /* if (jsFileString.match(MEDIA_FILE_REGEX)) { + if (jsFileString.match(MEDIA_FILE_REGEX)) { const filePath = jsFileString.substr(1, jsFileString.length - 2); const resolvedFile = resolvePathToFile(filePath, files); if (resolvedFile) { @@ -263,7 +262,7 @@ class PreviewFrame extends React.Component { newContent = newContent.replace(filePath, blobURL); } } - }*/ + } }); newContent = loopProtect(newContent); return newContent;