From e32faac17a302b2478dea36875d16fe05361661a Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Thu, 22 May 2014 22:27:02 +0100 Subject: [PATCH] Prevent writing output file over input file - closes #28 --- index.js | 8 ++++++-- tests/unit.js | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5f758544..13fbe539 100755 --- a/index.js +++ b/index.js @@ -107,9 +107,13 @@ Sharp.prototype.resize = function(width, height) { Sharp.prototype.write = function(output, callback) { if (!output || output.length === 0) { - throw 'Invalid output'; + callback('Invalid output'); } else { - this._sharp(output, callback); + if (this.options.inFile === output) { + callback('Cannot use same file for input and output'); + } else { + this._sharp(output, callback); + } } return this; }; diff --git a/tests/unit.js b/tests/unit.js index bdb8d574..9e393e07 100755 --- a/tests/unit.js +++ b/tests/unit.js @@ -145,5 +145,12 @@ async.series([ done(); }); }); + }, + // Attempt to output to input, should fail + function(done) { + sharp(inputJpg).write(inputJpg, function(err) { + assert(!!err); + done(); + }); } ]);