LANCommander/LANCommander.UI/Components/ChunkUploader/ChunkUploader.razor

228 lines
7.3 KiB
Text
Raw Permalink Normal View History

2026-05-28 22:24:46 -05:00
@using LANCommander.UI.Services
2026-01-03 15:51:21 -06:00
@using Microsoft.AspNetCore.Components.Forms
2026-05-28 22:24:46 -05:00
@inject UploadTracker UploadTracker
@inject IMessageService MessageService
2026-05-28 22:24:46 -05:00
@implements IDisposable
2025-09-06 13:09:47 -05:00
@namespace LANCommander.UI.Components
<div class="ant-upload ant-upload-select-text ant-upload-drag ant-upload-select chunk-uploader" data-status="@CurrentProgressStatus">
2026-01-03 15:51:21 -06:00
<Progress Type="ProgressType.Circle" Percent="_progress" Status="@CurrentProgressStatus" Class="uploader-progress" />
<span class="uploader-progress-rate"></span>
<label tabindex="0" class="ant-upload" style="display: grid;" for="@($"ChunkFileInput-{_uploaderId}")" role="button">
2026-01-03 15:51:21 -06:00
<InputFile @ref="_fileInput" @key="@_uploaderId" id="@($"ChunkFileInput-{_uploaderId}")" OnChange="OnFileSelected" accept="@Accept" style="position: absolute; width: 100%; height: 100%; opacity: 0; top: 0;left: 0; z-index:2;" />
2026-05-28 22:24:46 -05:00
<div class="ant-upload-drag-container">
<p class="ant-upload-drag-icon">
<Icon Type="@IconType.Outline.Upload" />
</p>
<p class="ant-upload-text">
@Text
</p>
<p class="ant-upload-hint">
@Hint
</p>
</div>
</label>
</div>
@if (File != null)
{
<div class="ant-upload-list ant-upload-list-picture">
<div class="ant-upload-list-picture-container" style="display: list-item;">
<div class="ant-upload-list-item ant-upload-list-item-done ant-upload-list-item-list-type-picture">
<div class="ant-upload-list-item-info">
<span class="ant-upload-span">
<div class="ant-upload-list-item-thumbnail ant-upload-list-item-file">
<Icon Type="@IconType.Outline.FileZip" />
</div>
<span target="_blank" rel="noopener noreferrer" class="ant-upload-list-item-name" title="@File.Name (@ByteSizeLib.ByteSize.FromBytes(File.Size))">
2025-01-29 23:02:59 -06:00
@File.Name (<ByteSize Value="File.Size" />)
</span>
<span class="ant-upload-list-item-card-actions picture">
2024-12-31 18:21:41 -06:00
<Button Type="ButtonType.Text" Size="ButtonSize.Small" Icon="@IconType.Outline.Delete" OnClick="Clear" Disabled="@(CurrentProgressStatus != ProgressStatus.Normal)" />
</span>
</span>
</div>
</div>
</div>
</div>
}
@code {
2026-01-03 15:51:21 -06:00
[Parameter] public RenderFragment? Text { get; set; }
[Parameter] public RenderFragment? Hint { get; set; }
[Parameter] public string? Accept { get; set; }
[Parameter] public string? ObjectKey { get; set; }
[Parameter] public EventCallback<string> OnUploadCompleted { get; set; }
[Parameter] public EventCallback<string> OnUploadError { get; set; }
2026-01-03 15:51:21 -06:00
[Parameter] public string? Status { get; set; }
[Parameter] public EventCallback<string> StatusChanged { get; set; }
2026-01-03 15:51:21 -06:00
[Parameter] public IBrowserFile? File { get; set; }
[Parameter] public EventCallback<IBrowserFile> FileChanged { get; set; }
[Parameter] public Guid StorageLocationId { get; set; }
[Parameter] public EventCallback<Guid> StorageLocationIdChanged { get; set; }
2026-05-28 22:24:46 -05:00
/// <summary>
/// The type of upload for tracking purposes.
/// </summary>
[Parameter] public UploadType UploadType { get; set; } = UploadType.Archive;
2026-01-03 15:51:21 -06:00
readonly Guid _uploaderId = Guid.NewGuid();
2026-01-03 15:51:21 -06:00
InputFile? _fileInput;
2026-05-28 22:24:46 -05:00
string? _currentUploadId;
2026-01-03 15:51:21 -06:00
int _progress = 0;
bool _uploading = false;
bool _finished = false;
double _speed = 0;
ProgressStatus CurrentProgressStatus
{
get
{
switch (Status)
{
case "Complete":
return ProgressStatus.Success;
case "Uploading":
return ProgressStatus.Active;
default:
return ProgressStatus.Normal;
}
}
}
protected override void OnInitialized()
{
2026-05-28 22:24:46 -05:00
UploadTracker.OnStateChanged += HandleUploadStateChanged;
}
2026-01-03 15:51:21 -06:00
2026-05-28 22:24:46 -05:00
/// <summary>
/// Starts the upload. The onCompleted callback is invoked when the upload finishes
/// and survives component disposal.
/// </summary>
public async Task Start(Func<string, Task>? onCompleted = null)
{
2026-01-03 15:51:21 -06:00
_uploading = true;
await ChangeStatus("Uploading");
2026-05-28 22:24:46 -05:00
var fileName = File?.Name ?? "Unknown";
2026-05-28 22:24:46 -05:00
_currentUploadId = await UploadTracker.StartUploadAsync(
$"ChunkFileInput-{_uploaderId}",
StorageLocationId,
fileName,
UploadType,
(!string.IsNullOrWhiteSpace(ObjectKey) && ObjectKey != Guid.Empty.ToString()) ? ObjectKey : null,
onCompleted);
2026-05-28 22:24:46 -05:00
await InvokeAsync(StateHasChanged);
}
2026-05-28 22:24:46 -05:00
private void HandleUploadStateChanged()
{
if (_currentUploadId != null && UploadTracker.ActiveUploads.TryGetValue(_currentUploadId, out var info))
{
_progress = info.Percent;
_speed = info.Speed;
2026-05-28 22:24:46 -05:00
if (info.Status == UploadStatus.Complete)
{
_uploading = false;
_finished = true;
_ = ChangeStatus("Complete");
}
else if (info.Status == UploadStatus.Error)
{
_uploading = false;
_ = ChangeStatus("");
try
{
if (OnUploadError.HasDelegate)
_ = OnUploadError.InvokeAsync(info.ErrorMessage ?? "Upload failed");
}
catch
{
// Component may be disposed; ignore.
}
}
}
2026-05-28 22:24:46 -05:00
try
{
InvokeAsync(StateHasChanged);
}
catch
{
// Component may be disposed; ignore.
}
}
public async Task Clear()
{
2026-05-28 22:24:46 -05:00
if (_currentUploadId != null)
{
await UploadTracker.CancelUploadAsync(_currentUploadId);
_currentUploadId = null;
}
File = null;
if (FileChanged.HasDelegate)
await FileChanged.InvokeAsync(null);
Status = "";
if (StatusChanged.HasDelegate)
await StatusChanged.InvokeAsync(Status);
StateHasChanged();
}
async void OnFileSelected(InputFileChangeEventArgs args)
{
var file = args.File;
if (!String.IsNullOrWhiteSpace(Accept))
{
2026-01-03 15:51:21 -06:00
var acceptedFileExtensions = Accept.Split(',').Select(x => x.Trim()).ToArray();
if (!acceptedFileExtensions.Any(x => file.Name.ToLower().EndsWith(x.ToLower())))
{
MessageService.Error($"Only {String.Join(", ", acceptedFileExtensions)} can be selected!");
return;
}
}
File = file;
if (FileChanged.HasDelegate)
await FileChanged.InvokeAsync(File);
await ChangeStatus("");
}
async Task ChangeStatus(string status)
{
Status = status;
if (StatusChanged.HasDelegate)
await StatusChanged.InvokeAsync(Status);
}
2026-05-28 22:24:46 -05:00
public string FileInputId => $"ChunkFileInput-{_uploaderId}";
2026-05-28 22:24:46 -05:00
public bool IsUploading => _uploading;
2026-05-28 22:24:46 -05:00
public void Dispose()
{
2026-05-28 22:24:46 -05:00
UploadTracker.OnStateChanged -= HandleUploadStateChanged;
}
}