-
-
Notifications
You must be signed in to change notification settings - Fork 385
feat(PdfReader): add load progress function #7322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0f70a4a
494af71
743361b
36574be
6ac8e9f
ed946ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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> | ||||||
|
||||||
| <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> |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await Task.Yield(); |
Copilot
AI
Dec 13, 2025
There was a problem hiding this comment.
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.
| var stream = File.OpenRead(Path.Combine(WebHostEnvironment.WebRootPath, "samples", _streamFileName)); | |
| var stream = File.OpenRead(Path.Combine(WebHostEnvironment.WebRootPath, "samples", Path.GetFileName(_streamFileName))); |
Copilot
AI
Dec 13, 2025
There was a problem hiding this comment.
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.
| 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
AI
Dec 13, 2025
There was a problem hiding this comment.
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.
| 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
AI
Dec 13, 2025
There was a problem hiding this comment.
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
AI
Dec 13, 2025
There was a problem hiding this comment.
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.
| 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"); |
There was a problem hiding this comment.
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
OnClickhave invalid string quoting and will not compile.The nested double quotes in the
OnClicklambdas 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').