Resolve paths before comparing input/output destination (#2878)

This fixes an issue where if you try to write to the same destination as the
input file but you are not using absolute (or the same relative path) for both
the input and output, sharp/vips might produce errors such as:

someFile.jpg: unable to open for write
unix error: No such file or directory
This commit is contained in:
Espen Hovlandsdal
2021-09-06 17:21:43 +02:00
committed by GitHub
parent afb21135c2
commit 52307fad5d
2 changed files with 45 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
'use strict';
const path = require('path');
const is = require('./is');
const sharp = require('./sharp');
@@ -59,7 +60,7 @@ function toFile (fileOut, callback) {
let err;
if (!is.string(fileOut)) {
err = new Error('Missing output file path');
} else if (this.options.input.file === fileOut) {
} else if (is.string(this.options.input.file) && path.resolve(this.options.input.file) === path.resolve(fileOut)) {
err = new Error('Cannot use same file for input and output');
} else if (this.options.formatOut === 'input' && fileOut.toLowerCase().endsWith('.gif') && !this.constructor.format.magick.output.file) {
err = errMagickSave;