2016-11-02 18:08:53 +00:00
|
|
|
import archiver from 'archiver';
|
2019-03-02 09:35:40 +00:00
|
|
|
import format from 'date-fns/format';
|
2017-07-19 17:56:52 +00:00
|
|
|
import isUrl from 'is-url';
|
|
|
|
import jsdom, { serializeDocument } from 'jsdom';
|
2019-04-17 18:08:33 +00:00
|
|
|
import isAfter from 'date-fns/is_after';
|
2019-03-02 09:35:40 +00:00
|
|
|
import request from 'request';
|
|
|
|
import slugify from 'slugify';
|
2017-02-22 19:29:35 +00:00
|
|
|
import Project from '../models/project';
|
|
|
|
import User from '../models/user';
|
2019-03-02 09:35:40 +00:00
|
|
|
import { resolvePathToFile } from '../utils/filePath';
|
|
|
|
import generateFileSystemSafeName from '../utils/generateFileSystemSafeName';
|
2016-06-17 18:11:52 +00:00
|
|
|
|
2019-08-30 18:26:57 +00:00
|
|
|
export { default as createProject, apiCreateProject } from './project.controller/createProject';
|
|
|
|
export { default as deleteProject } from './project.controller/deleteProject';
|
|
|
|
export { default as getProjectsForUser, apiGetProjectsForUser } from './project.controller/getProjectsForUser';
|
2016-06-18 22:33:49 +00:00
|
|
|
|
|
|
|
export function updateProject(req, res) {
|
2017-02-22 19:29:35 +00:00
|
|
|
Project.findById(req.params.project_id, (findProjectErr, project) => {
|
2018-01-09 20:57:49 +00:00
|
|
|
if (!project.user.equals(req.user._id)) {
|
2017-02-22 19:29:35 +00:00
|
|
|
res.status(403).send({ success: false, message: 'Session does not match owner of project.' });
|
|
|
|
return;
|
2017-01-24 20:29:25 +00:00
|
|
|
}
|
2019-04-17 18:08:33 +00:00
|
|
|
if (req.body.updatedAt && isAfter(new Date(project.updatedAt), req.body.updatedAt)) {
|
|
|
|
res.status(409).send({ success: false, message: 'Attempted to save stale version of project.' });
|
|
|
|
return;
|
|
|
|
}
|
2018-05-05 00:22:39 +00:00
|
|
|
Project.findByIdAndUpdate(
|
|
|
|
req.params.project_id,
|
2017-01-13 22:17:31 +00:00
|
|
|
{
|
|
|
|
$set: req.body
|
2017-03-02 19:38:29 +00:00
|
|
|
},
|
|
|
|
{
|
2020-08-03 20:42:28 +00:00
|
|
|
new: true,
|
|
|
|
runValidators: true
|
2018-05-05 00:22:39 +00:00
|
|
|
}
|
|
|
|
)
|
2017-01-13 22:17:31 +00:00
|
|
|
.populate('user', 'username')
|
2017-02-22 19:29:35 +00:00
|
|
|
.exec((updateProjectErr, updatedProject) => {
|
|
|
|
if (updateProjectErr) {
|
|
|
|
console.log(updateProjectErr);
|
2020-08-03 20:42:28 +00:00
|
|
|
res.status(400).json({ success: false });
|
2017-02-22 19:29:35 +00:00
|
|
|
return;
|
2017-01-13 22:17:31 +00:00
|
|
|
}
|
2019-06-19 20:21:25 +00:00
|
|
|
if (req.body.files && updatedProject.files.length !== req.body.files.length) {
|
2017-01-13 22:17:31 +00:00
|
|
|
const oldFileIds = updatedProject.files.map(file => file.id);
|
|
|
|
const newFileIds = req.body.files.map(file => file.id);
|
|
|
|
const staleIds = oldFileIds.filter(id => newFileIds.indexOf(id) === -1);
|
2017-02-22 19:29:35 +00:00
|
|
|
staleIds.forEach((staleId) => {
|
2017-01-13 22:17:31 +00:00
|
|
|
updatedProject.files.id(staleId).remove();
|
|
|
|
});
|
2017-03-02 19:38:29 +00:00
|
|
|
updatedProject.save((innerErr, savedProject) => {
|
2017-01-13 22:17:31 +00:00
|
|
|
if (innerErr) {
|
|
|
|
console.log(innerErr);
|
2020-08-03 20:42:28 +00:00
|
|
|
res.status(400).json({ success: false });
|
2017-02-22 19:29:35 +00:00
|
|
|
return;
|
2017-01-13 22:17:31 +00:00
|
|
|
}
|
2017-03-02 19:38:29 +00:00
|
|
|
res.json(savedProject);
|
2017-01-13 22:17:31 +00:00
|
|
|
});
|
2019-02-20 21:27:28 +00:00
|
|
|
} else {
|
|
|
|
res.json(updatedProject);
|
2017-01-13 22:17:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2016-06-18 22:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getProject(req, res) {
|
2020-05-26 21:35:13 +00:00
|
|
|
const { project_id: projectId, username } = req.params;
|
2020-07-15 21:33:11 +00:00
|
|
|
User.findByUsername(username, (err, user) => { // eslint-disable-line
|
2020-05-26 21:35:13 +00:00
|
|
|
if (!user) {
|
|
|
|
return res.status(404).send({ message: 'Project with that username does not exist' });
|
|
|
|
}
|
|
|
|
Project.findOne({ user: user._id, $or: [{ _id: projectId }, { slug: projectId }] })
|
|
|
|
.populate('user', 'username')
|
|
|
|
.exec((err, project) => { // eslint-disable-line
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return res.status(404).send({ message: 'Project with that id does not exist' });
|
|
|
|
}
|
2018-02-07 21:00:09 +00:00
|
|
|
return res.json(project);
|
2020-05-26 21:35:13 +00:00
|
|
|
});
|
|
|
|
});
|
2016-06-23 22:29:55 +00:00
|
|
|
}
|
2016-07-01 15:30:40 +00:00
|
|
|
|
2017-07-11 15:37:43 +00:00
|
|
|
export function getProjectsForUserId(userId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Project.find({ user: userId })
|
2016-07-01 15:30:40 +00:00
|
|
|
.sort('-createdAt')
|
2016-08-09 22:45:59 +00:00
|
|
|
.select('name files id createdAt updatedAt')
|
2016-07-01 15:30:40 +00:00
|
|
|
.exec((err, projects) => {
|
2017-07-11 15:37:43 +00:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
resolve(projects);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-01 04:08:11 +00:00
|
|
|
export function getProjectAsset(req, res) {
|
|
|
|
Project.findById(req.params.project_id)
|
|
|
|
.populate('user', 'username')
|
2017-10-16 03:27:05 +00:00
|
|
|
.exec((err, project) => { // eslint-disable-line
|
2017-06-01 04:08:11 +00:00
|
|
|
if (err) {
|
|
|
|
return res.status(404).send({ message: 'Project with that id does not exist' });
|
|
|
|
}
|
2017-11-27 20:14:50 +00:00
|
|
|
if (!project) {
|
|
|
|
return res.status(404).send({ message: 'Project with that id does not exist' });
|
|
|
|
}
|
2017-06-01 04:08:11 +00:00
|
|
|
|
2019-02-22 23:05:56 +00:00
|
|
|
const filePath = req.params[0];
|
|
|
|
const resolvedFile = resolvePathToFile(filePath, project.files);
|
|
|
|
if (!resolvedFile) {
|
2017-06-01 04:08:11 +00:00
|
|
|
return res.status(404).send({ message: 'Asset does not exist' });
|
|
|
|
}
|
2019-02-22 23:05:56 +00:00
|
|
|
if (!resolvedFile.url) {
|
|
|
|
return res.send(resolvedFile.content);
|
|
|
|
}
|
|
|
|
request({ method: 'GET', url: resolvedFile.url, encoding: null }, (innerErr, response, body) => {
|
2017-10-16 03:27:05 +00:00
|
|
|
if (innerErr) {
|
|
|
|
return res.status(404).send({ message: 'Asset does not exist' });
|
|
|
|
}
|
|
|
|
return res.send(body);
|
|
|
|
});
|
2017-06-01 04:08:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-11 15:37:43 +00:00
|
|
|
export function getProjects(req, res) {
|
|
|
|
if (req.user) {
|
|
|
|
getProjectsForUserId(req.user._id)
|
|
|
|
.then((projects) => {
|
2016-07-01 15:30:40 +00:00
|
|
|
res.json(projects);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// could just move this to client side
|
2017-02-22 19:29:35 +00:00
|
|
|
res.json([]);
|
2016-07-01 15:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-17 19:53:25 +00:00
|
|
|
|
2018-02-19 20:21:47 +00:00
|
|
|
export function projectExists(projectId, callback) {
|
|
|
|
Project.findById(projectId, (err, project) => (
|
|
|
|
project ? callback(true) : callback(false)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function projectForUserExists(username, projectId, callback) {
|
2020-07-15 21:33:11 +00:00
|
|
|
User.findByUsername(username, (err, user) => {
|
2018-02-19 20:21:47 +00:00
|
|
|
if (!user) {
|
|
|
|
callback(false);
|
|
|
|
return;
|
|
|
|
}
|
2020-05-26 21:35:13 +00:00
|
|
|
Project.findOne({ user: user._id, $or: [{ _id: projectId }, { slug: projectId }] }, (innerErr, project) => {
|
2018-02-20 19:16:58 +00:00
|
|
|
if (project) {
|
|
|
|
callback(true);
|
|
|
|
}
|
|
|
|
});
|
2018-02-19 20:21:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-19 17:56:52 +00:00
|
|
|
function bundleExternalLibs(project, zip, callback) {
|
2020-04-15 21:39:28 +00:00
|
|
|
const indexHtml = project.files.find(file => file.name.match(/\.html$/));
|
2017-07-19 17:56:52 +00:00
|
|
|
let numScriptsResolved = 0;
|
|
|
|
let numScriptTags = 0;
|
|
|
|
|
|
|
|
function resolveScriptTagSrc(scriptTag, document) {
|
|
|
|
const path = scriptTag.src.split('/');
|
|
|
|
const filename = path[path.length - 1];
|
2018-05-05 00:59:43 +00:00
|
|
|
const { src } = scriptTag;
|
2017-07-19 17:56:52 +00:00
|
|
|
|
|
|
|
if (!isUrl(src)) {
|
|
|
|
numScriptsResolved += 1;
|
2019-05-02 23:33:16 +00:00
|
|
|
if (numScriptsResolved === numScriptTags) {
|
|
|
|
indexHtml.content = serializeDocument(document);
|
|
|
|
callback();
|
|
|
|
}
|
2017-07-19 17:56:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request({ method: 'GET', url: src, encoding: null }, (err, response, body) => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
} else {
|
|
|
|
zip.append(body, { name: filename });
|
|
|
|
scriptTag.src = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
numScriptsResolved += 1;
|
|
|
|
if (numScriptsResolved === numScriptTags) {
|
|
|
|
indexHtml.content = serializeDocument(document);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
jsdom.env(indexHtml.content, (innerErr, window) => {
|
|
|
|
const indexHtmlDoc = window.document;
|
|
|
|
const scriptTags = indexHtmlDoc.getElementsByTagName('script');
|
|
|
|
numScriptTags = scriptTags.length;
|
|
|
|
for (let i = 0; i < numScriptTags; i += 1) {
|
|
|
|
resolveScriptTagSrc(scriptTags[i], indexHtmlDoc);
|
|
|
|
}
|
2019-05-02 23:33:16 +00:00
|
|
|
if (numScriptTags === 0) {
|
|
|
|
indexHtml.content = serializeDocument(document);
|
|
|
|
callback();
|
|
|
|
}
|
2017-07-19 17:56:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:08:53 +00:00
|
|
|
function buildZip(project, req, res) {
|
|
|
|
const zip = archiver('zip');
|
|
|
|
const rootFile = project.files.find(file => file.name === 'root');
|
|
|
|
const numFiles = project.files.filter(file => file.fileType !== 'folder').length;
|
2018-05-05 00:59:43 +00:00
|
|
|
const { files } = project;
|
2016-11-02 18:08:53 +00:00
|
|
|
let numCompletedFiles = 0;
|
|
|
|
|
2017-01-24 22:20:40 +00:00
|
|
|
zip.on('error', (err) => {
|
2016-11-17 16:15:35 +00:00
|
|
|
res.status(500).send({ error: err.message });
|
2016-11-02 18:08:53 +00:00
|
|
|
});
|
|
|
|
|
2019-03-02 09:35:40 +00:00
|
|
|
const currentTime = format(new Date(), 'YYYY_MM_DD_HH_mm_ss');
|
2019-02-10 01:27:03 +00:00
|
|
|
project.slug = slugify(project.name, '_');
|
|
|
|
res.attachment(`${generateFileSystemSafeName(project.slug)}_${currentTime}.zip`);
|
2016-11-02 18:08:53 +00:00
|
|
|
zip.pipe(res);
|
|
|
|
|
|
|
|
function addFileToZip(file, path) {
|
|
|
|
if (file.fileType === 'folder') {
|
|
|
|
const newPath = file.name === 'root' ? path : `${path}${file.name}/`;
|
2017-02-22 19:29:35 +00:00
|
|
|
file.children.forEach((fileId) => {
|
2016-11-02 18:08:53 +00:00
|
|
|
const childFile = files.find(f => f.id === fileId);
|
|
|
|
(() => {
|
|
|
|
addFileToZip(childFile, newPath);
|
|
|
|
})();
|
|
|
|
});
|
2017-02-22 19:29:35 +00:00
|
|
|
} else if (file.url) {
|
|
|
|
request({ method: 'GET', url: file.url, encoding: null }, (err, response, body) => {
|
|
|
|
zip.append(body, { name: `${path}${file.name}` });
|
2016-11-02 18:08:53 +00:00
|
|
|
numCompletedFiles += 1;
|
|
|
|
if (numCompletedFiles === numFiles) {
|
|
|
|
zip.finalize();
|
|
|
|
}
|
2017-02-22 19:29:35 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
zip.append(file.content, { name: `${path}${file.name}` });
|
|
|
|
numCompletedFiles += 1;
|
|
|
|
if (numCompletedFiles === numFiles) {
|
|
|
|
zip.finalize();
|
2016-11-02 18:08:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 17:56:52 +00:00
|
|
|
|
|
|
|
bundleExternalLibs(project, zip, () => {
|
|
|
|
addFileToZip(rootFile, '/');
|
|
|
|
});
|
2016-11-02 18:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function downloadProjectAsZip(req, res) {
|
|
|
|
Project.findById(req.params.project_id, (err, project) => {
|
2016-11-17 16:15:35 +00:00
|
|
|
// save project to some path
|
2016-11-02 18:08:53 +00:00
|
|
|
buildZip(project, req, res);
|
|
|
|
});
|
|
|
|
}
|