Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<PackageReference Include="BootstrapBlazor.OfficeViewer" Version="10.0.0" />
<PackageReference Include="BootstrapBlazor.OnScreenKeyboard" Version="10.0.0" />
<PackageReference Include="BootstrapBlazor.OpcDa" Version="10.0.0" />
<PackageReference Include="BootstrapBlazor.PdfReader" Version="10.0.12" />
<PackageReference Include="BootstrapBlazor.PdfReader" Version="10.0.15" />
<PackageReference Include="BootstrapBlazor.PdfViewer" Version="10.0.0" />
<PackageReference Include="BootstrapBlazor.Player" Version="10.0.1" />
<PackageReference Include="BootstrapBlazor.RDKit" Version="10.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@
<Switch @bind-Value="_showTwoPagesOneView"></Switch>
</BootstrapInputGroup>
</div>
<div class="col-12">
<Button Color="Color.Primary" OnClick="@(() => _url = "./samples/sample.pdf")" class="me-2">Sample-Url</Button>
<Button Color="Color.Primary" OnClick="@(() => _url = "./samples/sample2.pdf")" class="me-2">Sample2-Url</Button>
Comment on lines +42 to +43
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): The inline lambdas for OnClick have invalid string quoting and will not compile.

The nested double quotes in the OnClick lambdas break Razor/C# parsing. Use a different quoting strategy, e.g. escape the inner quotes (@(() => _url = "./samples/sample.pdf")) or switch to single quotes inside the string: @(() => _url = './samples/sample.pdf').

Copy link

Copilot AI Dec 13, 2025

Choose a reason for hiding this comment

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

The button label "Sample2-Url" is inconsistent with the naming used elsewhere. Consider using more descriptive labels that indicate the actual difference between the samples, or maintain consistency by using "Sample 2" (with a space) instead of "Sample2" to match the pattern if "Sample-Url" represents "Sample 1".

Suggested change
<Button Color="Color.Primary" OnClick="@(() => _url = "./samples/sample2.pdf")" class="me-2">Sample2-Url</Button>
<Button Color="Color.Primary" OnClick="@(() => _url = "./samples/sample2.pdf")" class="me-2">Sample 2-Url</Button>

Copilot uses AI. Check for mistakes.
<Button Color="Color.Danger" OnClick="GetSampleStream" class="me-2">Sample-Stream</Button>
<Button Color="Color.Danger" OnClick="GetTestStream">Sample2-Stream</Button>
</div>
</section>
<PdfReader Url="@_url" EnableThumbnails="_enableThumbnails"
ShowTwoPagesOneView="_showTwoPagesOneView" ShowDownload="_showDownload"
ShowToolbar="_showToolbar" ShowPrint="_showPrint"
ViewHeight="600px" OnDownloadAsync="OnDownloadAsync"></PdfReader>
ViewHeight="600px" OnGetStreamAsync="OnGetStreamAsync"></PdfReader>
</DemoBlock>
27 changes: 23 additions & 4 deletions src/BootstrapBlazor.Server/Components/Samples/PdfReaders.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,30 @@ public partial class PdfReaders
private bool _enableThumbnails = true;
private bool _showDownload = true;
private bool _showToolbar = true;
private string _url = "./samples/sample.pdf";
private string _url = "sample.pdf";
private string _streamFileName = "";

Comment on lines 21 to 25
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): The initial URL value uses a different path pattern than the button click handlers, which may be confusing or inconsistent.

The initial _url now points to "sample.pdf" while the buttons use "./samples/sample.pdf" and "./samples/sample2.pdf". If PdfReader resolves these differently, the first load could fail or behave differently from the buttons. Consider using a consistent path pattern (e.g., all with ./samples/... or all without) based on where the files actually reside.

Suggested change
private bool _enableThumbnails = true;
private bool _showDownload = true;
private bool _showToolbar = true;
private string _url = "./samples/sample.pdf";
private string _url = "sample.pdf";
private string _streamFileName = "";
private bool _enableThumbnails = true;
private bool _showDownload = true;
private bool _showToolbar = true;
private string _url = "./samples/sample.pdf";
private string _streamFileName = "";

private async Task OnDownloadAsync()
private async Task<Stream> OnGetStreamAsync()
{
var file = Path.Combine(WebHostEnvironment.WebRootPath, "samples", "sample.pdf");
await DownloadService.DownloadFromFileAsync($"sample_{DateTime.Now:yyyyMMddHHmmss}.pdf", file);
await Task.Yield();
Copy link

Copilot AI Dec 13, 2025

Choose a reason for hiding this comment

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

The call to await Task.Yield() appears unnecessary in this context. Task.Yield() is typically used to force asynchronous yielding in tight loops or to prevent blocking the synchronization context. Since the rest of the method is synchronous (File.OpenRead is synchronous), this adds no value and makes the code less clear. Consider removing it or using Task.FromResult if the method needs to remain async for interface compliance.

Suggested change
await Task.Yield();

Copilot uses AI. Check for mistakes.
if (string.IsNullOrEmpty(_streamFileName))
{
return Stream.Null;
}

var stream = File.OpenRead(Path.Combine(WebHostEnvironment.WebRootPath, "samples", _streamFileName));
Copy link

Copilot AI Dec 13, 2025

Choose a reason for hiding this comment

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

The method constructs a file path using _streamFileName without validating or sanitizing the input. While this is a demo/sample file, it sets a potentially dangerous precedent. An attacker could potentially manipulate _streamFileName to include path traversal sequences (e.g., "../../../sensitive.pdf") to access files outside the intended directory. Consider validating the filename to ensure it doesn't contain directory traversal characters or use Path.GetFileName to extract only the filename component.

Suggested change
var stream = File.OpenRead(Path.Combine(WebHostEnvironment.WebRootPath, "samples", _streamFileName));
var stream = File.OpenRead(Path.Combine(WebHostEnvironment.WebRootPath, "samples", Path.GetFileName(_streamFileName)));

Copilot uses AI. Check for mistakes.
return stream;
Comment on lines +35 to +36
Copy link

Copilot AI Dec 13, 2025

Choose a reason for hiding this comment

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

The file stream opened by File.OpenRead is not being properly disposed of. When OnGetStreamAsync is called, it opens a file stream and returns it to the caller. However, there's no guarantee that the caller will dispose of the stream. This can lead to file handle leaks and resource exhaustion over time. Consider implementing IDisposable pattern in the component or ensuring the stream is properly disposed after use.

Suggested change
var stream = File.OpenRead(Path.Combine(WebHostEnvironment.WebRootPath, "samples", _streamFileName));
return stream;
var filePath = Path.Combine(WebHostEnvironment.WebRootPath, "samples", _streamFileName);
var memoryStream = new MemoryStream();
using (var fileStream = File.OpenRead(filePath))
{
await fileStream.CopyToAsync(memoryStream);
}
memoryStream.Position = 0;
return memoryStream;

Copilot uses AI. Check for mistakes.
Comment on lines +35 to +36
Copy link

Copilot AI Dec 13, 2025

Choose a reason for hiding this comment

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

The File.OpenRead call does not handle potential exceptions such as FileNotFoundException, UnauthorizedAccessException, or IOException. If the file doesn't exist or cannot be accessed, this will result in an unhandled exception that could crash the component or display unhelpful errors to users. Consider wrapping this in a try-catch block and returning an appropriate error response or Stream.Null with proper error logging.

Suggested change
var stream = File.OpenRead(Path.Combine(WebHostEnvironment.WebRootPath, "samples", _streamFileName));
return stream;
try
{
var stream = File.OpenRead(Path.Combine(WebHostEnvironment.WebRootPath, "samples", _streamFileName));
return stream;
}
catch (FileNotFoundException ex)
{
Console.Error.WriteLine($"File not found: {ex.Message}");
return Stream.Null;
}
catch (UnauthorizedAccessException ex)
{
Console.Error.WriteLine($"Access denied: {ex.Message}");
return Stream.Null;
}
catch (IOException ex)
{
Console.Error.WriteLine($"IO error: {ex.Message}");
return Stream.Null;
}

Copilot uses AI. Check for mistakes.
}

private void GetTestStream()
{
_url = "";
_streamFileName = "sample2.pdf";
}
Comment on lines +39 to +43
Copy link

Copilot AI Dec 13, 2025

Choose a reason for hiding this comment

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

The method name GetTestStream is ambiguous. It's unclear what "Test" refers to in this context - it appears to load "sample2.pdf" which doesn't obviously relate to "test". Consider renaming to something more descriptive like GetSample2Stream or LoadSecondSampleStream to better reflect its purpose.

Copilot uses AI. Check for mistakes.

private void GetSampleStream()
{
_url = "";
_streamFileName = "sample.pdf";
Comment on lines +39 to +48
Copy link

Copilot AI Dec 13, 2025

Choose a reason for hiding this comment

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

The GetTestStream and GetSampleStream methods have duplicated logic - both clear the URL and set a stream filename. Consider consolidating this into a single parameterized method like LoadStreamFile(string fileName) to reduce code duplication and improve maintainability.

Suggested change
private void GetTestStream()
{
_url = "";
_streamFileName = "sample2.pdf";
}
private void GetSampleStream()
{
_url = "";
_streamFileName = "sample.pdf";
private void LoadStreamFile(string fileName)
{
_url = "";
_streamFileName = fileName;
}
private void GetTestStream()
{
LoadStreamFile("sample2.pdf");
}
private void GetSampleStream()
{
LoadStreamFile("sample.pdf");

Copilot uses AI. Check for mistakes.
}
}
Binary file not shown.
Loading