Skip to content

Update webdriver upload files method #1077

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

visagang
Copy link
Contributor

@visagang visagang commented Jun 13, 2025

User description

feat: stream downloads & use IHttpClientFactory

  • Replace direct HttpClient creation with IHttpClientFactory
    – prevents socket exhaustion and long-lived handler leaks.

  • Stream file content straight to FileStream (CopyToAsync)
    – no more full byte[] buffers in RAM

  • Add per-file try/catch so one bad URL no longer aborts the batch.


PR Type

Enhancement


Description

• Replace HttpClient with IHttpClientFactory for better resource management
• Stream file downloads directly to disk instead of loading into memory
• Add error handling for individual file downloads
• Extract directory deletion logic into separate method


Changes walkthrough 📝

Relevant files
Enhancement
PlaywrightWebDriver.DoAction.cs
Enhanced file upload with streaming and error handling     

src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs

• Replace direct HttpClient instantiation with IHttpClientFactory

Stream file downloads using CopyToAsync instead of GetByteArrayAsync

Add try-catch blocks for individual file download error handling

Extract directory deletion logic into DeleteDirectory method
• Fix
minor formatting issues and duplicate typing action

+28/-14 

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Duplicate Code

    The typing action has duplicate PressSequentiallyAsync calls which will cause the content to be typed twice

    await locator.PressSequentiallyAsync(action.Content);
    await locator.PressSequentiallyAsync(action.Content);
    Resource Leak

    HttpResponseMessage from GetAsync is not properly disposed, which could lead to resource leaks

    using var fileData = await httpClient.GetAsync(fileUrl);
    var fileName = new Uri(fileUrl).AbsolutePath;
    var localPath = Path.Combine(directory, Path.GetFileName(fileName));
    await using var fs = new FileStream(localPath, FileMode.Create, FileAccess.Write, FileShare.None);
    await fileData.Content.CopyToAsync(fs);
    Logic Issue

    DeleteDirectory is called before checking if directory exists, and then Directory.CreateDirectory is called immediately after, which may cause race conditions

    DeleteDirectory(directory);
    Directory.CreateDirectory(directory);

    Copy link

    qodo-merge-pro bot commented Jun 13, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Remove duplicate typing action
    Suggestion Impact:The suggestion was directly implemented - the duplicate line "await locator.PressSequentiallyAsync(action.Content);" was removed from the code

    code diff:

    -            await locator.PressSequentiallyAsync(action.Content);

    There's a duplicate call to PressSequentiallyAsync which will type the content
    twice. Remove one of the duplicate lines to prevent double typing.

    src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs [131-132]

    -await locator.PressSequentiallyAsync(action.Content);
     await locator.PressSequentiallyAsync(action.Content);

    [Suggestion processed]

    Suggestion importance[1-10]: 9

    __

    Why: The suggestion correctly identifies a duplicated line of code introduced in the PR. The call to locator.PressSequentiallyAsync(action.Content) appears twice, which would cause the content to be typed twice. This is a functional bug, and removing the duplicate line is the correct fix.

    High
    Validate HTTP response status

    The HTTP response should be checked for success status before attempting to read
    the content. Add fileData.EnsureSuccessStatusCode() to handle HTTP error
    responses properly.

    src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightWebDriver.DoAction.cs [114-118]

     using var fileData = await httpClient.GetAsync(fileUrl);
    +fileData.EnsureSuccessStatusCode();
     var fileName = new Uri(fileUrl).AbsolutePath;
     var localPath = Path.Combine(directory, Path.GetFileName(fileName));
     await using var fs = new FileStream(localPath, FileMode.Create, FileAccess.Write, FileShare.None);
     await fileData.Content.CopyToAsync(fs);
    • Apply / Chat
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly points out that checking the HTTP response status using EnsureSuccessStatusCode() is a good practice for robust error handling. While the code is already wrapped in a try-catch block, this change would provide more specific exceptions for failed HTTP requests, improving debuggability.

    Medium
    • Update

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant