Skip to content

Enhancement: Support both HTTP URLs and local file paths for image replacement in WordDocumentGenerator #48

@coderabbitai

Description

@coderabbitai

Description

The current implementation of image replacement in WordDocumentGenerator.cs only supports downloading images from HTTP/HTTPS URLs using HttpClient. This limits the functionality when users want to use local image files.

Current Behavior

The ReplaceImagePlaceholders method currently only handles HTTP URLs:

// Asynchronously download image data using HttpClient
byte[] imageData = await _httpClient.GetByteArrayAsync(imagePath);

Proposed Enhancement

Modify the code to detect whether the image path is:

  1. An HTTP/HTTPS URL - continue using HttpClient
  2. A local file path - use File.ReadAllBytesAsync() instead

Suggested Implementation

byte[] imageData;
if (Uri.TryCreate(imagePath, UriKind.Absolute, out Uri? imageUri) && 
    (imageUri.Scheme == Uri.UriSchemeHttp || imageUri.Scheme == Uri.UriSchemeHttps))
{
    // Download from HTTP/HTTPS URL
    imageData = await _httpClient.GetByteArrayAsync(imagePath);
}
else
{
    // Read from local file
    imageData = await File.ReadAllBytesAsync(imagePath);
}

Benefits

  • Increased flexibility for users who want to use local images
  • Better support for various deployment scenarios
  • Maintains backward compatibility with existing HTTP URL usage

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions