mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
CLI: Fix: Do not resize images if they are already below the max dimensions
This commit is contained in:
parent
c27861d40f
commit
ed3970be81
@ -8,10 +8,14 @@ const Resource = require('lib/models/Resource.js');
|
||||
const BaseModel = require('lib/BaseModel.js');
|
||||
const { shim } = require('lib/shim');
|
||||
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000; // The first test is slow because the database needs to be built
|
||||
|
||||
process.on('unhandledRejection', (reason, p) => {
|
||||
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
||||
});
|
||||
|
||||
const testImagePath = __dirname + '/../tests/support/photo.jpg';
|
||||
|
||||
describe('models_Resource', function() {
|
||||
|
||||
beforeEach(async (done) => {
|
||||
@ -23,7 +27,7 @@ describe('models_Resource', function() {
|
||||
it('should have a "done" fetch_status when created locally', asyncTest(async () => {
|
||||
let folder1 = await Folder.save({ title: "folder1" });
|
||||
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
||||
await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg');
|
||||
await shim.attachFileToNote(note1, testImagePath);
|
||||
let resource1 = (await Resource.all())[0];
|
||||
let ls = await Resource.localState(resource1);
|
||||
expect(ls.fetch_status).toBe(Resource.FETCH_STATUS_DONE);
|
||||
@ -32,7 +36,7 @@ describe('models_Resource', function() {
|
||||
it('should have a default local state', asyncTest(async () => {
|
||||
let folder1 = await Folder.save({ title: "folder1" });
|
||||
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
||||
await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg');
|
||||
await shim.attachFileToNote(note1, testImagePath);
|
||||
let resource1 = (await Resource.all())[0];
|
||||
let ls = await Resource.localState(resource1);
|
||||
expect(!ls.id).toBe(true);
|
||||
@ -43,7 +47,7 @@ describe('models_Resource', function() {
|
||||
it('should save and delete local state', asyncTest(async () => {
|
||||
let folder1 = await Folder.save({ title: "folder1" });
|
||||
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
||||
await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg');
|
||||
await shim.attachFileToNote(note1, testImagePath);
|
||||
let resource1 = (await Resource.all())[0];
|
||||
await Resource.setLocalState(resource1, { fetch_status: Resource.FETCH_STATUS_IDLE });
|
||||
|
||||
@ -56,4 +60,31 @@ describe('models_Resource', function() {
|
||||
expect(!ls.id).toBe(true);
|
||||
}));
|
||||
|
||||
it('should resize the resource if the image is below the required dimensions', asyncTest(async () => {
|
||||
let folder1 = await Folder.save({ title: "folder1" });
|
||||
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
||||
const previousMax = Resource.IMAGE_MAX_DIMENSION;
|
||||
Resource.IMAGE_MAX_DIMENSION = 5;
|
||||
await shim.attachFileToNote(note1, testImagePath);
|
||||
Resource.IMAGE_MAX_DIMENSION = previousMax;
|
||||
let resource1 = (await Resource.all())[0];
|
||||
|
||||
const originalStat = await shim.fsDriver().stat(testImagePath);
|
||||
const newStat = await shim.fsDriver().stat(Resource.fullPath(resource1));
|
||||
|
||||
expect(newStat.size < originalStat.size).toBe(true);
|
||||
}));
|
||||
|
||||
it('should not resize the resource if the image is below the required dimensions', asyncTest(async () => {
|
||||
let folder1 = await Folder.save({ title: "folder1" });
|
||||
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
||||
await shim.attachFileToNote(note1, testImagePath);
|
||||
let resource1 = (await Resource.all())[0];
|
||||
|
||||
const originalStat = await shim.fsDriver().stat(testImagePath);
|
||||
const newStat = await shim.fsDriver().stat(Resource.fullPath(resource1));
|
||||
|
||||
expect(originalStat.size).toBe(newStat.size);
|
||||
}));
|
||||
|
||||
});
|
@ -59,12 +59,13 @@ function shimInit() {
|
||||
}
|
||||
|
||||
const resizeImage_ = async function(filePath, targetPath, mime) {
|
||||
const maxDim = Resource.IMAGE_MAX_DIMENSION;
|
||||
|
||||
if (shim.isElectron()) { // For Electron
|
||||
const nativeImage = require('electron').nativeImage;
|
||||
let image = nativeImage.createFromPath(filePath);
|
||||
if (image.isEmpty()) throw new Error('Image is invalid or does not exist: ' + filePath);
|
||||
|
||||
const maxDim = Resource.IMAGE_MAX_DIMENSION;
|
||||
const size = image.getSize();
|
||||
|
||||
if (size.width <= maxDim && size.height <= maxDim) {
|
||||
@ -85,13 +86,19 @@ function shimInit() {
|
||||
} else { // For the CLI tool
|
||||
const sharp = require('sharp');
|
||||
|
||||
const image = sharp(filePath);
|
||||
const md = await image.metadata();
|
||||
|
||||
if (md.width <= maxDim && md.height <= maxDim) {
|
||||
shim.fsDriver().copy(filePath, targetPath);
|
||||
return;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
sharp(filePath)
|
||||
.resize(Resource.IMAGE_MAX_DIMENSION, Resource.IMAGE_MAX_DIMENSION, {
|
||||
image.resize(Resource.IMAGE_MAX_DIMENSION, Resource.IMAGE_MAX_DIMENSION, {
|
||||
fit: 'inside',
|
||||
withoutEnlargement: true,
|
||||
})
|
||||
.toFile(targetPath, (err, info) => {
|
||||
}).toFile(targetPath, (err, info) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user