2024-02-17 11:42:19 +02:00
|
|
|
const becca = require('../becca/becca');
|
2024-02-16 21:38:09 +02:00
|
|
|
const eu = require('./etapi_utils');
|
2024-04-07 14:56:22 +03:00
|
|
|
const mappers = require('./mappers');
|
2024-04-07 14:59:40 +03:00
|
|
|
const v = require('./validators');
|
2024-02-16 21:38:09 +02:00
|
|
|
const utils = require('../services/utils');
|
2023-06-05 09:23:42 +02:00
|
|
|
|
|
|
|
function register(router) {
|
|
|
|
const ALLOWED_PROPERTIES_FOR_CREATE_ATTACHMENT = {
|
2023-07-14 17:01:56 +02:00
|
|
|
'ownerId': [v.notNull, v.isNoteId],
|
2023-06-05 09:23:42 +02:00
|
|
|
'role': [v.notNull, v.isString],
|
|
|
|
'mime': [v.notNull, v.isString],
|
|
|
|
'title': [v.notNull, v.isString],
|
|
|
|
'position': [v.notNull, v.isInteger],
|
|
|
|
'content': [v.isString],
|
|
|
|
};
|
|
|
|
|
2024-04-07 14:56:22 +03:00
|
|
|
eu.route(router, 'post', '/etapi/attachments', (req, res, next) => {
|
2023-06-05 09:23:42 +02:00
|
|
|
const params = {};
|
|
|
|
|
|
|
|
eu.validateAndPatch(params, req.body, ALLOWED_PROPERTIES_FOR_CREATE_ATTACHMENT);
|
|
|
|
|
|
|
|
try {
|
2023-07-14 17:01:56 +02:00
|
|
|
const note = becca.getNoteOrThrow(params.ownerId);
|
2023-06-05 09:23:42 +02:00
|
|
|
const attachment = note.saveAttachment(params);
|
|
|
|
|
|
|
|
res.status(201).json(mappers.mapAttachmentToPojo(attachment));
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
throw new eu.EtapiError(500, eu.GENERIC_CODE, e.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
eu.route(router, 'get', '/etapi/attachments/:attachmentId', (req, res, next) => {
|
|
|
|
const attachment = eu.getAndCheckAttachment(req.params.attachmentId);
|
|
|
|
|
|
|
|
res.json(mappers.mapAttachmentToPojo(attachment));
|
|
|
|
});
|
|
|
|
|
|
|
|
const ALLOWED_PROPERTIES_FOR_PATCH = {
|
|
|
|
'role': [v.notNull, v.isString],
|
|
|
|
'mime': [v.notNull, v.isString],
|
|
|
|
'title': [v.notNull, v.isString],
|
|
|
|
'position': [v.notNull, v.isInteger],
|
|
|
|
};
|
|
|
|
|
2024-04-07 14:56:22 +03:00
|
|
|
eu.route(router, 'patch', '/etapi/attachments/:attachmentId', (req, res, next) => {
|
2023-06-05 09:23:42 +02:00
|
|
|
const attachment = eu.getAndCheckAttachment(req.params.attachmentId);
|
|
|
|
|
|
|
|
if (attachment.isProtected) {
|
|
|
|
throw new eu.EtapiError(400, "ATTACHMENT_IS_PROTECTED", `Attachment '${req.params.attachmentId}' is protected and cannot be modified through ETAPI.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
eu.validateAndPatch(attachment, req.body, ALLOWED_PROPERTIES_FOR_PATCH);
|
|
|
|
attachment.save();
|
|
|
|
|
|
|
|
res.json(mappers.mapAttachmentToPojo(attachment));
|
|
|
|
});
|
|
|
|
|
|
|
|
eu.route(router, 'get', '/etapi/attachments/:attachmentId/content', (req, res, next) => {
|
|
|
|
const attachment = eu.getAndCheckAttachment(req.params.attachmentId);
|
|
|
|
|
|
|
|
if (attachment.isProtected) {
|
|
|
|
throw new eu.EtapiError(400, "ATTACHMENT_IS_PROTECTED", `Attachment '${req.params.attachmentId}' is protected and content cannot be read through ETAPI.`);
|
|
|
|
}
|
|
|
|
|
2023-06-29 23:32:19 +02:00
|
|
|
const filename = utils.formatDownloadTitle(attachment.title, attachment.role, attachment.mime);
|
2023-06-05 09:23:42 +02:00
|
|
|
|
|
|
|
res.setHeader('Content-Disposition', utils.getContentDisposition(filename));
|
|
|
|
|
|
|
|
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
|
|
res.setHeader('Content-Type', attachment.mime);
|
|
|
|
|
|
|
|
res.send(attachment.getContent());
|
|
|
|
});
|
|
|
|
|
|
|
|
eu.route(router, 'put', '/etapi/attachments/:attachmentId/content', (req, res, next) => {
|
|
|
|
const attachment = eu.getAndCheckAttachment(req.params.attachmentId);
|
|
|
|
|
|
|
|
if (attachment.isProtected) {
|
|
|
|
throw new eu.EtapiError(400, "ATTACHMENT_IS_PROTECTED", `Attachment '${req.params.attachmentId}' is protected and cannot be modified through ETAPI.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
attachment.setContent(req.body);
|
|
|
|
|
|
|
|
return res.sendStatus(204);
|
|
|
|
});
|
|
|
|
|
2024-04-07 14:56:22 +03:00
|
|
|
eu.route(router, 'delete', '/etapi/attachments/:attachmentId', (req, res, next) => {
|
2023-06-05 09:23:42 +02:00
|
|
|
const attachment = becca.getAttachment(req.params.attachmentId);
|
|
|
|
|
|
|
|
if (!attachment) {
|
|
|
|
return res.sendStatus(204);
|
|
|
|
}
|
|
|
|
|
|
|
|
attachment.markAsDeleted();
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
register
|
|
|
|
};
|