Refactor image handling to use file URL for Blazor and remove base64 workaround

This commit is contained in:
Thorsten Sommer 2025-12-30 15:18:37 +01:00
parent 8123099d95
commit 359e57a66f
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 6 additions and 22 deletions

View File

@ -36,9 +36,9 @@ public record FileAttachment(FileAttachmentType Type, string FileName, string Fi
public bool IsImage { get; } = Type == FileAttachmentType.IMAGE;
/// <summary>
/// Gets the file path formatted as a file URL (file:///).
/// Gets the file path for loading the file from the web browser-side (Blazor).
/// </summary>
public string FilePathAsUrl { get; } = $"file:///{FilePath.Replace('\\', '/')}";
public string FilePathAsUrl { get; } = FileHandler.CreateFileUrl(FilePath);
/// <summary>
/// Gets a value indicating whether the file still exists on the file system.

View File

@ -31,7 +31,7 @@
@if (this.Document?.IsImage ?? false)
{
<MudTabPanel Text="@T("Image View")" Icon="@Icons.Material.Filled.Image">
<MudImage ObjectFit="ObjectFit.ScaleDown" Src="@this.imageDataUrl"/>
<MudImage ObjectFit="ObjectFit.ScaleDown" Src="@this.Document.FilePathAsUrl"/>
</MudTabPanel>
}
else

View File

@ -30,41 +30,25 @@ public partial class DocumentCheckDialog : MSGComponentBase
[Inject]
private ILogger<DocumentCheckDialog> Logger { get; init; } = null!;
private string imageDataUrl = string.Empty;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && this.Document is not null)
{
try
{
if (this.Document.IsImage)
{
// Load image as Base64 data URL since browsers cannot access local file:// URLs:
if (this.Document is FileAttachmentImage imageAttachment)
{
var (success, base64Content) = await imageAttachment.TryAsBase64();
if (success)
{
var mimeType = imageAttachment.DetermineMimeType();
this.imageDataUrl = ImageHelpers.ToDataUrl(base64Content, mimeType);
this.StateHasChanged();
}
}
}
else
if (!this.Document.IsImage)
{
var fileContent = await UserFile.LoadFileData(this.Document.FilePath, this.RustService, this.DialogService);
this.FileContent = fileContent;
this.StateHasChanged();
}
}
catch (Exception ex)
{
this.Logger.LogError(ex, "Failed to load file content from '{FilePath}'", this.Document);
this.FileContent = string.Empty;
this.StateHasChanged();
}
this.StateHasChanged();
}
else if (firstRender)
this.Logger.LogWarning("Document check dialog opened without a valid file path.");