p5.js-web-editor/server/controllers/file.controller.js

105 lines
3.1 KiB
JavaScript
Raw Normal View History

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