Improve detection of jp2 filename extensions #3674

This commit is contained in:
BJJ 2023-05-20 19:12:01 +08:00 committed by Lovell Fuller
parent f5845c7e61
commit 7e6a70af44
2 changed files with 9 additions and 2 deletions

View File

@ -29,7 +29,7 @@ const formats = new Map([
['jxl', 'jxl'] ['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'); const errJp2Save = () => new Error('JP2 output requires libvips with support for OpenJPEG');
@ -75,7 +75,7 @@ function toFile (fileOut, callback) {
err = new Error('Missing output file path'); err = new Error('Missing output file path');
} else if (is.string(this.options.input.file) && path.resolve(this.options.input.file) === path.resolve(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'); 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(); err = errJp2Save();
} }
if (err) { if (err) {

View File

@ -25,6 +25,13 @@ describe('JP2 output', () => {
/JP2 output requires libvips with support for OpenJPEG/ /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 { } else {
it('JP2 Buffer to PNG Buffer', () => { it('JP2 Buffer to PNG Buffer', () => {
sharp(fs.readFileSync(fixtures.inputJp2)) sharp(fs.readFileSync(fixtures.inputJp2))