Skip to content

Commit

Permalink
Use early return in downscaleForPlateRecognizer
Browse files Browse the repository at this point in the history
This makes it easier for me to read.
  • Loading branch information
josephfrazier committed Dec 20, 2024
1 parent e24fac9 commit 4e54a16
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/alpr.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ const downscaleForPlateRecognizer = ({ buffer, targetWidth }) => {
const fileSize = buffer.length;
const maxFilesize = 2411654;

if (fileSize >= maxFilesize) {
// eslint-disable-next-line no-console
console.log(
`file size is greater than maximum of ${maxFilesize} bytes, attempting to scale down to width of ${targetWidth}`,
);

return sharp(buffer)
.resize({ width: targetWidth })
.toBuffer()
.catch(error => {
console.error('could not scale down, using unscaled image', { error });
return buffer;
})
.then(resizedBufferish => {
const resizedBuffer = Buffer.from(resizedBufferish);
// eslint-disable-next-line no-console
console.log(
`file size after scaling down: ${resizedBuffer.length} bytes`,
);
return resizedBuffer;
});
if (fileSize < maxFilesize) {
return buffer;
}

return buffer;
// eslint-disable-next-line no-console
console.log(
`file size is greater than maximum of ${maxFilesize} bytes, attempting to scale down to width of ${targetWidth}`,
);

return sharp(buffer)
.resize({ width: targetWidth })
.toBuffer()
.catch(error => {
console.error('could not scale down, using unscaled image', { error });
return buffer;
})
.then(resizedBufferish => {
const resizedBuffer = Buffer.from(resizedBufferish);
// eslint-disable-next-line no-console
console.log(
`file size after scaling down: ${resizedBuffer.length} bytes`,
);
return resizedBuffer;
});
};

function platerecognizer({ attachmentBytesRotated, PLATERECOGNIZER_TOKEN }) {
Expand Down

0 comments on commit 4e54a16

Please sign in to comment.