2018-01-05 23:54:02 -05:00
|
|
|
"use strict";
|
|
|
|
|
2018-11-05 12:52:50 +01:00
|
|
|
const imageService = require('../../services/image');
|
2021-06-29 22:15:57 +02:00
|
|
|
const becca = require('../../becca/becca');
|
2018-01-07 14:07:59 -05:00
|
|
|
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
|
|
|
|
const fs = require('fs');
|
2018-01-05 23:54:02 -05:00
|
|
|
|
2023-10-02 15:24:40 +02:00
|
|
|
function returnImageFromNote(req, res) {
|
2021-05-02 11:23:58 +02:00
|
|
|
const image = becca.getNote(req.params.noteId);
|
2018-01-05 23:54:02 -05:00
|
|
|
|
2023-10-02 15:24:40 +02:00
|
|
|
return returnImageInt(image, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function returnImageFromRevision(req, res) {
|
|
|
|
const image = becca.getRevision(req.params.revisionId);
|
|
|
|
|
|
|
|
return returnImageInt(image, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {BNote|BRevision} image
|
|
|
|
* @param res
|
|
|
|
*/
|
|
|
|
function returnImageInt(image, res) {
|
2018-01-05 23:54:02 -05:00
|
|
|
if (!image) {
|
2023-03-16 18:34:39 +01:00
|
|
|
res.set('Content-Type', 'image/png');
|
|
|
|
return res.send(fs.readFileSync(`${RESOURCE_DIR}/db/image-deleted.png`));
|
2023-10-21 00:23:16 +02:00
|
|
|
} else if (!["image", "canvas", "mermaid"].includes(image.type)) {
|
2018-11-08 10:30:35 +01:00
|
|
|
return res.sendStatus(400);
|
|
|
|
}
|
2020-03-25 18:21:55 +01:00
|
|
|
|
2022-05-10 13:43:05 +02:00
|
|
|
if (image.type === 'canvas') {
|
2023-10-21 00:23:16 +02:00
|
|
|
renderSvgAttachment(image, res, 'canvas-export.svg');
|
|
|
|
} else if (image.type === 'mermaid') {
|
|
|
|
renderSvgAttachment(image, res, 'mermaid-export.svg');
|
2022-04-11 11:56:36 +02:00
|
|
|
} else {
|
|
|
|
res.set('Content-Type', image.mime);
|
|
|
|
res.set("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
|
|
res.send(image.getContent());
|
|
|
|
}
|
2018-03-30 17:07:41 -04:00
|
|
|
}
|
2018-01-05 23:54:02 -05:00
|
|
|
|
2023-10-21 00:23:16 +02:00
|
|
|
function renderSvgAttachment(image, res, attachmentName) {
|
|
|
|
let svgString = '<svg/>'
|
|
|
|
const attachment = image.getAttachmentByTitle(attachmentName);
|
|
|
|
|
|
|
|
if (attachment) {
|
|
|
|
svgString = attachment.getContent();
|
|
|
|
} else {
|
|
|
|
// backwards compatibility, before attachments, the SVG was stored in the main note content as a separate key
|
|
|
|
const contentSvg = image.getJsonContentSafely()?.svg;
|
|
|
|
|
|
|
|
if (contentSvg) {
|
|
|
|
svgString = contentSvg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const svg = svgString
|
|
|
|
res.set('Content-Type', "image/svg+xml");
|
|
|
|
res.set("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
|
|
res.send(svg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-16 18:34:39 +01:00
|
|
|
function returnAttachedImage(req, res) {
|
|
|
|
const attachment = becca.getAttachment(req.params.attachmentId);
|
|
|
|
|
2023-04-17 22:40:53 +02:00
|
|
|
if (!attachment) {
|
2023-03-16 18:34:39 +01:00
|
|
|
res.set('Content-Type', 'image/png');
|
|
|
|
return res.send(fs.readFileSync(`${RESOURCE_DIR}/db/image-deleted.png`));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!["image"].includes(attachment.role)) {
|
2023-09-14 00:18:56 +02:00
|
|
|
return res.setHeader("Content-Type", "text/plain")
|
|
|
|
.status(400)
|
|
|
|
.send(`Attachment '${attachment.attachmentId}' has role '${attachment.role}', but 'image' was expected.`);
|
2023-03-16 18:34:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res.set('Content-Type', attachment.mime);
|
|
|
|
res.set("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
|
|
res.send(attachment.getContent());
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function updateImage(req) {
|
2019-11-08 22:34:30 +01:00
|
|
|
const {noteId} = req.params;
|
|
|
|
const {file} = req;
|
|
|
|
|
2023-05-08 00:02:08 +02:00
|
|
|
const note = becca.getNoteOrThrow(noteId);
|
2019-11-08 22:34:30 +01:00
|
|
|
|
2020-06-28 23:10:45 +02:00
|
|
|
if (!["image/png", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
|
2019-11-08 22:34:30 +01:00
|
|
|
return {
|
|
|
|
uploaded: false,
|
2022-12-21 15:19:05 +01:00
|
|
|
message: `Unknown image type: ${file.mimetype}`
|
2019-11-08 22:34:30 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
imageService.updateImage(noteId, file.buffer, file.originalname);
|
2019-11-08 22:34:30 +01:00
|
|
|
|
|
|
|
return { uploaded: true };
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:07:41 -04:00
|
|
|
module.exports = {
|
2023-10-02 15:24:40 +02:00
|
|
|
returnImageFromNote,
|
|
|
|
returnImageFromRevision,
|
2023-03-16 18:34:39 +01:00
|
|
|
returnAttachedImage,
|
2019-11-08 22:34:30 +01:00
|
|
|
updateImage
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|