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

129 lines
4 KiB
JavaScript
Raw Permalink Normal View History

import each from 'async/each';
import mime from 'mime-types';
2019-03-02 09:35:40 +00:00
import isBefore from 'date-fns/is_before';
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-09-19 20:09:12 +00:00
Project.findOneAndUpdate(
{
_id: req.params.project_id,
user: req.user._id
},
{
$push: {
2017-01-11 20:50:36 +00:00
files: req.body
}
},
{
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.' });
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);
updatedProject.save((innerErr, savedProject) => {
2016-08-24 21:29:44 +00:00
if (innerErr) {
console.log(innerErr);
res.json({ success: false });
return;
2016-08-24 21:29:44 +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
}
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)]
), []);
}
function deleteMany(files, ids) {
const objectKeys = [];
each(ids, (id, cb) => {
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)))) {
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 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;
}
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.save((innerErr, savedProject) => {
res.json({ project: savedProject });
});
2016-08-24 21:59:15 +00:00
});
}
export function getFileContent(req, res) {
Project.findById(req.params.project_id, (err, project) => {
if (err || project === null) {
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;
}
const contentType = mime.lookup(resolvedFile.name) || 'application/octet-stream';
res.set('Content-Type', contentType);
res.send(resolvedFile.content);
});
}