2017-03-30 22:52:36 +02:00
|
|
|
import each from 'async/each';
|
|
|
|
import moment from 'moment';
|
2017-04-06 20:34:14 +02:00
|
|
|
|
|
|
|
import Project from '../models/project';
|
2016-11-08 22:50:21 +01:00
|
|
|
import { resolvePathToFile } from '../utils/filePath';
|
2017-04-13 20:17:30 +02:00
|
|
|
import { deleteObjectsFromS3, getObjectKey } from './aws.controller';
|
2016-07-14 00:53:56 +02: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 21:50:36 +01:00
|
|
|
files: req.body
|
2016-07-14 00:53:56 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
new: true
|
|
|
|
}, (err, updatedProject) => {
|
2016-08-24 23:29:44 +02:00
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
2017-02-22 20:29:35 +01:00
|
|
|
res.json({ success: false });
|
|
|
|
return;
|
2016-08-24 23:29:44 +02:00
|
|
|
}
|
2016-08-24 01:40:47 +02:00
|
|
|
const newFile = updatedProject.files[updatedProject.files.length - 1];
|
2016-08-24 23:29:44 +02:00
|
|
|
updatedProject.files.id(req.body.parentId).children.push(newFile.id);
|
2017-02-22 20:29:35 +01:00
|
|
|
updatedProject.save((innerErr) => {
|
2016-08-24 23:29:44 +02:00
|
|
|
if (innerErr) {
|
|
|
|
console.log(innerErr);
|
2017-02-22 20:29:35 +01:00
|
|
|
res.json({ success: false });
|
|
|
|
return;
|
2016-08-24 23:29:44 +02:00
|
|
|
}
|
2017-02-22 20:29:35 +01:00
|
|
|
res.json(updatedProject.files[updatedProject.files.length - 1]);
|
2016-08-24 23:29:44 +02:00
|
|
|
});
|
2016-07-14 00:53:56 +02:00
|
|
|
});
|
2016-08-24 23:59:15 +02:00
|
|
|
}
|
|
|
|
|
2016-09-03 01:02:38 +02: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) {
|
2017-03-30 22:52:36 +02:00
|
|
|
const objectKeys = [];
|
|
|
|
|
|
|
|
each(ids, (id, cb) => {
|
2017-03-30 23:28:28 +02:00
|
|
|
if (files.id(id).url) {
|
|
|
|
if (!process.env.S3_DATE || (process.env.S3_DATE && moment(process.env.S3_DATE) < moment(files.id(id).createdAt))) {
|
2017-04-13 20:17:30 +02:00
|
|
|
const objectKey = getObjectKey(files.id(id).url);
|
|
|
|
objectKeys.push(objectKey);
|
2017-03-30 23:28:28 +02:00
|
|
|
}
|
2017-03-30 22:52:36 +02:00
|
|
|
}
|
2016-09-03 01:02:38 +02:00
|
|
|
files.id(id).remove();
|
2017-03-30 22:52:36 +02:00
|
|
|
cb();
|
|
|
|
}, (err) => {
|
|
|
|
deleteObjectsFromS3(objectKeys);
|
2016-09-03 01:02:38 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteChild(files, parentId, id) {
|
2017-02-22 20:29:35 +01:00
|
|
|
return files.map((file) => {
|
2016-09-03 01:02:38 +02:00
|
|
|
if (file.id === parentId) {
|
|
|
|
file.children = file.children.filter(child => child !== id);
|
2016-11-17 17:15:35 +01:00
|
|
|
return file;
|
2016-09-03 01:02:38 +02:00
|
|
|
}
|
|
|
|
return file;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:59:15 +02:00
|
|
|
export function deleteFile(req, res) {
|
|
|
|
Project.findById(req.params.project_id, (err, project) => {
|
2016-09-03 01:02:38 +02:00
|
|
|
const idsToDelete = getAllDescendantIds(project.files, req.params.file_id);
|
|
|
|
deleteMany(project.files, [req.params.file_id, ...idsToDelete]);
|
2017-02-22 20:29:35 +01:00
|
|
|
project.files = deleteChild(project.files, req.query.parentId, req.params.file_id);
|
2016-09-03 01:02:38 +02:00
|
|
|
// 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);
|
2017-02-22 20:29:35 +01:00
|
|
|
project.save((innerErr) => {
|
2016-08-24 23:59:15 +02:00
|
|
|
res.json(project.files);
|
2016-11-17 17:15:35 +01:00
|
|
|
});
|
2016-08-24 23:59:15 +02:00
|
|
|
});
|
2016-11-08 20:42:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getFileContent(req, res) {
|
|
|
|
Project.findById(req.params.project_id, (err, project) => {
|
|
|
|
if (err) {
|
2017-02-22 20:29:35 +01:00
|
|
|
res.status(404).send({ success: false, message: 'Project with that id does not exist.' });
|
|
|
|
return;
|
2016-11-08 20:42:23 +01:00
|
|
|
}
|
2016-11-08 22:50:21 +01:00
|
|
|
const filePath = req.params[0];
|
|
|
|
const resolvedFile = resolvePathToFile(filePath, project.files);
|
|
|
|
if (!resolvedFile) {
|
2017-02-22 20:29:35 +01:00
|
|
|
res.status(404).send({ success: false, message: 'File with that name and path does not exist.' });
|
|
|
|
return;
|
2016-11-08 20:42:23 +01:00
|
|
|
}
|
2016-11-08 22:50:21 +01:00
|
|
|
res.send(resolvedFile.content);
|
2016-11-08 20:42:23 +01:00
|
|
|
});
|
|
|
|
}
|