This commit is contained in:
Đăng Nguyễn
2025-11-03 10:29:18 +07:00
parent aea55d52f1
commit 70e27da4a2
14 changed files with 883 additions and 71 deletions

View File

@@ -1,5 +1,6 @@
@implements IDisposable
<div class="d-flex w-100 h-100 flex-column">
<EditForm EditContext="EditContext">
<DataAnnotationsValidator />
@@ -71,12 +72,27 @@
<ValidationMessage For="@(() => Model.VDA5050EnableTls)" />
</div>
<div class="row g-0 mb-2">
<div class="mb-2">
<label class="form-label" for="caFile">CA File</label>
<InputFile class="form-control" id="caFile" OnChange="e => OnFileSelected(e, FileSlot.Ca)" accept=".crt,.pem,.cer,.pfx,.key,.jks" />
@if (!string.IsNullOrEmpty(CaFileName))
<div class="custom-file-input-wrapper position-relative">
<InputFile class="d-none" id="caFile" OnChange="e => OnFileSelected(e, FileSlot.Ca)" accept=".crt,.pem,.cer,.pfx,.key,.jks" />
<div class="form-control d-flex align-items-center gap-2 ps-0">
<label for="caFile" class="upload-btn d-flex align-items-center gap-1">
<i class="mdi mdi-attachment"></i>
</label>
<span id="fileNameDisplay" class="text-muted flex-grow-1 text-truncate" style="max-width: 200px;">
@Model.VDA5050CA
</span>
<button class="password-toggle-btn position-absolute rounded-end-2 top-50 translate-middle-y" type="button" @onclick="() => RemoveFile(FileSlot.Ca)" aria-label="Remove client certificate file">
<i class="mdi mdi-close" aria-hidden="true"></i>
</button>
</div>
</div>
@if (!string.IsNullOrEmpty(CaFileInfo))
{
<div class="small text-muted mt-1 mb-1">@CaFileName (@FormatSize(CaFileSize))</div>
<div class="small text-muted mt-1 mb-1">@CaFileInfo</div>
}
@if (!string.IsNullOrEmpty(CaFileError))
{
@@ -84,12 +100,27 @@
}
</div>
<div class="row g-0 mb-2">
<div class="mb-2">
<label class="form-label" for="clientCertFile">Client Certificate File</label>
<InputFile class="form-control" id="clientCertFile" OnChange="e => OnFileSelected(e, FileSlot.Cert)" accept=".crt,.pem,.cer,.pfx,.key,.jks" />
@if (!string.IsNullOrEmpty(CertFileName))
<div class="custom-file-input-wrapper position-relative">
<InputFile class="d-none" id="clientCertFile" OnChange="e => OnFileSelected(e, FileSlot.Cert)" accept=".crt,.pem,.cer,.pfx,.key,.jks" />
<div class="form-control d-flex align-items-center gap-2 ps-0">
<label for="clientCertFile" class="upload-btn d-flex align-items-center gap-1">
<i class="mdi mdi-attachment"></i>
</label>
<span id="fileNameDisplay" class="text-muted flex-grow-1 text-truncate" style="max-width: 200px;">
@Model.VDA5050Cer
</span>
<button class="password-toggle-btn position-absolute rounded-end-2 top-50 translate-middle-y" type="button" @onclick="() => RemoveFile(FileSlot.Cert)" aria-label="Remove client certificate file">
<i class="mdi mdi-close" aria-hidden="true"></i>
</button>
</div>
</div>
@if (!string.IsNullOrEmpty(CertFileInfo))
{
<div class="small text-muted mt-1 mb-1">@CertFileName (@FormatSize(CertFileSize))</div>
<div class="small text-muted mt-1 mb-1">@CertFileInfo</div>
}
@if (!string.IsNullOrEmpty(CertFileError))
{
@@ -97,12 +128,27 @@
}
</div>
<div class="row g-0 mb-2">
<div class="mb-2">
<label class="form-label" for="clientKeyFile">Client Key File</label>
<InputFile class="form-control" id="clientKeyFile" OnChange="e => OnFileSelected(e, FileSlot.Key)" accept=".crt,.pem,.cer,.pfx,.key,.jks" />
@if (!string.IsNullOrEmpty(KeyFileName))
<div class="custom-file-input-wrapper position-relative">
<InputFile class="d-none" id="clientKeyFile" OnChange="e => OnFileSelected(e, FileSlot.Key)" accept=".crt,.pem,.cer,.pfx,.key,.jks" />
<div class="form-control d-flex align-items-center gap-2 ps-0">
<label for="clientKeyFile" class="upload-btn d-flex align-items-center gap-1">
<i class="mdi mdi-attachment"></i>
</label>
<span id="fileNameDisplay" class="text-muted flex-grow-1 text-truncate" style="max-width: 200px;">
@Model.VDA5050Key
</span>
<button class="password-toggle-btn position-absolute rounded-end-2 top-50 translate-middle-y" type="button" @onclick="() => RemoveFile(FileSlot.Key)" aria-label="Remove client key file">
<i class="mdi mdi-close" aria-hidden="true"></i>
</button>
</div>
</div>
@if (!string.IsNullOrEmpty(KeyFileInfo))
{
<div class="small text-muted mt-1 mb-1">@KeyFileName (@FormatSize(KeyFileSize))</div>
<div class="small text-muted mt-1 mb-1">@KeyFileInfo</div>
}
@if (!string.IsNullOrEmpty(KeyFileError))
{
@@ -128,7 +174,6 @@
</div>
</div>
@code {
[Parameter]
public RobotVDA5050ConfigDto Model { get; set; } = new();
@@ -136,6 +181,11 @@
[Parameter]
public EventCallback<RobotVDA5050ConfigDto> 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";
@@ -143,22 +193,14 @@
private enum FileSlot { Ca, Cert, Key }
private string? CaFileName;
private long CaFileSize;
private string? CaFileInfo;
private string? CaFileError;
private byte[]? CaFileData;
private string? CertFileName;
private long CertFileSize;
private string? CertFileInfo;
private string? CertFileError;
private byte[]? CertFileData;
private string? KeyFileName;
private long KeyFileSize;
private string? KeyFileInfo;
private string? KeyFileError;
private byte[]? KeyFileData;
private const long MaxFileSize = 10 * 1024 * 1024; // 10 MB
private async Task OnFileSelected(InputFileChangeEventArgs e, FileSlot slot)
{
@@ -169,7 +211,7 @@
if (file.Size > MaxFileSize)
{
SetFileError(slot, $"File too large (max {FormatSize(MaxFileSize)})");
SetFileInfo(slot, null, 0, null);
SetFileInfo(slot, string.Empty, 0);
return;
}
@@ -180,30 +222,25 @@
await stream.CopyToAsync(ms);
var data = ms.ToArray();
SetFileData(slot, data, file.Name, file.Size);
ClearFileError(slot);
// Optionally propagate change to parent via ModelChanged if required
// _ = ModelChanged.InvokeAsync(Model);
SetFileError(slot, null);
SetFileInfo(slot, file.Name, file.Size);
SetFileName(slot, file.Name);
SetBrowserFile(slot, file);
_ = ModelChanged.InvokeAsync(Model);
}
catch (Exception ex)
catch
{
SetFileError(slot, "Failed to read file");
Console.Error.WriteLine(ex);
}
}
private void SetFileData(FileSlot slot, byte[] data, string name, long size)
private void RemoveFile(FileSlot slot)
{
switch (slot)
{
case FileSlot.Ca:
CaFileData = data; CaFileName = name; CaFileSize = size; break;
case FileSlot.Cert:
CertFileData = data; CertFileName = name; CertFileSize = size; break;
case FileSlot.Key:
KeyFileData = data; KeyFileName = name; KeyFileSize = size; break;
}
SetFileInfo(slot, string.Empty, 0);
SetFileError(slot, null);
SetFileName(slot, string.Empty);
_ = ModelChanged.InvokeAsync(Model);
}
private void SetFileError(FileSlot slot, string? error)
@@ -216,15 +253,33 @@
}
}
private void ClearFileError(FileSlot slot) => SetFileError(slot, null);
private void SetFileInfo(FileSlot slot, string? name, long size, byte[]? data)
private void SetFileInfo(FileSlot slot, string? name, long size)
{
switch (slot)
{
case FileSlot.Ca: CaFileName = name; CaFileSize = size; CaFileData = data; break;
case FileSlot.Cert: CertFileName = name; CertFileSize = size; CertFileData = data; break;
case FileSlot.Key: KeyFileName = name; KeyFileSize = size; KeyFileData = data; break;
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;
}
}
@@ -245,13 +300,10 @@
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;
}
CertFileName = string.Empty;
CertFileError = string.Empty;
KeyFileName = string.Empty;
KeyFileError = string.Empty;
CaFileName = string.Empty;
CaFileError = string.Empty;
}
private void TogglePasswordVisibility()

View File

@@ -40,4 +40,54 @@
.password-toggle-btn .mdi {
font-size: 1.15rem;
pointer-events: none; /* let clicks hit the button, not the icon */
}
}
.file-input-wrapper {
max-width: 100%;
}
.custom-file-input-wrapper {
max-width: 100%;
}
.custom-file-input-wrapper .form-control {
height: 38px;
padding-right: 50px;
background-color: #f8f9fa;
border: 1px solid #ced4da;
}
.text-truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* === NÚT LABEL GIỐNG BUTTON THẬT === */
.upload-btn {
cursor: pointer;
transition: all 0.2s ease;
user-select: none;
min-width: 50px;
font-size: 23px;
justify-content: center;
border-right: 1px solid silver;
border-radius: 0.25rem;
}
/* HOVER: đổi màu nền + viền */
.upload-btn:hover {
background-color: #e7f3ff !important;
}
/* FOCUS: khi tab đến (accessibility) */
.upload-btn:focus {
outline: 2px solid transparent;
box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.5) !important;
}
/* ACTIVE: khi nhấn */
.upload-btn:active {
transform: translateY(0);
background-color: #d0e8ff !important;
}

View File

@@ -83,7 +83,7 @@
@switch (SelectedType)
{
case RobotConfigType.VDA5050:
<RobotApp.Client.Pages.Components.Config.RobotVDA5050Config @bind-Model="SelectedVda" />
<RobotApp.Client.Pages.Components.Config.RobotVDA5050Config @ref="@RobotVDA5050ConfigRef" @bind-Model="SelectedVda" />
break;
case RobotConfigType.Safety:
<RobotApp.Client.Pages.Components.Config.RobotSafetyConfig @bind-Model="SelectedSafety" />
@@ -204,6 +204,8 @@
private RobotConfigType SelectedType = RobotConfigType.VDA5050;
private RobotApp.Client.Pages.Components.Config.RobotVDA5050Config RobotVDA5050ConfigRef = default!;
private int SelectedIndex = -1;
private bool HasSelection => SelectedIndex >= 0;
private bool IsLoading = false;

View File

@@ -2,6 +2,7 @@
using RobotApp.Common.Shares;
using RobotApp.Common.Shares.Dtos;
using RobotApp.Common.Shares.Enums;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Xml.Schema;
@@ -305,6 +306,33 @@ public partial class RobotConfigManager
}
}
private async Task<bool> SaveCertificates(Guid id)
{
using var content = new MultipartFormDataContent();
if (RobotVDA5050ConfigRef.CaFile is not null)
{
var fileContent = new StreamContent(RobotVDA5050ConfigRef.CaFile.OpenReadStream(maxAllowedSize: RobotVDA5050ConfigRef.MaxFileSize));
content.Add(fileContent, "CaFile", RobotVDA5050ConfigRef.CaFile.Name);
}
if (RobotVDA5050ConfigRef.CertFile is not null)
{
var fileContent = new StreamContent(RobotVDA5050ConfigRef.CertFile.OpenReadStream(maxAllowedSize: RobotVDA5050ConfigRef.MaxFileSize));
content.Add(fileContent, "CertFile", RobotVDA5050ConfigRef.CertFile.Name);
}
if (RobotVDA5050ConfigRef.KeyFile is not null)
{
var fileContent = new StreamContent(RobotVDA5050ConfigRef.KeyFile.OpenReadStream(maxAllowedSize: RobotVDA5050ConfigRef.MaxFileSize));
content.Add(fileContent, "KeyFile", RobotVDA5050ConfigRef.KeyFile.Name);
}
var response = await (await Http.PostAsync($"api/File/certificates/{id}", content)).Content.ReadFromJsonAsync<MessageResult>();
if (response is null) Snackbar.Add("Failed to update certificates", Severity.Warning);
else if (!response.IsSuccess) Snackbar.Add(response.Message ?? "Failed to update certificates config", Severity.Warning);
else return true;
return false;
}
private async Task SaveConfig()
{
try
@@ -344,9 +372,14 @@ public partial class RobotConfigManager
SelectedVda.VDA5050PublishRepeat,
SelectedVda.VDA5050EnablePassword,
SelectedVda.VDA5050EnableTls,
SelectedVda.VDA5050CA,
SelectedVda.VDA5050Cer,
SelectedVda.VDA5050Key,
SelectedVda.Description
};
result = await (await Http.PutAsJsonAsync($"api/RobotConfigs/vda5050/{id}", updateDto)).Content.ReadFromJsonAsync<MessageResult>();
var saveCer = await SaveCertificates(SelectedVda.Id);
if (saveCer) result = await (await Http.PutAsJsonAsync($"api/RobotConfigs/vda5050/{id}", updateDto)).Content.ReadFromJsonAsync<MessageResult>();
else return;
break;
}