Skip to content
Merged
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
21 changes: 15 additions & 6 deletions src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ const loadPdf = async pdf => {
pdfViewer.setDocument(pdfDocument);

pdfDocument.getMetadata().then(metadata => {
if (metadata.contentLength === null) {
metadata.contentLength = options.data.length;
}
loadMetadata(el, pdfViewer, metadata);
});

Comment on lines 155 to 163
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Broaden the null check for contentLength to cover undefined as well.

A strict === null check misses the common case where metadata.contentLength is undefined, so getFileSize receives undefined, leading to NaN and a fallback of 0B. Use a nullish check like metadata.contentLength == null (or similar explicit guard) so both null and undefined are handled before calling loadMetadata.

Suggested change
pdfViewer.setDocument(pdfDocument);
pdfDocument.getMetadata().then(metadata => {
if (metadata.contentLength === null) {
metadata.contentLength = options.data.length;
}
loadMetadata(el, pdfViewer, metadata);
});
pdfViewer.setDocument(pdfDocument);
pdfDocument.getMetadata().then(metadata => {
if (metadata.contentLength == null) {
metadata.contentLength = options.data.length;
}
loadMetadata(el, pdfViewer, metadata);
});
}

Expand Down Expand Up @@ -208,7 +211,7 @@ const loadMetadata = (el, pdfViewer, metadata) => {
}

const filesize = el.querySelector('.bb-view-pdf-dialog-file-size');
filesize.textContent = getFilesize(metadata);
filesize.textContent = getFileSize(metadata);

const title = el.querySelector('.bb-view-pdf-dialog-title');
const author = el.querySelector('.bb-view-pdf-dialog-author');
Expand Down Expand Up @@ -291,20 +294,26 @@ function parsePdfDate(pdfDateString) {
return date;
}

const getFilesize = metadata => {
const getFileSize = metadata => {
const length = metadata.contentLength;
Comment on lines +297 to 298
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Guard getFileSize against missing or non-numeric contentLength to avoid NaN-based output.

When metadata.contentLength is undefined, null, or non-numeric, none of the branches will run and val remains 0, producing a misleading "0B" for an unknown size. Consider either early-returning a placeholder (e.g. empty string or "-") when length == null or Number.isNaN(length), or coercing once with const length = Number(metadata.contentLength) and validating before use.

let val = 0;
let unit = 'B';
if (length < 1024) {
return `${Math.round(length)}B`;
val = length;
}
else if (length < 1024 * 1024) {
return `${Math.round(length / 1024)}KB`;
unit = 'KB';
val = length / 1024;
}
else if (length < 1024 * 1024 * 1024) {
return `${length / 1024 / 1024}MB`;
unit = 'MB';
val = length / 1024 / 1024;
}
else if (length < 1024 * 1024 * 1024 * 1024) {
return `${length / 1024 / 1024 / 1024}GB`;
unit = 'GB';
val = length / 1024 / 1024 / 1024;
}
return `${Math.round(val * 100) / 100}${unit}`;
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rounding calculation Math.round(val * 100) / 100 will show inconsistent decimal places. For values like 1.5, it will display '1.5KB' instead of '1.50KB'. Consider using val.toFixed(2) for consistent two-decimal formatting across all file sizes.

Suggested change
return `${Math.round(val * 100) / 100}${unit}`;
return `${val.toFixed(2)}${unit}`;

Copilot uses AI. Check for mistakes.
}

const setObserver = el => {
Expand Down