Files

66 lines
2.3 KiB
Plaintext

@using Microsoft.AspNetCore.Components.Forms
@using SharepointToolbox.Web.Core.Models
@inject TranslationSource T
@* Reusable logo picker. Reads an image into a base64 LogoData (no disk/blob storage). *@
<div class="logo-upload">
@if (Value is not null)
{
<div class="flex-row" style="gap:12px;align-items:center">
<img src="data:@Value.MimeType;base64,@Value.Base64" alt=""
style="max-height:60px;max-width:200px;object-fit:contain;border:1px solid var(--border);border-radius:4px;padding:4px;background:#fff" />
<button type="button" class="btn btn-secondary btn-sm" @onclick="Remove">@T["logoupload.remove"]</button>
</div>
}
else
{
<InputFile OnChange="OnChange" accept="image/png,image/jpeg,image/svg+xml,image/gif" />
<small class="text-muted d-block">@string.Format(T["logoupload.hint"], MaxBytes / 1024)</small>
}
@if (!string.IsNullOrEmpty(_error)) { <div class="alert alert-error mt-8">@_error</div> }
</div>
@code {
[Parameter] public LogoData? Value { get; set; }
[Parameter] public EventCallback<LogoData?> ValueChanged { get; set; }
/// <summary>Upload size cap. Logos are stored inline as base64 in JSON, so keep small.</summary>
[Parameter] public long MaxBytes { get; set; } = 512 * 1024;
private string _error = string.Empty;
private async Task OnChange(InputFileChangeEventArgs e)
{
_error = string.Empty;
var file = e.File;
if (file is null) return;
if (file.Size > MaxBytes)
{
_error = string.Format(T["logoupload.err.toolarge"], file.Size / 1024, MaxBytes / 1024);
return;
}
try
{
using var ms = new MemoryStream();
await file.OpenReadStream(MaxBytes).CopyToAsync(ms);
var mime = string.IsNullOrWhiteSpace(file.ContentType) ? "image/png" : file.ContentType;
var logo = new LogoData { Base64 = Convert.ToBase64String(ms.ToArray()), MimeType = mime };
Value = logo;
await ValueChanged.InvokeAsync(logo);
}
catch (Exception ex)
{
_error = string.Format(T["logoupload.err.read"], ex.Message);
}
}
private async Task Remove()
{
Value = null;
_error = string.Empty;
await ValueChanged.InvokeAsync(null);
}
}