Skip to content

feat(PdfReader): getFileSize support stream#843

Merged
ArgoZhang merged 2 commits intomasterfrom
feat-pdf
Dec 18, 2025
Merged

feat(PdfReader): getFileSize support stream#843
ArgoZhang merged 2 commits intomasterfrom
feat-pdf

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Dec 18, 2025

Link issues

fixes #842

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Improve PDF reader file size handling and display in the JavaScript viewer.

Bug Fixes:

  • Fallback to the PDF data length when metadata contentLength is missing so file size can still be determined, e.g., for streamed sources.

Enhancements:

  • Unify and rename the file size helper to getFileSize and standardize file size formatting to rounded values with up to two decimal places.

Copilot AI review requested due to automatic review settings December 18, 2025 04:07
@bb-auto bb-auto Bot added the enhancement New feature or request label Dec 18, 2025
@bb-auto bb-auto Bot added this to the v9.2.0 milestone Dec 18, 2025
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Dec 18, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates the PDF reader’s file size handling to support stream-based content by ensuring content length is set when missing and by improving the human-readable file size formatter and its usage.

Sequence diagram for updated PDF metadata and file size handling

sequenceDiagram
    actor User
    participant PdfReader as PdfReaderComponent
    participant PdfJs as PdfJsLibrary
    participant PdfDoc as PdfDocument
    participant Dom as DialogElement

    User->>PdfReader: openPdf(options)
    PdfReader->>PdfJs: loadPdf(options.data)
    PdfJs->>PdfDoc: createPdfDocument(options.data)
    PdfJs->>PdfDoc: getMetadata()
    PdfDoc-->>PdfJs: metadata
    PdfJs->>PdfReader: metadata

    alt metadata.contentLength is null
        PdfReader->>PdfReader: metadata.contentLength = options.data.length
    end

    PdfReader->>Dom: loadMetadata(Dom, pdfViewer, metadata)
    PdfReader->>PdfReader: getFileSize(metadata)
    PdfReader-->>Dom: filesize textContent

    Dom-->>User: show updated file size in dialog
Loading

File-Level Changes

Change Details Files
Ensure content length is available when loading PDFs so file size can be computed for streamed data.
  • In loadPdf, if metadata.contentLength is null, set it from options.data.length before calling loadMetadata.
  • Pass the updated metadata object with guaranteed contentLength into loadMetadata for downstream size display.
src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js
Standardize and improve the file size formatter used in the PDF metadata display.
  • Rename getFilesize to getFileSize and update its usage in loadMetadata.
  • Refactor the file size calculation to use a value/unit pair, rounding to two decimal places and supporting B, KB, MB, and GB units.
  • Ensure filesize DOM element text content is set using the new getFileSize output.
src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js

Assessment against linked issues

Issue Objective Addressed Explanation
#842 Ensure PdfReader's file size display (getFileSize) works when the PDF is loaded from a stream where metadata.contentLength may be null.
#842 Use the getFileSize helper consistently to render human-readable file sizes in the PdfReader UI.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ArgoZhang ArgoZhang changed the title getFileSize support stream feat(PdfReader): getFileSize support stream Dec 18, 2025
@ArgoZhang ArgoZhang merged commit 5bccde5 into master Dec 18, 2025
5 of 6 checks passed
@ArgoZhang ArgoZhang deleted the feat-pdf branch December 18, 2025 04:08
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • In getFileSize, consider guarding against metadata.contentLength being null/undefined or non-numeric before doing numeric comparisons, to avoid runtime errors when metadata is incomplete or streamed.
  • When falling back to options.data.length for metadata.contentLength, verify that options and options.data always exist and that length reflects the byte length (e.g., use byteLength for ArrayBuffer/Uint8Array) so file size is computed accurately for streams.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `getFileSize`, consider guarding against `metadata.contentLength` being `null`/`undefined` or non-numeric before doing numeric comparisons, to avoid runtime errors when metadata is incomplete or streamed.
- When falling back to `options.data.length` for `metadata.contentLength`, verify that `options` and `options.data` always exist and that `length` reflects the byte length (e.g., use `byteLength` for ArrayBuffer/Uint8Array) so file size is computed accurately for streams.

## Individual Comments

### Comment 1
<location> `src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:155-163` </location>
<code_context>
     pdfViewer.setDocument(pdfDocument);

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

<issue_to_address>
**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`.

```suggestion
    pdfViewer.setDocument(pdfDocument);

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

    }
```
</issue_to_address>

### Comment 2
<location> `src/components/BootstrapBlazor.PdfReader/PdfReader.razor.js:297-298` </location>
<code_context>
 }

-const getFilesize = metadata => {
+const getFileSize = metadata => {
     const length = metadata.contentLength;
+    let val = 0;
+    let unit = 'B';
</code_context>

<issue_to_address>
**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.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines 155 to 163
pdfViewer.setDocument(pdfDocument);

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

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);
});
}

Comment on lines +297 to 298
const getFileSize = metadata => {
const length = metadata.contentLength;
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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances the getFileSize function to support PDF streams by adding a fallback mechanism when contentLength is null, and improves file size formatting with consistent decimal precision.

Key Changes:

  • Added null check for contentLength with fallback to options.data.length for stream-based PDFs
  • Refactored getFileSize to apply consistent rounding (2 decimal places) across all file size units

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(PdfReader): getFileSize support stream

2 participants