@implements IDisposable
@Model.VDA5050CA
@if (!string.IsNullOrEmpty(CaFileInfo)) {
@CaFileInfo
} @if (!string.IsNullOrEmpty(CaFileError)) {
@CaFileError
}
@Model.VDA5050Cer
@if (!string.IsNullOrEmpty(CertFileInfo)) {
@CertFileInfo
} @if (!string.IsNullOrEmpty(CertFileError)) {
@CertFileError
}
@Model.VDA5050Key
@if (!string.IsNullOrEmpty(KeyFileInfo)) {
@KeyFileInfo
} @if (!string.IsNullOrEmpty(KeyFileError)) {
@KeyFileError
}
@if (Model.CreatedAt != default || Model.UpdatedAt != default) {
Created: @Model.CreatedAt.ToString("dd/MM/yyyy HH:mm:ss") Updated: @Model.UpdatedAt.ToString("dd/MM/yyyy HH:mm:ss")
}
@code { [Parameter] public RobotVDA5050ConfigDto Model { get; set; } = new(); [Parameter] public EventCallback ModelChanged { get; set; } public IBrowserFile? CaFile { get; set; } public IBrowserFile? CertFile { get; set; } public IBrowserFile? KeyFile { get; set; } public long MaxFileSize { get; set; } = 10 * 1024 * 1024; private EditContext? EditContext; private bool showPassword; private string PasswordInputType => showPassword ? "text" : "password"; private string PasswordIconClass => showPassword ? "mdi mdi-eye-off" : "mdi mdi-eye"; private enum FileSlot { Ca, Cert, Key } private string? CaFileInfo; private string? CaFileError; private string? CertFileInfo; private string? CertFileError; private string? KeyFileInfo; private string? KeyFileError; private async Task OnFileSelected(InputFileChangeEventArgs e, FileSlot slot) { var file = e.File; if (file is null) return; if (file.Size > MaxFileSize) { SetFileError(slot, $"File too large (max {FormatSize(MaxFileSize)})"); SetFileInfo(slot, string.Empty, 0); return; } try { using var stream = file.OpenReadStream(MaxFileSize); using var ms = new MemoryStream(); await stream.CopyToAsync(ms); var data = ms.ToArray(); SetFileError(slot, null); SetFileInfo(slot, file.Name, file.Size); SetFileName(slot, file.Name); SetBrowserFile(slot, file); _ = ModelChanged.InvokeAsync(Model); } catch { SetFileError(slot, "Failed to read file"); } } private void RemoveFile(FileSlot slot) { SetFileInfo(slot, string.Empty, 0); SetFileError(slot, null); SetFileName(slot, string.Empty); _ = ModelChanged.InvokeAsync(Model); } private void SetFileError(FileSlot slot, string? error) { switch (slot) { case FileSlot.Ca: CaFileError = error; break; case FileSlot.Cert: CertFileError = error; break; case FileSlot.Key: KeyFileError = error; break; } } private void SetFileInfo(FileSlot slot, string? name, long size) { switch (slot) { case FileSlot.Ca: CaFileInfo = string.IsNullOrEmpty(name) ? "" : $"{name} {FormatSize(size)}"; break; case FileSlot.Cert: CertFileInfo = string.IsNullOrEmpty(name) ? "" : $"{name} {FormatSize(size)}"; break; case FileSlot.Key: KeyFileInfo = string.IsNullOrEmpty(name) ? "" : $"{name} {FormatSize(size)}"; break; } } private void SetFileName(FileSlot slot, string? name) { switch (slot) { case FileSlot.Ca: Model.VDA5050CA = name; break; case FileSlot.Cert: Model.VDA5050Cer = name; break; case FileSlot.Key: Model.VDA5050Key = name; break; } } private void SetBrowserFile(FileSlot slot, IBrowserFile file) { switch (slot) { case FileSlot.Ca: CaFile = file; break; case FileSlot.Cert: CertFile = file; break; case FileSlot.Key: KeyFile = file; break; } } private static string FormatSize(long size) { if (size <= 0) return "0 B"; if (size < 1024) return $"{size} B"; double kb = size / 1024.0; if (kb < 1024) return $"{kb:F1} KB"; double mb = kb / 1024.0; return $"{mb:F2} MB"; } protected override void OnParametersSet() { if (EditContext is null || !EditContext.Model!.Equals(Model)) { if (EditContext is not null) EditContext.OnFieldChanged -= EditContext_OnFieldChanged; EditContext = new EditContext(Model); EditContext.OnFieldChanged += EditContext_OnFieldChanged; CaFileInfo = string.Empty; CertFileInfo = string.Empty; KeyFileInfo = string.Empty; } } private void TogglePasswordVisibility() { showPassword = !showPassword; } private void EditContext_OnFieldChanged(object? sender, FieldChangedEventArgs e) { _ = ModelChanged.InvokeAsync(Model); } public void Dispose() { if (EditContext is not null) EditContext.OnFieldChanged -= EditContext_OnFieldChanged; } }