Notes/src/routes/api/image.ts

116 lines
3.6 KiB
TypeScript
Raw Normal View History

"use strict";
2024-04-06 21:21:22 +03:00
import imageService = require('../../services/image');
import becca = require('../../becca/becca');
2024-02-16 22:51:37 +02:00
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
2024-04-06 21:21:22 +03:00
import fs = require('fs');
import { Request, Response } from 'express';
import BNote = require('../../becca/entities/bnote');
import BRevision = require('../../becca/entities/brevision');
2024-04-07 14:29:08 +03:00
import { AppRequest } from '../route-interface';
2024-04-06 21:21:22 +03:00
function returnImageFromNote(req: Request, res: Response) {
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);
}
2024-04-06 21:21:22 +03:00
function returnImageFromRevision(req: Request, res: Response) {
2023-10-02 15:24:40 +02:00
const image = becca.getRevision(req.params.revisionId);
return returnImageInt(image, res);
}
2024-04-06 21:21:22 +03:00
function returnImageInt(image: BNote | BRevision | null, res: Response) {
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());
}
}
2024-04-06 21:21:22 +03:00
function renderSvgAttachment(image: BNote | BRevision, res: Response, attachmentName: string) {
let svgString = '<svg/>'
const attachment = image.getAttachmentByTitle(attachmentName);
2024-04-06 21:21:22 +03:00
const content = attachment.getContent();
if (attachment && typeof content === "string") {
svgString = content;
} 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);
}
2024-04-06 21:21:22 +03:00
function returnAttachedImage(req: Request, res: Response) {
2023-03-16 18:34:39 +01:00
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());
}
2024-04-07 14:29:08 +03:00
function updateImage(req: AppRequest) {
2019-11-08 22:34:30 +01:00
const {noteId} = req.params;
2024-04-07 14:29:08 +03:00
const {file} = req;
2019-11-08 22:34:30 +01: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,
message: `Unknown image type: ${file.mimetype}`
2019-11-08 22:34:30 +01:00
};
}
2024-04-07 14:29:08 +03:00
if (typeof file.buffer === "string") {
return {
uploaded: false,
message: "Invalid image content."
};
}
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 };
}
2024-04-06 21:21:22 +03:00
export = {
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
};