Skip to content

Commit

Permalink
Update error handling for non-HEIC images
Browse files Browse the repository at this point in the history
  • Loading branch information
szapp committed Mar 6, 2024
1 parent ad227fa commit 1a7d573
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "heic-jpg-exif",
"version": "0.2.0",
"version": "0.3.0",
"description": "Conversion extending heic-convert to retain essential EXIF metadata",
"main": "src/index.js",
"scripts": {
Expand Down
10 changes: 8 additions & 2 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ const convert = async (inputFile, outputPath = null, quality = 1) => {
const exr = new exifr.Exifr(options);
await exr.read(inputFile);
var { ifd0, exif, gps } = await exr.parse();
if (!(exr.fileParser instanceof exifr.fileParsers.get('heic'))) {
if (exr.fileParser instanceof exifr.fileParsers.get('jpeg'))
throw new TypeError('Input is already a JPEG image');
throw new TypeError('Input is not a HEIC image');
}

const filterExifKeys = (tag, raw) => {
if (typeof raw === 'undefined') return {}
let filteredKeys = Object.values(piexif.TagValues[tag]).map(String);
return Object.keys(raw)
.filter(key => filteredKeys.includes(key))
Expand Down Expand Up @@ -62,9 +68,9 @@ const convert = async (inputFile, outputPath = null, quality = 1) => {
const newData = piexif.insert(exifBytes, imgData);
const newJpeg = Buffer.from(newData, 'binary');
if (outputPath)
await fs.writeFileSync(outputPath, newJpeg);
fs.writeFileSync(outputPath, newJpeg);
else
return newJpeg
return newJpeg;
};

module.exports = {
Expand Down

0 comments on commit 1a7d573

Please sign in to comment.