mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-02-15 08:41: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;
|
public bool IsImage { get; } = Type == FileAttachmentType.IMAGE;
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
public string FilePathAsUrl { get; } = $"file:///{FilePath.Replace('\\', '/')}";
|
public string FilePathAsUrl { get; } = FileHandler.CreateFileUrl(FilePath);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a value indicating whether the file still exists on the file system.
|
/// Gets a value indicating whether the file still exists on the file system.
|
||||||
|
|||||||
@ -31,7 +31,7 @@
|
|||||||
@if (this.Document?.IsImage ?? false)
|
@if (this.Document?.IsImage ?? false)
|
||||||
{
|
{
|
||||||
<MudTabPanel Text="@T("Image View")" Icon="@Icons.Material.Filled.Image">
|
<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>
|
</MudTabPanel>
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@ -30,41 +30,25 @@ public partial class DocumentCheckDialog : MSGComponentBase
|
|||||||
[Inject]
|
[Inject]
|
||||||
private ILogger<DocumentCheckDialog> Logger { get; init; } = null!;
|
private ILogger<DocumentCheckDialog> Logger { get; init; } = null!;
|
||||||
|
|
||||||
private string imageDataUrl = string.Empty;
|
|
||||||
|
|
||||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||||
{
|
{
|
||||||
if (firstRender && this.Document is not null)
|
if (firstRender && this.Document is not null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (this.Document.IsImage)
|
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
|
|
||||||
{
|
{
|
||||||
var fileContent = await UserFile.LoadFileData(this.Document.FilePath, this.RustService, this.DialogService);
|
var fileContent = await UserFile.LoadFileData(this.Document.FilePath, this.RustService, this.DialogService);
|
||||||
this.FileContent = fileContent;
|
this.FileContent = fileContent;
|
||||||
this.StateHasChanged();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
this.Logger.LogError(ex, "Failed to load file content from '{FilePath}'", this.Document);
|
this.Logger.LogError(ex, "Failed to load file content from '{FilePath}'", this.Document);
|
||||||
this.FileContent = string.Empty;
|
this.FileContent = string.Empty;
|
||||||
this.StateHasChanged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.StateHasChanged();
|
||||||
}
|
}
|
||||||
else if (firstRender)
|
else if (firstRender)
|
||||||
this.Logger.LogWarning("Document check dialog opened without a valid file path.");
|
this.Logger.LogWarning("Document check dialog opened without a valid file path.");
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user