Skip to content

Commit

Permalink
Improve detection of jp2 filename extensions #3674
Browse files Browse the repository at this point in the history
  • Loading branch information
bianjunjie1981 authored and lovell committed Jun 5, 2023
1 parent f5845c7 commit 7e6a70a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const formats = new Map([
['jxl', 'jxl']
]);

const jp2Regex = /\.jp[2x]|j2[kc]$/i;
const jp2Regex = /\.(jp[2x]|j2[kc])$/i;

const errJp2Save = () => new Error('JP2 output requires libvips with support for OpenJPEG');

Expand Down Expand Up @@ -75,7 +75,7 @@ function toFile (fileOut, callback) {
err = new Error('Missing output file path');
} 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 (jp2Regex.test(fileOut) && !this.constructor.format.jp2k.output.file) {
} else if (jp2Regex.test(path.extname(fileOut)) && !this.constructor.format.jp2k.output.file) {
err = errJp2Save();
}
if (err) {
Expand Down
7 changes: 7 additions & 0 deletions test/unit/jp2.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ describe('JP2 output', () => {
/JP2 output requires libvips with support for OpenJPEG/
)
);

it('File with JP2-like suffix should not fail due to missing OpenJPEG', () => {
const output = fixtures.path('output.failj2c');
return assert.doesNotReject(
async () => sharp(fixtures.inputPngWithOneColor).toFile(output)
);
});
} else {
it('JP2 Buffer to PNG Buffer', () => {
sharp(fs.readFileSync(fixtures.inputJp2))
Expand Down

0 comments on commit 7e6a70a

Please sign in to comment.