p5.js-web-editor/server/controllers/project.controller.js

339 lines
9.9 KiB
JavaScript
Raw Normal View History

import archiver from 'archiver';
2019-03-02 10:35:40 +01:00
import format from 'date-fns/format';
import isUrl from 'is-url';
import jsdom, { serializeDocument } from 'jsdom';
2019-03-02 10:35:40 +01:00
import isBefore from 'date-fns/is_before';
import request from 'request';
import slugify from 'slugify';
import Project from '../models/project';
import User from '../models/user';
2019-03-02 10:35:40 +01:00
import { resolvePathToFile } from '../utils/filePath';
import generateFileSystemSafeName from '../utils/generateFileSystemSafeName';
import { deleteObjectsFromS3, getObjectKey } from './aws.controller';
2016-06-17 20:11:52 +02:00
export function createProject(req, res) {
2016-07-15 19:36:33 +02:00
let projectValues = {
user: req.user._id
2016-06-24 00:29:55 +02:00
};
2016-06-20 22:29:08 +02:00
2016-07-15 19:36:33 +02:00
projectValues = Object.assign(projectValues, req.body);
2016-06-24 00:29:55 +02:00
Project.create(projectValues, (err, newProject) => {
if (err) {
res.json({ success: false });
return;
}
2018-05-05 02:22:39 +02:00
Project.populate(
newProject,
{ path: 'user', select: 'username' },
2016-07-15 17:54:47 +02:00
(innerErr, newProjectWithUser) => {
if (innerErr) {
res.json({ success: false });
return;
}
res.json(newProjectWithUser);
2018-05-05 02:22:39 +02:00
}
);
2016-06-24 00:29:55 +02:00
});
2016-06-19 00:33:49 +02:00
}
export function updateProject(req, res) {
Project.findById(req.params.project_id, (findProjectErr, project) => {
if (!project.user.equals(req.user._id)) {
res.status(403).send({ success: false, message: 'Session does not match owner of project.' });
return;
}
2017-03-15 16:59:07 +01:00
// if (req.body.updatedAt && moment(req.body.updatedAt) < moment(project.updatedAt)) {
// res.status(409).send({ success: false, message: 'Attempted to save stale version of project.' });
// return;
// }
2018-05-05 02:22:39 +02:00
Project.findByIdAndUpdate(
req.params.project_id,
{
$set: req.body
},
{
new: true
2018-05-05 02:22:39 +02:00
}
)
.populate('user', 'username')
.exec((updateProjectErr, updatedProject) => {
if (updateProjectErr) {
console.log(updateProjectErr);
res.json({ success: false });
return;
}
if (updatedProject.files.length !== req.body.files.length) {
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);
staleIds.forEach((staleId) => {
updatedProject.files.id(staleId).remove();
});
updatedProject.save((innerErr, savedProject) => {
if (innerErr) {
console.log(innerErr);
res.json({ success: false });
return;
}
res.json(savedProject);
});
} else {
res.json(updatedProject);
}
});
});
2016-06-19 00:33:49 +02:00
}
export function getProject(req, res) {
2018-02-07 22:00:09 +01:00
const projectId = req.params.project_id;
Project.findById(projectId)
2016-07-15 17:54:47 +02:00
.populate('user', 'username')
2018-02-07 22:00:09 +01:00
.exec((err, project) => { // eslint-disable-line
2016-07-15 17:54:47 +02:00
if (err) {
return res.status(404).send({ message: 'Project with that id does not exist' });
2018-02-07 22:00:09 +01:00
} else if (!project) {
Project.findOne({ slug: projectId })
2018-05-05 02:22:39 +02:00
.populate('user', 'username')
.exec((innerErr, projectBySlug) => {
if (innerErr || !projectBySlug) {
return res.status(404).send({ message: 'Project with that id does not exist' });
}
return res.json(projectBySlug);
});
2018-02-07 22:00:09 +01:00
} else {
return res.json(project);
2016-07-15 17:54:47 +02:00
}
});
2016-06-24 00:29:55 +02:00
}
2016-07-01 17:30:40 +02:00
function deleteFilesFromS3(files) {
2018-05-05 02:22:39 +02:00
deleteObjectsFromS3(files.filter((file) => {
if (file.url) {
if (!process.env.S3_DATE || (
process.env.S3_DATE &&
isBefore(new Date(process.env.S3_DATE), new Date(file.createdAt)))) {
2018-05-05 02:22:39 +02:00
return true;
}
2018-05-05 02:22:39 +02:00
}
return false;
})
.map(file => getObjectKey(file.url)));
}
export function deleteProject(req, res) {
Project.findById(req.params.project_id, (findProjectErr, project) => {
if (!project.user.equals(req.user._id)) {
res.status(403).json({ success: false, message: 'Session does not match owner of project.' });
return;
}
deleteFilesFromS3(project.files);
Project.remove({ _id: req.params.project_id }, (removeProjectError) => {
if (removeProjectError) {
res.status(404).send({ message: 'Project with that id does not exist' });
return;
}
res.json({ success: true });
});
});
}
export function getProjectsForUserId(userId) {
return new Promise((resolve, reject) => {
Project.find({ user: userId })
2016-07-01 17:30:40 +02:00
.sort('-createdAt')
.select('name files id createdAt updatedAt')
2016-07-01 17:30:40 +02:00
.exec((err, projects) => {
if (err) {
console.log(err);
}
resolve(projects);
});
});
}
export function getProjectAsset(req, res) {
Project.findById(req.params.project_id)
.populate('user', 'username')
2017-10-16 05:27:05 +02:00
.exec((err, project) => { // eslint-disable-line
if (err) {
return res.status(404).send({ message: 'Project with that id does not exist' });
}
2017-11-27 21:14:50 +01:00
if (!project) {
return res.status(404).send({ message: 'Project with that id does not exist' });
}
const filePath = req.params[0];
const resolvedFile = resolvePathToFile(filePath, project.files);
if (!resolvedFile) {
return res.status(404).send({ message: 'Asset does not exist' });
}
if (!resolvedFile.url) {
return res.send(resolvedFile.content);
}
request({ method: 'GET', url: resolvedFile.url, encoding: null }, (innerErr, response, body) => {
2017-10-16 05:27:05 +02:00
if (innerErr) {
return res.status(404).send({ message: 'Asset does not exist' });
}
return res.send(body);
});
});
}
export function getProjectsForUserName(username) {
2017-10-16 05:27:05 +02:00
}
export function getProjects(req, res) {
if (req.user) {
getProjectsForUserId(req.user._id)
.then((projects) => {
2016-07-01 17:30:40 +02:00
res.json(projects);
});
} else {
// could just move this to client side
res.json([]);
2016-07-01 17:30:40 +02:00
}
}
export function getProjectsForUser(req, res) {
if (req.params.username) {
User.findOne({ username: req.params.username }, (err, user) => {
if (!user) {
res.status(404).json({ message: 'User with that username does not exist.' });
return;
}
Project.find({ user: user._id })
2017-01-24 23:20:40 +01:00
.sort('-createdAt')
.select('name files id createdAt updatedAt')
.exec((innerErr, projects) => res.json(projects));
});
} else {
// could just move this to client side
res.json([]);
}
}
export function projectExists(projectId, callback) {
Project.findById(projectId, (err, project) => (
project ? callback(true) : callback(false)
));
}
export function projectForUserExists(username, projectId, callback) {
User.findOne({ username }, (err, user) => {
if (!user) {
callback(false);
return;
}
2018-02-20 20:22:33 +01:00
Project.findOne({ _id: projectId, user: user._id }, (innerErr, project) => {
2018-02-20 20:16:58 +01:00
if (project) {
callback(true);
return;
}
2018-02-20 20:22:33 +01:00
Project.findOne({ slug: projectId, user: user._id }, (slugError, projectBySlug) => {
2018-02-20 20:16:58 +01:00
if (projectBySlug) {
callback(true);
return;
}
callback(false);
});
});
});
}
function bundleExternalLibs(project, zip, callback) {
const indexHtml = project.files.find(file => file.name === 'index.html');
let numScriptsResolved = 0;
let numScriptTags = 0;
function resolveScriptTagSrc(scriptTag, document) {
const path = scriptTag.src.split('/');
const filename = path[path.length - 1];
2018-05-05 02:59:43 +02:00
const { src } = scriptTag;
if (!isUrl(src)) {
numScriptsResolved += 1;
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);
}
});
}
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 02:59:43 +02:00
const { files } = project;
let numCompletedFiles = 0;
2017-01-24 23:20:40 +01:00
zip.on('error', (err) => {
res.status(500).send({ error: err.message });
});
2019-03-02 10:35:40 +01:00
const currentTime = format(new Date(), 'YYYY_MM_DD_HH_mm_ss');
project.slug = slugify(project.name, '_');
res.attachment(`${generateFileSystemSafeName(project.slug)}_${currentTime}.zip`);
zip.pipe(res);
function addFileToZip(file, path) {
if (file.fileType === 'folder') {
const newPath = file.name === 'root' ? path : `${path}${file.name}/`;
file.children.forEach((fileId) => {
const childFile = files.find(f => f.id === fileId);
(() => {
addFileToZip(childFile, newPath);
})();
});
} else if (file.url) {
request({ method: 'GET', url: file.url, encoding: null }, (err, response, body) => {
zip.append(body, { name: `${path}${file.name}` });
numCompletedFiles += 1;
if (numCompletedFiles === numFiles) {
zip.finalize();
}
});
} else {
zip.append(file.content, { name: `${path}${file.name}` });
numCompletedFiles += 1;
if (numCompletedFiles === numFiles) {
zip.finalize();
}
}
}
bundleExternalLibs(project, zip, () => {
addFileToZip(rootFile, '/');
});
}
export function downloadProjectAsZip(req, res) {
Project.findById(req.params.project_id, (err, project) => {
// save project to some path
buildZip(project, req, res);
});
}