save paste images locally WIP

This commit is contained in:
zadam
2020-03-25 11:28:44 +01:00
parent a856463173
commit 8a92786012
10 changed files with 196 additions and 40 deletions

View File

@@ -13,18 +13,19 @@ const jimp = require('jimp');
const imageType = require('image-type');
const sanitizeFilename = require('sanitize-filename');
const noteRevisionService = require('./note_revisions.js');
const isSvg = require('is-svg');
async function processImage(uploadBuffer, originalName, shrinkImageSwitch) {
const origImageFormat = imageType(uploadBuffer);
const origImageFormat = getImageType(uploadBuffer);
if (origImageFormat.ext === "webp") {
if (origImageFormat && ["webp", "svg"].includes(origImageFormat.ext)) {
// JIMP does not support webp at the moment: https://github.com/oliver-moran/jimp/issues/144
shrinkImageSwitch = false;
}
const finalImageBuffer = shrinkImageSwitch ? await shrinkImage(uploadBuffer, originalName) : uploadBuffer;
const imageFormat = imageType(finalImageBuffer);
const imageFormat = getImageType(finalImageBuffer);
return {
buffer: finalImageBuffer,
@@ -32,6 +33,17 @@ async function processImage(uploadBuffer, originalName, shrinkImageSwitch) {
};
}
function getImageType(buffer) {
if (isSvg(buffer)) {
return {
ext: 'svg'
}
}
else {
return imageType(buffer);
}
}
async function updateImage(noteId, uploadBuffer, originalName) {
const {buffer, imageFormat} = await processImage(uploadBuffer, originalName, true);