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

209 lines
6.2 KiB
JavaScript
Raw Normal View History

import archiver from 'archiver';
import request from 'request';
import moment from 'moment';
import Project from '../models/project';
import User from '../models/user';
import { deleteObjectsFromS3 } from './aws.controller';
2016-06-17 18:11:52 +00:00
export function createProject(req, res) {
if (!req.user) {
res.status(403).send({ success: false, message: 'Session does not match owner of project.' });
return;
}
2016-07-15 17:36:33 +00:00
let projectValues = {
user: req.user._id
2016-06-23 22:29:55 +00:00
};
2016-06-20 20:29:08 +00:00
2016-07-15 17:36:33 +00:00
projectValues = Object.assign(projectValues, req.body);
2016-06-23 22:29:55 +00:00
Project.create(projectValues, (err, newProject) => {
if (err) {
res.json({ success: false });
return;
}
2016-07-15 15:54:47 +00:00
Project.populate(newProject,
{ path: 'user', select: 'username' },
2016-07-15 15:54:47 +00:00
(innerErr, newProjectWithUser) => {
if (innerErr) {
res.json({ success: false });
return;
}
res.json(newProjectWithUser);
});
2016-06-23 22:29:55 +00:00
});
2016-06-18 22:33:49 +00:00
}
export function updateProject(req, res) {
Project.findById(req.params.project_id, (findProjectErr, project) => {
if (!req.user || !project.user.equals(req.user._id)) {
res.status(403).send({ success: false, message: 'Session does not match owner of project.' });
return;
}
2017-03-15 15:59:07 +00: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;
// }
Project.findByIdAndUpdate(req.params.project_id,
{
$set: req.body
},
{
new: true
})
.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);
});
}
res.json(updatedProject);
});
});
2016-06-18 22:33:49 +00:00
}
export function getProject(req, res) {
2016-07-15 15:54:47 +00:00
Project.findById(req.params.project_id)
.populate('user', 'username')
.exec((err, project) => {
if (err) {
return res.status(404).send({ message: 'Project with that id does not exist' });
}
return res.json(project);
});
2016-06-23 22:29:55 +00:00
}
2016-07-01 15:30:40 +00:00
function deleteFilesFromS3(files) {
deleteObjectsFromS3(
files.filter((file) => {
if (file.url) {
if (!process.env.S3_DATE || (process.env.S3_DATE && moment(process.env.S3_DATE) < moment(file.createdAt))) {
return true;
}
}
return false;
})
.map((file) => {
const urlComponents = file.url.split('/');
const key = urlComponents.pop();
const userId = urlComponents.pop();
return `${userId}/${key}`;
})
);
}
export function deleteProject(req, res) {
Project.findById(req.params.project_id, (findProjectErr, project) => {
if (!req.user || !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 });
});
});
}
2016-07-01 15:30:40 +00:00
export function getProjects(req, res) {
if (req.user) {
Project.find({ user: req.user._id }) // eslint-disable-line no-underscore-dangle
2016-07-01 15:30:40 +00:00
.sort('-createdAt')
.select('name files id createdAt updatedAt')
2016-07-01 15:30:40 +00:00
.exec((err, projects) => {
res.json(projects);
});
} else {
// could just move this to client side
res.json([]);
2016-07-01 15:30:40 +00: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;
}
2017-01-24 22:20:40 +00:00
Project.find({ user: user._id }) // eslint-disable-line no-underscore-dangle
.sort('-createdAt')
.select('name files id createdAt updatedAt')
.exec((innerErr, projects) => res.json(projects));
});
} else {
// could just move this to client side
res.json([]);
}
}
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;
const files = project.files;
let numCompletedFiles = 0;
2017-01-24 22:20:40 +00:00
zip.on('error', (err) => {
res.status(500).send({ error: err.message });
});
res.attachment(`${project.name}.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();
}
}
}
addFileToZip(rootFile, '/');
}
export function downloadProjectAsZip(req, res) {
Project.findById(req.params.project_id, (err, project) => {
// save project to some path
buildZip(project, req, res);
});
}