227 lines
7.3 KiB
Text
227 lines
7.3 KiB
Text
@using LANCommander.UI.Services
|
|
@using Microsoft.AspNetCore.Components.Forms
|
|
@inject UploadTracker UploadTracker
|
|
@inject IMessageService MessageService
|
|
@implements IDisposable
|
|
@namespace LANCommander.UI.Components
|
|
|
|
<div class="ant-upload ant-upload-select-text ant-upload-drag ant-upload-select chunk-uploader" data-status="@CurrentProgressStatus">
|
|
<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">
|
|
<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;" />
|
|
|
|
<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))">
|
|
@File.Name (<ByteSize Value="File.Size" />)
|
|
</span>
|
|
<span class="ant-upload-list-item-card-actions picture">
|
|
<Button Type="ButtonType.Text" Size="ButtonSize.Small" Icon="@IconType.Outline.Delete" OnClick="Clear" Disabled="@(CurrentProgressStatus != ProgressStatus.Normal)" />
|
|
</span>
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
[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; }
|
|
[Parameter] public string? Status { get; set; }
|
|
[Parameter] public EventCallback<string> StatusChanged { get; set; }
|
|
[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; }
|
|
|
|
/// <summary>
|
|
/// The type of upload for tracking purposes.
|
|
/// </summary>
|
|
[Parameter] public UploadType UploadType { get; set; } = UploadType.Archive;
|
|
|
|
readonly Guid _uploaderId = Guid.NewGuid();
|
|
|
|
InputFile? _fileInput;
|
|
string? _currentUploadId;
|
|
|
|
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()
|
|
{
|
|
UploadTracker.OnStateChanged += HandleUploadStateChanged;
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
_uploading = true;
|
|
|
|
await ChangeStatus("Uploading");
|
|
|
|
var fileName = File?.Name ?? "Unknown";
|
|
|
|
_currentUploadId = await UploadTracker.StartUploadAsync(
|
|
$"ChunkFileInput-{_uploaderId}",
|
|
StorageLocationId,
|
|
fileName,
|
|
UploadType,
|
|
(!string.IsNullOrWhiteSpace(ObjectKey) && ObjectKey != Guid.Empty.ToString()) ? ObjectKey : null,
|
|
onCompleted);
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
private void HandleUploadStateChanged()
|
|
{
|
|
if (_currentUploadId != null && UploadTracker.ActiveUploads.TryGetValue(_currentUploadId, out var info))
|
|
{
|
|
_progress = info.Percent;
|
|
_speed = info.Speed;
|
|
|
|
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.
|
|
}
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
InvokeAsync(StateHasChanged);
|
|
}
|
|
catch
|
|
{
|
|
// Component may be disposed; ignore.
|
|
}
|
|
}
|
|
|
|
public async Task Clear()
|
|
{
|
|
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))
|
|
{
|
|
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);
|
|
}
|
|
|
|
public string FileInputId => $"ChunkFileInput-{_uploaderId}";
|
|
|
|
public bool IsUploading => _uploading;
|
|
|
|
public void Dispose()
|
|
{
|
|
UploadTracker.OnStateChanged -= HandleUploadStateChanged;
|
|
}
|
|
}
|