2016-11-02 18:08:53 +00:00
|
|
|
import archiver from 'archiver';
|
|
|
|
import request from 'request';
|
2017-03-02 19:38:29 +00:00
|
|
|
import moment from 'moment';
|
2017-02-22 19:29:35 +00:00
|
|
|
import Project from '../models/project';
|
|
|
|
import User from '../models/user';
|
2017-04-13 18:17:30 +00:00
|
|
|
import { deleteObjectsFromS3, getObjectKey } from './aws.controller';
|
2016-06-17 18:11:52 +00:00
|
|
|
|
|
|
|
export function createProject(req, res) {
|
2017-01-13 22:32:06 +00:00
|
|
|
if (!req.user) {
|
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-13 22:32:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 17:36:33 +00:00
|
|
|
let projectValues = {
|
2017-01-13 22:32:06 +00:00
|
|
|
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) => {
|
2017-02-22 19:29:35 +00:00
|
|
|
if (err) {
|
|
|
|
res.json({ success: false });
|
|
|
|
return;
|
|
|
|
}
|
2016-07-15 15:54:47 +00:00
|
|
|
Project.populate(newProject,
|
2016-11-17 16:15:35 +00:00
|
|
|
{ path: 'user', select: 'username' },
|
2016-07-15 15:54:47 +00:00
|
|
|
(innerErr, newProjectWithUser) => {
|
2017-02-22 19:29:35 +00:00
|
|
|
if (innerErr) {
|
|
|
|
res.json({ success: false });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.json(newProjectWithUser);
|
2016-11-17 16:15:35 +00:00
|
|
|
});
|
2016-06-23 22:29:55 +00:00
|
|
|
});
|
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) => {
|
2017-01-13 22:17:31 +00:00
|
|
|
if (!req.user || !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
|
|
|
}
|
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;
|
|
|
|
// }
|
2017-01-13 22:17:31 +00:00
|
|
|
Project.findByIdAndUpdate(req.params.project_id,
|
|
|
|
{
|
|
|
|
$set: req.body
|
2017-03-02 19:38:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
new: true
|
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);
|
|
|
|
res.json({ success: false });
|
|
|
|
return;
|
2017-01-13 22:17:31 +00:00
|
|
|
}
|
|
|
|
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);
|
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);
|
2017-02-22 19:29:35 +00:00
|
|
|
res.json({ success: false });
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
2017-02-22 19:29:35 +00:00
|
|
|
res.json(updatedProject);
|
2017-01-13 22:17:31 +00:00
|
|
|
});
|
|
|
|
});
|
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
|
|
|
|
2017-03-30 20:52:36 +00:00
|
|
|
function deleteFilesFromS3(files) {
|
|
|
|
deleteObjectsFromS3(
|
2017-03-30 21:28:28 +00:00
|
|
|
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;
|
|
|
|
})
|
2017-06-13 20:47:36 +00:00
|
|
|
.map(file => getObjectKey(file.url))
|
2017-03-30 20:52:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-12 18:24:53 +00:00
|
|
|
export function deleteProject(req, res) {
|
2017-02-22 19:29:35 +00:00
|
|
|
Project.findById(req.params.project_id, (findProjectErr, project) => {
|
2017-01-13 22:17:31 +00:00
|
|
|
if (!req.user || !project.user.equals(req.user._id)) {
|
2017-02-22 19:29:35 +00:00
|
|
|
res.status(403).json({ success: false, message: 'Session does not match owner of project.' });
|
|
|
|
return;
|
2016-10-12 18:24:53 +00:00
|
|
|
}
|
2017-03-30 20:52:36 +00:00
|
|
|
deleteFilesFromS3(project.files);
|
2017-02-22 19:29:35 +00:00
|
|
|
Project.remove({ _id: req.params.project_id }, (removeProjectError) => {
|
|
|
|
if (removeProjectError) {
|
|
|
|
res.status(404).send({ message: 'Project with that id does not exist' });
|
|
|
|
return;
|
2017-01-13 22:17:31 +00:00
|
|
|
}
|
2017-02-22 19:29:35 +00:00
|
|
|
res.json({ success: true });
|
2017-01-13 22:17:31 +00:00
|
|
|
});
|
2016-10-12 18:24:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:30:40 +00:00
|
|
|
export function getProjects(req, res) {
|
|
|
|
if (req.user) {
|
2016-11-17 16:15:35 +00:00
|
|
|
Project.find({ user: req.user._id }) // eslint-disable-line no-underscore-dangle
|
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) => {
|
|
|
|
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
|
|
|
|
|
|
|
export function getProjectsForUser(req, res) {
|
|
|
|
if (req.params.username) {
|
|
|
|
User.findOne({ username: req.params.username }, (err, user) => {
|
2017-01-06 18:08:03 +00:00
|
|
|
if (!user) {
|
2017-02-22 19:29:35 +00:00
|
|
|
res.status(404).json({ message: 'User with that username does not exist.' });
|
|
|
|
return;
|
2017-01-06 18:08:03 +00:00
|
|
|
}
|
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')
|
2017-02-22 19:29:35 +00:00
|
|
|
.exec((innerErr, projects) => res.json(projects));
|
2016-08-17 19:53:25 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// could just move this to client side
|
2017-02-22 19:29:35 +00:00
|
|
|
res.json([]);
|
2016-08-17 19:53:25 +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;
|
|
|
|
const files = project.files;
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
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}/`;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addFileToZip(rootFile, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|