2018-02-11 13:18:59 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const utils = require('./utils');
|
2018-04-02 05:38:24 +08:00
|
|
|
const Image = require('../entities/image');
|
|
|
|
const NoteImage = require('../entities/note_image');
|
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');
|
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
async function saveImage(file, noteId) {
|
2018-02-11 13:18:59 +08:00
|
|
|
const resizedImage = await resize(file.buffer);
|
|
|
|
const optimizedImage = await optimize(resizedImage);
|
|
|
|
|
|
|
|
const imageFormat = imageType(optimizedImage);
|
|
|
|
|
|
|
|
const fileNameWithouExtension = file.originalname.replace(/\.[^/.]+$/, "");
|
|
|
|
const fileName = sanitizeFilename(fileNameWithouExtension + "." + imageFormat.ext);
|
|
|
|
|
2018-04-03 10:53:01 +08:00
|
|
|
const image = await new Image({
|
2018-04-02 05:38:24 +08:00
|
|
|
format: imageFormat.ext,
|
|
|
|
name: fileName,
|
|
|
|
checksum: utils.hash(optimizedImage),
|
|
|
|
data: optimizedImage
|
2018-04-03 10:53:01 +08:00
|
|
|
}).save();
|
2018-02-11 13:18:59 +08:00
|
|
|
|
2018-04-03 10:53:01 +08:00
|
|
|
await new NoteImage({
|
2018-04-02 05:38:24 +08:00
|
|
|
noteId: noteId,
|
|
|
|
imageId: image.imageId
|
2018-04-03 10:53:01 +08:00
|
|
|
}).save();
|
2018-02-11 13:18:59 +08:00
|
|
|
|
2018-04-02 05:38:24 +08:00
|
|
|
return {fileName, imageId: image.imageId};
|
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);
|
|
|
|
|
|
|
|
// getBuffer doesn't support promises so this workaround
|
|
|
|
return await new Promise((resolve, reject) => image.getBuffer(jimp.MIME_JPEG, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
resolve(data);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|