mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-02-12 21:21:37 +00:00
Refactor image handling to use file URL for Blazor and remove base64 workaround
This commit is contained in:
parent
8123099d95
commit
359e57a66f
@ -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.
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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.");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user