2016-08-24 21:29:44 +00:00
|
|
|
import Project from '../models/project';
|
2016-11-08 21:50:21 +00:00
|
|
|
import { resolvePathToFile } from '../utils/filePath';
|
2016-07-13 22:53:56 +00:00
|
|
|
|
|
|
|
// Bug -> timestamps don't get created, but it seems like this will
|
|
|
|
// be fixed in mongoose soon
|
|
|
|
// https://github.com/Automattic/mongoose/issues/4049
|
|
|
|
export function createFile(req, res) {
|
|
|
|
Project.findByIdAndUpdate(req.params.project_id,
|
|
|
|
{
|
|
|
|
$push: {
|
2017-01-11 20:50:36 +00:00
|
|
|
files: req.body
|
2016-07-13 22:53:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
new: true
|
|
|
|
}, (err, updatedProject) => {
|
2016-08-24 21:29:44 +00:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return res.json({ success: false });
|
|
|
|
}
|
2016-08-23 23:40:47 +00:00
|
|
|
const newFile = updatedProject.files[updatedProject.files.length - 1];
|
2016-08-24 21:29:44 +00:00
|
|
|
updatedProject.files.id(req.body.parentId).children.push(newFile.id);
|
|
|
|
updatedProject.save(innerErr => {
|
|
|
|
if (innerErr) {
|
|
|
|
console.log(innerErr);
|
|
|
|
return res.json({ success: false });
|
|
|
|
}
|
|
|
|
return res.json(updatedProject.files[updatedProject.files.length - 1]);
|
|
|
|
});
|
2016-07-13 22:53:56 +00:00
|
|
|
});
|
2016-08-24 21:59:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 23:02:38 +00:00
|
|
|
function getAllDescendantIds(files, nodeId) {
|
|
|
|
return files.find(file => file.id === nodeId).children
|
|
|
|
.reduce((acc, childId) => (
|
|
|
|
[...acc, childId, ...getAllDescendantIds(files, childId)]
|
|
|
|
), []);
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteMany(files, ids) {
|
|
|
|
ids.forEach(id => {
|
|
|
|
files.id(id).remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteChild(files, parentId, id) {
|
|
|
|
files = files.map((file) => {
|
|
|
|
if (file.id === parentId) {
|
|
|
|
file.children = file.children.filter(child => child !== id);
|
2016-11-17 16:15:35 +00:00
|
|
|
return file;
|
2016-09-02 23:02:38 +00:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:59:15 +00:00
|
|
|
export function deleteFile(req, res) {
|
|
|
|
Project.findById(req.params.project_id, (err, project) => {
|
2016-09-02 23:02:38 +00:00
|
|
|
const idsToDelete = getAllDescendantIds(project.files, req.params.file_id);
|
|
|
|
deleteMany(project.files, [req.params.file_id, ...idsToDelete]);
|
|
|
|
deleteChild(project.files, req.query.parentId, req.params.file_id);
|
|
|
|
// project.files.id(req.params.file_id).remove();
|
|
|
|
// const childrenArray = project.files.id(req.query.parentId).children;
|
|
|
|
// project.files.id(req.query.parentId).children = childrenArray.filter(id => id !== req.params.file_id);
|
2016-08-24 21:59:15 +00:00
|
|
|
project.save(innerErr => {
|
|
|
|
res.json(project.files);
|
2016-11-17 16:15:35 +00:00
|
|
|
});
|
2016-08-24 21:59:15 +00:00
|
|
|
});
|
2016-11-08 19:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getFileContent(req, res) {
|
|
|
|
Project.findById(req.params.project_id, (err, project) => {
|
|
|
|
if (err) {
|
2016-11-17 16:15:35 +00:00
|
|
|
return res.status(404).send({ success: false, message: 'Project with that id does not exist.' });
|
2016-11-08 19:42:23 +00:00
|
|
|
}
|
2016-11-08 21:50:21 +00:00
|
|
|
const filePath = req.params[0];
|
|
|
|
const resolvedFile = resolvePathToFile(filePath, project.files);
|
|
|
|
if (!resolvedFile) {
|
2016-11-17 16:15:35 +00:00
|
|
|
return res.status(404).send({ success: false, message: 'File with that name and path does not exist.' });
|
2016-11-08 19:42:23 +00:00
|
|
|
}
|
2016-11-08 21:50:21 +00:00
|
|
|
res.send(resolvedFile.content);
|
2016-11-08 19:42:23 +00:00
|
|
|
});
|
|
|
|
}
|