2017-03-30 20:52:36 +00:00
|
|
|
import each from 'async/each';
|
2019-03-02 09:35:40 +00:00
|
|
|
import isBefore from 'date-fns/is_before';
|
2017-04-06 18:34:14 +00:00
|
|
|
import Project from '../models/project';
|
2016-11-08 21:50:21 +00:00
|
|
|
import { resolvePathToFile } from '../utils/filePath';
|
2017-04-13 18:17:30 +00:00
|
|
|
import { deleteObjectsFromS3, getObjectKey } from './aws.controller';
|
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) {
|
2018-09-19 20:09:12 +00:00
|
|
|
Project.findOneAndUpdate(
|
|
|
|
{
|
|
|
|
_id: req.params.project_id,
|
|
|
|
user: req.user._id
|
|
|
|
},
|
2016-07-13 22:53:56 +00:00
|
|
|
{
|
|
|
|
$push: {
|
2017-01-11 20:50:36 +00:00
|
|
|
files: req.body
|
2016-07-13 22:53:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
new: true
|
|
|
|
}, (err, updatedProject) => {
|
2018-09-19 20:09:12 +00:00
|
|
|
if (err || !updatedProject) {
|
2016-08-24 21:29:44 +00:00
|
|
|
console.log(err);
|
2018-09-19 20:09:12 +00:00
|
|
|
res.status(403).send({ success: false, message: 'Project does not exist, or user does not match owner.' });
|
2017-02-22 19:29:35 +00:00
|
|
|
return;
|
2016-08-24 21:29:44 +00:00
|
|
|
}
|
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);
|
2019-04-24 17:32:23 +00:00
|
|
|
updatedProject.save((innerErr, savedProject) => {
|
2016-08-24 21:29:44 +00:00
|
|
|
if (innerErr) {
|
|
|
|
console.log(innerErr);
|
2017-02-22 19:29:35 +00:00
|
|
|
res.json({ success: false });
|
|
|
|
return;
|
2016-08-24 21:29:44 +00:00
|
|
|
}
|
2019-04-24 17:32:23 +00:00
|
|
|
savedProject.populate({ path: 'user', select: 'username' }, (_, populatedProject) => {
|
|
|
|
res.json({
|
|
|
|
updatedFile: updatedProject.files[updatedProject.files.length - 1],
|
|
|
|
project: populatedProject
|
|
|
|
});
|
|
|
|
});
|
2016-08-24 21:29:44 +00:00
|
|
|
});
|
2018-05-05 00:22:39 +00:00
|
|
|
}
|
|
|
|
);
|
2016-08-24 21:59:15 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 23:02:38 +00:00
|
|
|
function getAllDescendantIds(files, nodeId) {
|
2018-09-19 20:09:12 +00:00
|
|
|
const parentFile = files.find(file => file.id === nodeId);
|
|
|
|
if (!parentFile) return [];
|
|
|
|
return parentFile.children
|
2018-05-05 00:22:39 +00:00
|
|
|
.reduce((acc, childId) => (
|
|
|
|
[...acc, childId, ...getAllDescendantIds(files, childId)]
|
|
|
|
), []);
|
2016-09-02 23:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteMany(files, ids) {
|
2017-03-30 20:52:36 +00:00
|
|
|
const objectKeys = [];
|
|
|
|
|
|
|
|
each(ids, (id, cb) => {
|
2017-03-30 21:28:28 +00:00
|
|
|
if (files.id(id).url) {
|
2017-06-13 20:47:36 +00:00
|
|
|
if (!process.env.S3_DATE
|
2019-03-02 09:35:40 +00:00
|
|
|
|| (process.env.S3_DATE && isBefore(new Date(process.env.S3_DATE), new Date(files.id(id).createdAt)))) {
|
2017-04-13 18:17:30 +00:00
|
|
|
const objectKey = getObjectKey(files.id(id).url);
|
|
|
|
objectKeys.push(objectKey);
|
2017-03-30 21:28:28 +00:00
|
|
|
}
|
2017-03-30 20:52:36 +00:00
|
|
|
}
|
2016-09-02 23:02:38 +00:00
|
|
|
files.id(id).remove();
|
2017-03-30 20:52:36 +00:00
|
|
|
cb();
|
|
|
|
}, (err) => {
|
|
|
|
deleteObjectsFromS3(objectKeys);
|
2016-09-02 23:02:38 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteChild(files, parentId, id) {
|
2017-02-22 19:29:35 +00:00
|
|
|
return files.map((file) => {
|
2016-09-02 23:02:38 +00:00
|
|
|
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) => {
|
2018-09-19 20:09:12 +00:00
|
|
|
if (!project) {
|
|
|
|
res.status(404).send({ success: false, message: 'Project does not exist.' });
|
|
|
|
}
|
|
|
|
if (!project.user.equals(req.user._id)) {
|
|
|
|
res.status(403).send({ success: false, message: 'Session does not match owner of project.' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure file exists for project
|
|
|
|
const fileToDelete = project.files.find(file => file.id === req.params.file_id);
|
|
|
|
if (!fileToDelete) {
|
|
|
|
res.status(404).send({ success: false, message: 'File does not exist in project.' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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]);
|
2017-02-22 19:29:35 +00:00
|
|
|
project.files = deleteChild(project.files, req.query.parentId, req.params.file_id);
|
|
|
|
project.save((innerErr) => {
|
2016-08-24 21:59:15 +00:00
|
|
|
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) {
|
2017-02-22 19:29:35 +00:00
|
|
|
res.status(404).send({ success: false, message: 'Project with that id does not exist.' });
|
|
|
|
return;
|
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) {
|
2017-02-22 19:29:35 +00:00
|
|
|
res.status(404).send({ success: false, message: 'File with that name and path does not exist.' });
|
|
|
|
return;
|
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
|
|
|
});
|
|
|
|
}
|