2016-06-24 00:29:55 +02:00
|
|
|
import Project from '../models/project';
|
2016-08-17 21:53:25 +02:00
|
|
|
import User from '../models/user';
|
2016-11-02 19:08:53 +01:00
|
|
|
import archiver from 'archiver';
|
|
|
|
import request from 'request';
|
|
|
|
|
2016-06-17 20:11:52 +02:00
|
|
|
|
|
|
|
export function createProject(req, res) {
|
2016-07-15 19:36:33 +02:00
|
|
|
let projectValues = {
|
2016-07-08 20:57:22 +02:00
|
|
|
user: req.user ? req.user._id : undefined // eslint-disable-line no-underscore-dangle
|
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) { return res.json({ success: false }); }
|
2016-07-15 17:54:47 +02:00
|
|
|
Project.populate(newProject,
|
2016-11-17 17:15:35 +01:00
|
|
|
{ path: 'user', select: 'username' },
|
2016-07-15 17:54:47 +02:00
|
|
|
(innerErr, newProjectWithUser) => {
|
|
|
|
if (innerErr) { return res.json({ success: false }); }
|
|
|
|
return res.json(newProjectWithUser);
|
2016-11-17 17:15:35 +01:00
|
|
|
});
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
2016-06-19 00:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function updateProject(req, res) {
|
2016-06-29 01:35:56 +02:00
|
|
|
Project.findByIdAndUpdate(req.params.project_id,
|
2016-06-24 00:29:55 +02:00
|
|
|
{
|
|
|
|
$set: req.body
|
2016-07-15 17:54:47 +02:00
|
|
|
})
|
|
|
|
.populate('user', 'username')
|
|
|
|
.exec((err, updatedProject) => {
|
2016-08-24 23:29:44 +02:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return res.json({ success: false });
|
|
|
|
}
|
|
|
|
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) => {
|
|
|
|
if (innerErr) {
|
|
|
|
console.log(innerErr);
|
|
|
|
return res.json({ success: false });
|
|
|
|
}
|
|
|
|
return res.json(updatedProject);
|
|
|
|
});
|
|
|
|
}
|
2016-07-07 19:50:52 +02:00
|
|
|
return res.json(updatedProject);
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
2016-06-19 00:33:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getProject(req, res) {
|
2016-07-15 17:54:47 +02: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-24 00:29:55 +02:00
|
|
|
}
|
2016-07-01 17:30:40 +02:00
|
|
|
|
2016-10-12 20:24:53 +02:00
|
|
|
export function deleteProject(req, res) {
|
2016-11-17 17:15:35 +01:00
|
|
|
Project.remove({ _id: req.params.project_id }, (err) => {
|
2016-10-12 20:24:53 +02:00
|
|
|
if (err) {
|
|
|
|
return res.status(404).send({ message: 'Project with that id does not exist' });
|
|
|
|
}
|
|
|
|
return res.json({ success: true });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-01 17:30:40 +02:00
|
|
|
export function getProjects(req, res) {
|
|
|
|
if (req.user) {
|
2016-11-17 17:15:35 +01:00
|
|
|
Project.find({ user: req.user._id }) // eslint-disable-line no-underscore-dangle
|
2016-07-01 17:30:40 +02:00
|
|
|
.sort('-createdAt')
|
2016-08-10 00:45:59 +02:00
|
|
|
.select('name files id createdAt updatedAt')
|
2016-07-01 17:30:40 +02:00
|
|
|
.exec((err, projects) => {
|
|
|
|
res.json(projects);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// could just move this to client side
|
|
|
|
return res.json([]);
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 21:53:25 +02:00
|
|
|
|
|
|
|
export function getProjectsForUser(req, res) {
|
|
|
|
if (req.params.username) {
|
|
|
|
User.findOne({ username: req.params.username }, (err, user) => {
|
2017-01-06 19:08:03 +01:00
|
|
|
if (!user) {
|
|
|
|
return res.status(404).json({ message: 'User with that username does not exist.' });
|
|
|
|
} else {
|
|
|
|
Project.find({ user: user._id }) // eslint-disable-line no-underscore-dangle
|
|
|
|
.sort('-createdAt')
|
|
|
|
.select('name files id createdAt updatedAt')
|
|
|
|
.exec((err, projects) => res.json(projects));
|
|
|
|
}
|
|
|
|
return null;
|
2016-08-17 21:53:25 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// could just move this to client side
|
|
|
|
return res.json([]);
|
|
|
|
}
|
2017-01-06 19:08:03 +01:00
|
|
|
return null;
|
2016-08-17 21:53:25 +02:00
|
|
|
}
|
2016-11-02 19:08:53 +01: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;
|
|
|
|
const projectName = project.name;
|
|
|
|
let numCompletedFiles = 0;
|
|
|
|
|
2016-11-17 17:15:35 +01:00
|
|
|
zip.on('error', function (err) {
|
|
|
|
res.status(500).send({ error: err.message });
|
2016-11-02 19:08:53 +01: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}/`;
|
|
|
|
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) => {
|
2016-11-17 17:15:35 +01:00
|
|
|
// save project to some path
|
2016-11-02 19:08:53 +01:00
|
|
|
buildZip(project, req, res);
|
|
|
|
});
|
|
|
|
}
|