2018-01-05 23:54:02 -05:00
|
|
|
"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';
|
2018-01-05 23:54:02 -05:00
|
|
|
|
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);
|
2018-01-05 23:54:02 -05:00
|
|
|
|
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) {
|
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
|
|
|
|
2024-04-06 21:21:22 +03:00
|
|
|
function renderSvgAttachment(image: BNote | BRevision, res: Response, attachmentName: string) {
|
2023-10-21 00:23:16 +02:00
|
|
|
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;
|
2023-10-21 00:23:16 +02:00
|
|
|
} 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)) {
|
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());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|