2018-02-11 00:18:59 -05:00
|
|
|
"use strict";
|
|
|
|
|
2018-11-15 12:13:32 +01:00
|
|
|
const repository = require('./repository');
|
2019-01-09 06:29:49 -08:00
|
|
|
const log = require('./log');
|
2018-11-15 12:13:32 +01:00
|
|
|
const protectedSessionService = require('./protected_session');
|
2018-11-08 11:08:16 +01:00
|
|
|
const noteService = require('./notes');
|
2019-11-03 11:43:04 +01:00
|
|
|
const optionService = require('./options');
|
2018-02-11 00:18:59 -05:00
|
|
|
const imagemin = require('imagemin');
|
|
|
|
const imageminMozJpeg = require('imagemin-mozjpeg');
|
|
|
|
const imageminPngQuant = require('imagemin-pngquant');
|
|
|
|
const imageminGifLossy = require('imagemin-giflossy');
|
|
|
|
const jimp = require('jimp');
|
|
|
|
const imageType = require('image-type');
|
|
|
|
const sanitizeFilename = require('sanitize-filename');
|
2019-11-08 23:09:57 +01:00
|
|
|
const dateUtils = require('./date_utils');
|
2019-11-09 11:58:52 +01:00
|
|
|
const noteRevisionService = require('./note_revisions.js');
|
2019-11-08 23:09:57 +01:00
|
|
|
const NoteRevision = require("../entities/note_revision");
|
2018-02-11 00:18:59 -05:00
|
|
|
|
2019-11-08 22:34:30 +01:00
|
|
|
async function processImage(uploadBuffer, originalName, shrinkImageSwitch) {
|
|
|
|
const origImageFormat = imageType(uploadBuffer);
|
2019-07-10 20:38:27 +02:00
|
|
|
|
|
|
|
if (origImageFormat.ext === "webp") {
|
|
|
|
// JIMP does not support webp at the moment: https://github.com/oliver-moran/jimp/issues/144
|
|
|
|
shrinkImageSwitch = false;
|
|
|
|
}
|
|
|
|
|
2019-11-08 22:34:30 +01:00
|
|
|
const finalImageBuffer = shrinkImageSwitch ? await shrinkImage(uploadBuffer, originalName) : uploadBuffer;
|
2018-02-11 00:18:59 -05:00
|
|
|
|
2019-02-24 12:24:28 +01:00
|
|
|
const imageFormat = imageType(finalImageBuffer);
|
2018-02-11 00:18:59 -05:00
|
|
|
|
2019-11-08 22:34:30 +01:00
|
|
|
return {
|
|
|
|
buffer: finalImageBuffer,
|
|
|
|
imageFormat
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateImage(noteId, uploadBuffer, originalName) {
|
|
|
|
const {buffer, imageFormat} = await processImage(uploadBuffer, originalName, true);
|
|
|
|
|
|
|
|
const note = await repository.getNote(noteId);
|
|
|
|
|
2019-11-09 11:58:52 +01:00
|
|
|
await noteRevisionService.createNoteRevision(note);
|
2019-11-08 23:09:57 +01:00
|
|
|
|
2019-11-08 22:34:30 +01:00
|
|
|
note.mime = 'image/' + imageFormat.ext.toLowerCase();
|
|
|
|
|
|
|
|
await note.setContent(buffer);
|
|
|
|
|
|
|
|
await note.setLabel('originalFileName', originalName);
|
2019-11-08 23:09:57 +01:00
|
|
|
|
2019-11-09 11:58:52 +01:00
|
|
|
await noteRevisionService.protectNoteRevisions(note);
|
2019-11-08 22:34:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function saveImage(parentNoteId, uploadBuffer, originalName, shrinkImageSwitch) {
|
|
|
|
const {buffer, imageFormat} = await processImage(uploadBuffer, originalName, shrinkImageSwitch);
|
2018-11-15 12:13:32 +01:00
|
|
|
|
2019-07-10 23:01:30 +02:00
|
|
|
const fileName = sanitizeFilename(originalName);
|
2018-02-11 00:18:59 -05:00
|
|
|
|
2019-11-08 22:34:30 +01:00
|
|
|
const parentNote = await repository.getNote(parentNoteId);
|
|
|
|
|
|
|
|
const {note} = await noteService.createNote(parentNoteId, fileName, buffer, {
|
2018-11-08 11:08:16 +01:00
|
|
|
target: 'into',
|
|
|
|
type: 'image',
|
2018-11-15 12:13:32 +01:00
|
|
|
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
|
|
|
|
mime: 'image/' + imageFormat.ext.toLowerCase(),
|
2019-11-09 09:36:08 +01:00
|
|
|
attributes: [{ type: 'label', name: 'originalFileName', value: originalName }]
|
2018-11-08 11:08:16 +01:00
|
|
|
});
|
2018-02-11 00:18:59 -05:00
|
|
|
|
2018-11-05 12:52:50 +01:00
|
|
|
return {
|
|
|
|
fileName,
|
2019-02-25 21:22:57 +01:00
|
|
|
note,
|
2018-11-08 11:08:16 +01:00
|
|
|
noteId: note.noteId,
|
2019-11-08 23:09:57 +01:00
|
|
|
url: `api/images/${note.noteId}/${fileName}`
|
2018-11-05 12:52:50 +01:00
|
|
|
};
|
2018-02-11 00:18:59 -05:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:41:03 +01:00
|
|
|
async function shrinkImage(buffer, originalName) {
|
2019-02-24 12:24:28 +01:00
|
|
|
const resizedImage = await resize(buffer);
|
|
|
|
let finalImageBuffer;
|
|
|
|
|
|
|
|
try {
|
|
|
|
finalImageBuffer = await optimize(resizedImage);
|
|
|
|
} catch (e) {
|
2019-07-10 20:38:27 +02:00
|
|
|
log.error("Failed to optimize image '" + originalName + "'\nStack: " + e.stack);
|
2019-02-24 12:24:28 +01:00
|
|
|
finalImageBuffer = resizedImage;
|
|
|
|
}
|
2019-04-15 21:12:47 +02:00
|
|
|
|
|
|
|
// if resizing & shrinking did not help with size then save the original
|
|
|
|
// (can happen when e.g. resizing PNG into JPEG)
|
|
|
|
if (finalImageBuffer.byteLength >= buffer.byteLength) {
|
|
|
|
finalImageBuffer = buffer;
|
|
|
|
}
|
|
|
|
|
2019-02-24 12:24:28 +01:00
|
|
|
return finalImageBuffer;
|
|
|
|
}
|
|
|
|
|
2018-02-11 00:18:59 -05:00
|
|
|
async function resize(buffer) {
|
2019-11-03 11:43:04 +01:00
|
|
|
const imageMaxWidthHeight = await optionService.getOptionInt('imageMaxWidthHeight');
|
|
|
|
|
2018-02-11 00:18:59 -05:00
|
|
|
const image = await jimp.read(buffer);
|
|
|
|
|
2019-11-03 11:43:04 +01:00
|
|
|
if (image.bitmap.width > image.bitmap.height && image.bitmap.width > imageMaxWidthHeight) {
|
|
|
|
image.resize(imageMaxWidthHeight, jimp.AUTO);
|
2018-02-11 00:18:59 -05:00
|
|
|
}
|
2019-11-03 11:43:04 +01:00
|
|
|
else if (image.bitmap.height > imageMaxWidthHeight) {
|
|
|
|
image.resize(jimp.AUTO, imageMaxWidthHeight);
|
2018-02-11 00:18:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// we do resizing with max quality which will be trimmed during optimization step next
|
|
|
|
image.quality(100);
|
|
|
|
|
|
|
|
// when converting PNG to JPG we lose alpha channel, this is replaced by white to match Trilium white background
|
|
|
|
image.background(0xFFFFFFFF);
|
|
|
|
|
2019-02-25 21:22:57 +01:00
|
|
|
return image.getBufferAsync(jimp.MIME_JPEG);
|
2018-02-11 00:18:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function optimize(buffer) {
|
|
|
|
return await imagemin.buffer(buffer, {
|
|
|
|
plugins: [
|
|
|
|
imageminMozJpeg({
|
2019-11-03 11:43:04 +01:00
|
|
|
quality: await optionService.getOptionInt('imageJpegQuality')
|
2018-02-11 00:18:59 -05:00
|
|
|
}),
|
|
|
|
imageminPngQuant({
|
2019-07-10 20:38:27 +02:00
|
|
|
quality: [0, 0.7]
|
2018-02-11 00:18:59 -05:00
|
|
|
}),
|
|
|
|
imageminGifLossy({
|
|
|
|
lossy: 80,
|
|
|
|
optimize: '3' // needs to be string
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2019-11-08 22:34:30 +01:00
|
|
|
saveImage,
|
|
|
|
updateImage
|
2018-02-11 00:18:59 -05:00
|
|
|
};
|