correct detection of optimized image format

This commit is contained in:
azivner 2018-01-07 08:24:04 -05:00
parent c022fcf196
commit 97c1b3061f
7 changed files with 35 additions and 13 deletions

13
package-lock.json generated
View file

@ -5449,6 +5449,14 @@
"dev": true,
"optional": true
},
"image-type": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/image-type/-/image-type-3.0.0.tgz",
"integrity": "sha1-FQKvMTX5BuEiyHfDHpSve3qRRsU=",
"requires": {
"file-type": "4.4.0"
}
},
"imagemin": {
"version": "5.3.1",
"resolved": "https://registry.npmjs.org/imagemin/-/imagemin-5.3.1.tgz",
@ -8830,7 +8838,6 @@
"version": "1.6.1",
"resolved": "https://registry.npmjs.org/sanitize-filename/-/sanitize-filename-1.6.1.tgz",
"integrity": "sha1-YS2hyWRz+gLczaktzVtKsWSmdyo=",
"dev": true,
"requires": {
"truncate-utf8-bytes": "1.0.2"
}
@ -10662,7 +10669,6 @@
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz",
"integrity": "sha1-QFkjkJWS1W94pYGENLC3hInKXys=",
"dev": true,
"requires": {
"utf8-byte-length": "1.0.4"
}
@ -10930,8 +10936,7 @@
"utf8-byte-length": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz",
"integrity": "sha1-9F8VDExm7uloGGUFq5P8u4rWv2E=",
"dev": true
"integrity": "sha1-9F8VDExm7uloGGUFq5P8u4rWv2E="
},
"util-deprecate": {
"version": "1.0.2",

View file

@ -33,6 +33,7 @@
"fs-extra": "^4.0.2",
"helmet": "^3.9.0",
"html": "^1.0.0",
"image-type": "^3.0.0",
"imagemin": "^5.3.1",
"imagemin-mozjpeg": "^7.0.0",
"imagemin-pngquant": "^5.0.1",
@ -43,6 +44,7 @@
"request": "^2.83.0",
"request-promise": "^4.2.2",
"rimraf": "^2.6.2",
"sanitize-filename": "^1.6.1",
"scrypt": "^6.0.3",
"serve-favicon": "~2.4.5",
"session-file-store": "^1.1.2",

View file

@ -49,6 +49,9 @@ const messaging = (function() {
recentNotes.reload();
}
// we don't detect image changes here since images themselves are immutable and references should be
// updated in note detail as well
changesToPushCountEl.html(message.changesToPushCount);
}
else if (message.type === 'sync-hash-check-failed') {

View file

@ -91,6 +91,7 @@ const server = (function() {
get,
post,
put,
remove
remove,
getHeaders
}
})();

File diff suppressed because one or more lines are too long

View file

@ -11,6 +11,8 @@ const imagemin = require('imagemin');
const imageminMozJpeg = require('imagemin-mozjpeg');
const imageminPngQuant = require('imagemin-pngquant');
const jimp = require('jimp');
const imageType = require('image-type');
const sanitizeFilename = require('sanitize-filename');
router.get('/:imageId/:filename', auth.checkApiAuth, async (req, res, next) => {
const image = await sql.getFirst("SELECT * FROM images WHERE image_id = ?", [req.params.imageId]);
@ -35,7 +37,7 @@ router.post('', auth.checkApiAuth, multer.single('upload'), async (req, res, nex
return req.status(404).send(`Note ${noteId} doesn't exist.`);
}
if (!file.mimetype.startsWith("image/")) {
if (!["image/png", "image/jpeg"].includes(file.mimetype)) {
return req.send("Unknown image type: " + file.mimetype);
}
@ -44,13 +46,19 @@ router.post('', auth.checkApiAuth, multer.single('upload'), async (req, res, nex
const resizedImage = await resize(file.buffer);
const optimizedImage = await optimize(resizedImage);
const imageFormat = imageType(optimizedImage);
console.log(imageFormat);
const fileNameWithouExtension = file.originalname.replace(/\.[^/.]+$/, "");
const fileName = sanitizeFilename(fileNameWithouExtension + "." + imageFormat.ext);
const imageId = utils.newImageId();
await sql.doInTransaction(async () => {
await sql.insert("images", {
image_id: imageId,
format: file.mimetype.substr(6),
name: file.originalname,
format: imageFormat.ext,
name: fileName,
checksum: utils.hash(optimizedImage),
data: optimizedImage,
is_deleted: 0,
@ -76,7 +84,7 @@ router.post('', auth.checkApiAuth, multer.single('upload'), async (req, res, nex
res.send({
uploaded: true,
url: `/api/images/${imageId}/${file.originalname}`
url: `/api/images/${imageId}/${fileName}`
});
});
@ -113,9 +121,9 @@ async function resize(buffer) {
async function optimize(buffer) {
return await imagemin.buffer(buffer, {
plugins: [
// imageminMozJpeg({
// quality: 50
// }),
imageminMozJpeg({
quality: 50
}),
imageminPngQuant({
quality: "0-70"
})

View file

@ -146,6 +146,9 @@ async function pullSync(syncContext) {
else if (sync.entity_name === 'images') {
await syncUpdate.updateImage(resp, syncContext.sourceId);
}
else if (sync.entity_name === 'notes_image') {
await syncUpdate.updateNoteImage(resp, syncContext.sourceId);
}
else {
throw new Error(`Unrecognized entity type ${sync.entity_name} in sync #${sync.id}`);
}