diff --git a/server/controllers/embed.controller.js b/server/controllers/embed.controller.js index b0e33b94..bdbb221b 100644 --- a/server/controllers/embed.controller.js +++ b/server/controllers/embed.controller.js @@ -2,7 +2,7 @@ import Project from '../models/project'; import escapeStringRegexp from 'escape-string-regexp'; const startTag = '@fs-'; -function injectMediaUrls(filesToInject, mediaFiles) { +function injectMediaUrls(filesToInject, mediaFiles, textFiles, projectId) { filesToInject.forEach(file => { let fileStrings = file.content.match(/(['"])((\\\1|.)*?)\1/gm); const fileStringRegex = /^('|")(?!(http:\/\/|https:\/\/)).*('|")$/i; @@ -17,7 +17,14 @@ function injectMediaUrls(filesToInject, mediaFiles) { if (mediaFile.name === fileName) { file.content = file.content.replace(filePath, mediaFile.url); } - }) + }); + if (textFiles) { + textFiles.forEach(textFile => { + if (textFile.name === fileName) { + file.content = file.content.replace(filePath, `/api/projects/${projectId}/${textFile.name}`); + } + }); + } } }); }); @@ -31,8 +38,9 @@ export function serveProject(req, res) { const jsFiles = files.filter(file => file.name.match(/\.js$/i)); const cssFiles = files.filter(file => file.name.match(/\.css$/i)); const mediaFiles = files.filter(file => file.url); + const textFiles = files.filter(file => file.name.match(/(.+\.json$|.+\.txt$|.+\.csv$)/i) && file.url === undefined); - injectMediaUrls(jsFiles, mediaFiles); + injectMediaUrls(jsFiles, mediaFiles, textFiles, req.params.project_id); injectMediaUrls(cssFiles, mediaFiles); jsFiles.forEach(jsFile => { diff --git a/server/controllers/file.controller.js b/server/controllers/file.controller.js index 542db026..428ddc5c 100644 --- a/server/controllers/file.controller.js +++ b/server/controllers/file.controller.js @@ -64,4 +64,17 @@ export function deleteFile(req, res) { res.json(project.files); }) }); -} \ No newline at end of file +} + +export function getFileContent(req, res) { + Project.findById(req.params.project_id, (err, project) => { + if (err) { + return res.status(404).send({success: false, message: 'Project with that id does not exist.'}); + } + const file = project.files.find(file => file.name === req.params.file_name); + if (!file) { + return res.status(404).send({success: false, message: 'File with that name does not exist.'}); + } + res.send(file.content); + }); +} diff --git a/server/routes/file.routes.js b/server/routes/file.routes.js index 8870208e..d4fb5764 100644 --- a/server/routes/file.routes.js +++ b/server/routes/file.routes.js @@ -5,5 +5,6 @@ const router = new Router(); router.route('/projects/:project_id/files').post(FileController.createFile); router.route('/projects/:project_id/files/:file_id').delete(FileController.deleteFile); +router.route('/projects/:project_id/:file_name').get(FileController.getFileContent); export default router; \ No newline at end of file