Notes/src/routes/api/image.js

94 lines
2.6 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');
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);
if (!image) {
return res.sendStatus(404);
}
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);
}
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-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-05-10 13:43:05 +02:00
if (image.type === 'canvas') {
const content = image.getContent();
try {
const data = JSON.parse(content);
2022-05-03 22:06:24 +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);
} catch(err) {
res.status(500).send("there was an error parsing excalidraw to svg");
}
} else {
res.set('Content-Type', image.mime);
res.set("Cache-Control", "no-cache, no-store, must-revalidate");
res.send(image.getContent());
}
}
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;
2021-05-02 11:23:58 +02:00
const note = becca.getNote(noteId);
2018-01-06 21:49:02 -05:00
if (!note) {
return [404, `Note ${noteId} doesn't exist.`];
2018-01-06 21:49:02 -05:00
}
if (!["image/png", "image/jpg", "image/jpeg", "image/gif", "image/webp", "image/svg+xml"].includes(file.mimetype)) {
return [400, "Unknown image type: " + file.mimetype];
}
const {url} = imageService.saveImage(noteId, file.buffer, file.originalname, true, true);
return {
uploaded: true,
url
};
}
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 };
}
module.exports = {
returnImage,
2019-11-08 22:34:30 +01:00
uploadImage,
updateImage
2020-06-20 12:31:38 +02:00
};