Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,55 @@ export function parseFfprobeFrameRate(frameRate) {
return Number.isFinite(parsedValue) && parsedValue > 0 ? parsedValue : 0;
}

/**
* Parses ImageMagick %[b] / %b file-size output into bytes.
*
* ImageMagick can return human-readable values such as "12.3KB"; parseInt()
* would incorrectly report that as 12 bytes.
*
* @param {string|number} size - ImageMagick file-size value
* @returns {number} - Parsed byte count, or 0 for invalid values
*/
export function parseImageMagickFileSize(size) {
if (typeof size === 'number') {
return Number.isFinite(size) && size > 0 ? Math.round(size) : 0;
}

if (typeof size !== 'string') {
return 0;
}

const value = size.trim();
if (!value) {
return 0;
}

const match = value.match(/^(\d+(?:\.\d+)?)\s*([KMGT]?I?B)?$/i);
if (!match) {
return 0;
}

const amount = Number(match[1]);
if (!Number.isFinite(amount) || amount < 0) {
return 0;
}

const unit = (match[2] || 'B').toUpperCase();
const multipliers = {
B: 1,
KB: 1024,
KIB: 1024,
MB: 1024 ** 2,
MIB: 1024 ** 2,
GB: 1024 ** 3,
GIB: 1024 ** 3,
TB: 1024 ** 4,
TIB: 1024 ** 4
};

return Math.round(amount * (multipliers[unit] || 1));
}

/**
* Gets video metadata using ffprobe
*
Expand Down Expand Up @@ -252,7 +301,7 @@ export async function getImageMetadata(inputPath) {
result.format = {
filename: inputPath,
formatName: format,
size: parseInt(filesize) || 0
size: parseImageMagickFileSize(filesize)
};

// Image metadata
Expand Down
18 changes: 17 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import { expect } from 'chai';
import { parseFfprobeFrameRate } from '../src/utils.js';
import { parseFfprobeFrameRate, parseImageMagickFileSize } from '../src/utils.js';

describe('Utils Module', function() {
describe('parseFfprobeFrameRate()', function() {
Expand All @@ -25,4 +25,20 @@ describe('Utils Module', function() {
expect(parseFfprobeFrameRate(null)).to.equal(0);
});
});

describe('parseImageMagickFileSize()', function() {
it('should parse ImageMagick byte and unit-suffixed sizes', function() {
expect(parseImageMagickFileSize('512B')).to.equal(512);
expect(parseImageMagickFileSize('12.5KB')).to.equal(12800);
expect(parseImageMagickFileSize('1.5MB')).to.equal(1572864);
expect(parseImageMagickFileSize('2MiB')).to.equal(2097152);
});

it('should reject invalid ImageMagick file-size values', function() {
expect(parseImageMagickFileSize('')).to.equal(0);
expect(parseImageMagickFileSize('abc')).to.equal(0);
expect(parseImageMagickFileSize(null)).to.equal(0);
expect(parseImageMagickFileSize(-1)).to.equal(0);
});
});
});