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
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function returnImage(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
|
|
|
|
|
|
|
if (!image) {
|
2018-03-30 19:31:22 -04:00
|
|
|
return res.sendStatus(404);
|
2018-01-05 23:54:02 -05:00
|
|
|
}
|
2022-05-10 13:43:05 +02:00
|
|
|
else if (!["image", "canvas"].includes(image.type)){
|
2018-11-08 10:30:35 +01:00
|
|
|
return res.sendStatus(400);
|
|
|
|
}
|
2019-04-15 21:08:31 +02:00
|
|
|
else if (image.isDeleted || image.data === null) {
|
2018-01-07 14:07:59 -05:00
|
|
|
res.set('Content-Type', 'image/png');
|
|
|
|
return res.send(fs.readFileSync(RESOURCE_DIR + '/db/image-deleted.png'));
|
|
|
|
}
|
2020-03-25 18:21:55 +01:00
|
|
|
|
2022-04-11 11:56:36 +02:00
|
|
|
/**
|
2022-05-10 13:43:05 +02:00
|
|
|
* special "image" type. the canvas is actually type application/json
|
2022-05-03 22:06:24 +02:00
|
|
|
* to avoid bitrot and enable usage as referenced image the svg is included.
|
2022-04-11 11:56:36 +02:00
|
|
|
*/
|
2022-05-10 13:43:05 +02:00
|
|
|
if (image.type === 'canvas') {
|
2022-04-11 11:56:36 +02:00
|
|
|
const content = image.getContent();
|
|
|
|
try {
|
missing path2d support for freedawings, remove node-side rendering, allow async getContent()
* ## Excalidraw and SVG
* 2022-04-16 - @thfrei
*
* Known issues:
* - excalidraw-to-svg (node.js) does not render any hand drawn (freedraw) paths. There is an issue with
* Path2D object not present in node-canvas library used by jsdom. (See Trilium PR for samples and other issues
* in respective library. Link will be added later). Related links:
* - https://github.com/Automattic/node-canvas/pull/2013
* - https://github.com/google/canvas-5-polyfill
* - https://github.com/Automattic/node-canvas/issues/1116
* - https://www.npmjs.com/package/path2d-polyfill
* - excalidraw-to-svg (node.js) takes quite some time to load an image (1-2s)
* - excalidraw-utils (browser) does render freedraw, however NOT freedraw with background
*
* Due to this issues, we opt to use **only excalidraw in the frontend**. Upon saving, we will also get the SVG
* output from the live excalidraw instance. We will save this **SVG side by side the native excalidraw format
* in the trilium note**.
*
* Pro: we will combat bit-rot. Showing the SVG will be very fast, since it is already rendered.
* Con: The note will get bigger (maybe +30%?), we will generate more bandwith.
* (However, using trilium desktop instance, does not care too much about bandwidth. Size increase is probably
* acceptable, as a trade off.)
2022-04-19 00:21:20 +02:00
|
|
|
const data = JSON.parse(content);
|
2022-05-03 22:06:24 +02:00
|
|
|
|
missing path2d support for freedawings, remove node-side rendering, allow async getContent()
* ## Excalidraw and SVG
* 2022-04-16 - @thfrei
*
* Known issues:
* - excalidraw-to-svg (node.js) does not render any hand drawn (freedraw) paths. There is an issue with
* Path2D object not present in node-canvas library used by jsdom. (See Trilium PR for samples and other issues
* in respective library. Link will be added later). Related links:
* - https://github.com/Automattic/node-canvas/pull/2013
* - https://github.com/google/canvas-5-polyfill
* - https://github.com/Automattic/node-canvas/issues/1116
* - https://www.npmjs.com/package/path2d-polyfill
* - excalidraw-to-svg (node.js) takes quite some time to load an image (1-2s)
* - excalidraw-utils (browser) does render freedraw, however NOT freedraw with background
*
* Due to this issues, we opt to use **only excalidraw in the frontend**. Upon saving, we will also get the SVG
* output from the live excalidraw instance. We will save this **SVG side by side the native excalidraw format
* in the trilium note**.
*
* Pro: we will combat bit-rot. Showing the SVG will be very fast, since it is already rendered.
* Con: The note will get bigger (maybe +30%?), we will generate more bandwith.
* (However, using trilium desktop instance, does not care too much about bandwidth. Size increase is probably
* acceptable, as a trade off.)
2022-04-19 00:21:20 +02:00
|
|
|
const svg = data.svg || '<svg />'
|
|
|
|
res.set('Content-Type', "image/svg+xml");
|
|
|
|
res.set("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
|
|
res.send(svg);
|
2022-04-11 11:56:36 +02:00
|
|
|
} catch(err) {
|
2022-04-11 21:38:22 +02:00
|
|
|
res.status(500).send("there was an error parsing excalidraw to 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
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function uploadImage(req) {
|
2019-11-08 22:34:30 +01:00
|
|
|
const {noteId} = req.query;
|
|
|
|
const {file} = req;
|
2018-01-05 23:54:02 -05:00
|
|
|
|
2021-05-02 11:23:58 +02:00
|
|
|
const note = becca.getNote(noteId);
|
2018-01-06 21:49:02 -05:00
|
|
|
|
|
|
|
if (!note) {
|
2018-03-30 19:31:22 -04:00
|
|
|
return [404, `Note ${noteId} doesn't exist.`];
|
2018-01-06 21:49:02 -05:00
|
|
|
}
|
2018-01-05 23:54:02 -05:00
|
|
|
|
2021-08-31 22:02:38 +02:00
|
|
|
if (!["image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
|
2018-03-30 19:31:22 -04:00
|
|
|
return [400, "Unknown image type: " + file.mimetype];
|
2018-01-05 23:54:02 -05:00
|
|
|
}
|
|
|
|
|
2021-11-04 21:48:46 +01:00
|
|
|
const {url} = imageService.saveImage(noteId, file.buffer, file.originalname, true, true);
|
2018-01-05 23:54:02 -05:00
|
|
|
|
2018-03-30 19:31:22 -04:00
|
|
|
return {
|
2018-01-05 23:54:02 -05:00
|
|
|
uploaded: true,
|
2018-11-05 12:52:50 +01:00
|
|
|
url
|
2018-03-30 19:31:22 -04:00
|
|
|
};
|
2018-03-30 17:07:41 -04:00
|
|
|
}
|
2018-01-05 23:54:02 -05:00
|
|
|
|
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;
|
|
|
|
|
2021-05-02 11:23:58 +02:00
|
|
|
const note = becca.getNote(noteId);
|
2019-11-08 22:34:30 +01:00
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
return [404, `Note ${noteId} doesn't exist.`];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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 = {
|
|
|
|
returnImage,
|
2019-11-08 22:34:30 +01:00
|
|
|
uploadImage,
|
|
|
|
updateImage
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|