#189 load text files from server via filename, does not work for file paths

This commit is contained in:
Cassie Tarakajian 2016-11-08 14:42:23 -05:00
parent 77e2f5bfff
commit 081c7d521e
3 changed files with 26 additions and 4 deletions

View file

@ -2,7 +2,7 @@ import Project from '../models/project';
import escapeStringRegexp from 'escape-string-regexp'; import escapeStringRegexp from 'escape-string-regexp';
const startTag = '@fs-'; const startTag = '@fs-';
function injectMediaUrls(filesToInject, mediaFiles) { function injectMediaUrls(filesToInject, mediaFiles, textFiles, projectId) {
filesToInject.forEach(file => { filesToInject.forEach(file => {
let fileStrings = file.content.match(/(['"])((\\\1|.)*?)\1/gm); let fileStrings = file.content.match(/(['"])((\\\1|.)*?)\1/gm);
const fileStringRegex = /^('|")(?!(http:\/\/|https:\/\/)).*('|")$/i; const fileStringRegex = /^('|")(?!(http:\/\/|https:\/\/)).*('|")$/i;
@ -17,7 +17,14 @@ function injectMediaUrls(filesToInject, mediaFiles) {
if (mediaFile.name === fileName) { if (mediaFile.name === fileName) {
file.content = file.content.replace(filePath, mediaFile.url); 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 jsFiles = files.filter(file => file.name.match(/\.js$/i));
const cssFiles = files.filter(file => file.name.match(/\.css$/i)); const cssFiles = files.filter(file => file.name.match(/\.css$/i));
const mediaFiles = files.filter(file => file.url); 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); injectMediaUrls(cssFiles, mediaFiles);
jsFiles.forEach(jsFile => { jsFiles.forEach(jsFile => {

View file

@ -65,3 +65,16 @@ export function deleteFile(req, res) {
}) })
}); });
} }
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);
});
}

View file

@ -5,5 +5,6 @@ const router = new Router();
router.route('/projects/:project_id/files').post(FileController.createFile); 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/files/:file_id').delete(FileController.deleteFile);
router.route('/projects/:project_id/:file_name').get(FileController.getFileContent);
export default router; export default router;