Notes/src/routes/api/image.js

108 lines
3.1 KiB
JavaScript
Raw Normal View History

"use strict";
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');
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);
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) {
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`));
} 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') {
renderSvgAttachment(image, res, 'canvas-export.svg');
} else if (image.type === 'mermaid') {
renderSvgAttachment(image, res, 'mermaid-export.svg');
} else {
res.set('Content-Type', image.mime);
res.set("Cache-Control", "no-cache, no-store, must-revalidate");
res.send(image.getContent());
}
}
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)) {
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;
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,
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 };
}
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
};