MeowStash/Pages/Index.razor

115 lines
3.6 KiB
Text
Raw Permalink Normal View History

2023-01-03 23:39:51 +01:00
@page "/"
<PageTitle>MeowStash</PageTitle>
<h1>MeowStash</h1>
<p>A meowmeow host for stashing random files~</p>
2023-01-08 14:54:50 +01:00
<InputFile OnChange="@SendFiles" multiple /><br />
2023-03-22 11:32:04 +01:00
@if (_uploadData.Any())
{
<div class="uploads">
@foreach (var item in _uploadData)
{
<div class="upload-item">
<div class="upload-item-data">
<span class="upload-item-name">@item.Key.Name</span>
</div>
<div class="upload-progress">
<span class="upload-progress-value @GetUploadProgressClassesFor(item.Value.Item1)">@item.Value.Item1%</span>
<span class="upload-progress-bytes">@item.Value.Item2</span>
<div class="upload-progress-bar-container">
<div class="upload-progress-bar @GetUploadProgressClassesFor(item.Value.Item1)"
style="width: @($"{item.Value.Item1}%");"></div>
</div>
</div>
</div>
}
</div>
}
2023-01-08 14:54:50 +01:00
@code {
2023-03-22 11:32:04 +01:00
private string GetUploadProgressClassesFor(float percent)
{
return percent switch
{
(< 5f) => "upload-progress-low",
(> 5f) and (< 50f) => "upload-progress-submedial",
(> 50f) and (< 95f) => "upload-progress-supermedial",
(> 95f) and (< 100f) => "upload-progress-nearly-done",
100f or (> 100f) => "upload-progress-done",
_ => "upload-progress-invalid"
};
}
private System.Collections.Concurrent.ConcurrentDictionary<IBrowserFile, (float, long)> _uploadData = new();
private CancellationTokenSource _cancel = null!;
2023-01-08 14:54:50 +01:00
private async Task SendFiles(InputFileChangeEventArgs e)
{
2023-03-22 11:32:04 +01:00
if (_cancel != null)
{
_cancel.Cancel();
_cancel.Dispose();
}
_cancel = new();
2023-01-08 14:54:50 +01:00
var files = e.GetMultipleFiles(e.FileCount);
2023-03-22 11:32:04 +01:00
_uploadData = new(files.Count, files.ToDictionary(f => f, _ => (0f, 0L)), null);
if (!System.IO.Directory.Exists("/tmp/uploads"))
{
System.IO.Directory.CreateDirectory("/tmp/uploads");
}
var tasks = new List<Task>();
2023-01-08 14:54:50 +01:00
foreach (var file in files)
{
2023-03-22 11:32:04 +01:00
tasks.Add(Task.Run(async () =>
{
2023-03-22 11:32:04 +01:00
await using var fos = System.IO.File.OpenWrite($"/tmp/uploads/{file.Name}");
var stream = file.OpenReadStream(1024 * 1024 * 1024, _cancel.Token);
int read;
byte[] buffer = new byte[16 * 1024];
while ((read = await stream.ReadAsync(buffer, 0, buffer.Length, _cancel.Token)) > 0)
{
if (_cancel.IsCancellationRequested)
{
stream.Close();
return;
}
await fos.WriteAsync(buffer, 0, read);
long totalRead = _uploadData[file].Item2;
totalRead += read;
float percent = float.Round(((float)totalRead / (float)file.Size * 100f), 2);
_uploadData[file] = (percent, totalRead);
await InvokeAsync(StateHasChanged);
2023-06-21 19:59:56 +02:00
await Task.Delay(10, _cancel.Token);
2023-03-22 11:32:04 +01:00
}
stream.Close();
Console.WriteLine($"File: /tmp/uploads/{file.Name} written...");
await InvokeAsync(StateHasChanged);
}));
2023-01-08 14:54:50 +01:00
}
2023-03-22 11:32:04 +01:00
await Task.WhenAll(tasks);
2023-01-08 14:54:50 +01:00
StateHasChanged();
}
}