From 03d7533e2a2d974454f19ca3b304f3f7304face8 Mon Sep 17 00:00:00 2001 From: Oliver Wright Date: Fri, 22 Feb 2019 23:05:56 +0000 Subject: [PATCH] Serve text-type assets from sketch asset urls - Fixes #839 (#843) --- server/controllers/project.controller.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index 9e510e4b..c8899a38 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -4,6 +4,7 @@ import moment from 'moment'; import isUrl from 'is-url'; import slugify from 'slugify'; import jsdom, { serializeDocument } from 'jsdom'; +import { resolvePathToFile } from '../utils/filePath'; import generateFileSystemSafeName from '../utils/generateFileSystemSafeName'; import Project from '../models/project'; import User from '../models/user'; @@ -159,20 +160,15 @@ export function getProjectAsset(req, res) { return res.status(404).send({ message: 'Project with that id does not exist' }); } - let assetURL = null; - const seekPath = req.params[0]; // req.params.asset_path; - const seekPathSplit = seekPath.split('/'); - const seekFilename = seekPathSplit[seekPathSplit.length - 1]; - project.files.forEach((file) => { - if (file.name === seekFilename) { - assetURL = file.url; - } - }); - - if (!assetURL) { + const filePath = req.params[0]; + const resolvedFile = resolvePathToFile(filePath, project.files); + if (!resolvedFile) { return res.status(404).send({ message: 'Asset does not exist' }); } - request({ method: 'GET', url: assetURL, encoding: null }, (innerErr, response, body) => { + if (!resolvedFile.url) { + return res.send(resolvedFile.content); + } + request({ method: 'GET', url: resolvedFile.url, encoding: null }, (innerErr, response, body) => { if (innerErr) { return res.status(404).send({ message: 'Asset does not exist' }); }