trilium/src/services/image.js

108 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-02-11 13:18:59 +08:00
"use strict";
const repository = require('./repository');
const log = require('./log');
const protectedSessionService = require('./protected_session');
2018-11-08 18:08:16 +08:00
const noteService = require('./notes');
2018-02-11 13:18:59 +08: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-02-24 20:10:47 +08:00
async function saveImage(buffer, originalName, parentNoteId, shrinkImageSwitch) {
2019-03-04 03:41:03 +08:00
const finalImageBuffer = shrinkImageSwitch ? await shrinkImage(buffer, originalName) : buffer;
2018-02-11 13:18:59 +08:00
2019-02-24 19:24:28 +08:00
const imageFormat = imageType(finalImageBuffer);
2018-02-11 13:18:59 +08:00
const parentNote = await repository.getNote(parentNoteId);
const fileNameWithoutExtension = originalName.replace(/\.[^/.]+$/, "");
const fileName = sanitizeFilename(fileNameWithoutExtension + "." + imageFormat.ext);
2018-02-11 13:18:59 +08:00
2019-02-24 19:24:28 +08:00
const {note} = await noteService.createNote(parentNoteId, fileName, finalImageBuffer, {
2018-11-08 18:08:16 +08:00
target: 'into',
type: 'image',
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable(),
mime: 'image/' + imageFormat.ext.toLowerCase(),
attributes: [
{ type: 'label', name: 'originalFileName', value: originalName },
2019-02-24 19:24:28 +08:00
{ type: 'label', name: 'fileSize', value: finalImageBuffer.byteLength }
]
2018-11-08 18:08:16 +08:00
});
2018-02-11 13:18:59 +08:00
return {
fileName,
2019-02-26 04:22:57 +08:00
note,
2018-11-08 18:08:16 +08:00
noteId: note.noteId,
2019-02-23 06:03:20 +08:00
url: `api/images/${note.noteId}/${fileName}`
};
2018-02-11 13:18:59 +08:00
}
2019-03-04 03:41:03 +08:00
async function shrinkImage(buffer, originalName) {
2019-02-24 19:24:28 +08:00
const resizedImage = await resize(buffer);
let finalImageBuffer;
try {
finalImageBuffer = await optimize(resizedImage);
} catch (e) {
2019-03-04 03:41:03 +08:00
log.error("Failed to optimize image '" + originalName + "\nStack: " + e.stack);
2019-02-24 19:24:28 +08:00
finalImageBuffer = resizedImage;
}
// 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 19:24:28 +08:00
return finalImageBuffer;
}
2018-02-11 13:18:59 +08:00
const MAX_SIZE = 1000;
const MAX_BYTE_SIZE = 200000; // images should have under 100 KBs
async function resize(buffer) {
const image = await jimp.read(buffer);
if (image.bitmap.width > image.bitmap.height && image.bitmap.width > MAX_SIZE) {
image.resize(MAX_SIZE, jimp.AUTO);
}
else if (image.bitmap.height > MAX_SIZE) {
image.resize(jimp.AUTO, MAX_SIZE);
}
else if (buffer.byteLength <= MAX_BYTE_SIZE) {
return buffer;
}
// 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-26 04:22:57 +08:00
return image.getBufferAsync(jimp.MIME_JPEG);
2018-02-11 13:18:59 +08:00
}
async function optimize(buffer) {
return await imagemin.buffer(buffer, {
plugins: [
imageminMozJpeg({
quality: 50
}),
imageminPngQuant({
quality: "0-70"
}),
imageminGifLossy({
lossy: 80,
optimize: '3' // needs to be string
})
]
});
}
module.exports = {
saveImage
};