Switch to upload using JS

This commit is contained in:
Pat Hartl 2023-04-07 19:11:09 -05:00
parent 9e4abc535b
commit 09211b6213

View file

@ -151,83 +151,30 @@
private async void FileSelected(InputFileChangeEventArgs args)
{
File = args.File;
//var buffer = new byte[File.Size];
//await File.OpenReadStream().ReadAsync(buffer);
}
private async Task UploadArchiveJS()
{
var key = (await JS.InvokeAsync<string>("Uploader.Upload", "FileInput"));
}
Uploading = true;
private async Task UploadArchive()
{
long uploadedBytes = 0;
long totalBytes = File.Size;
var response = (await JS.InvokeAsync<string>("Uploader.Upload", "FileInput"));
Watch = new Stopwatch();
using (var stream = File.OpenReadStream(long.MaxValue))
if (Guid.TryParse(response, out var objectKey))
{
Uploading = true;
Watch.Start();
while (Uploading)
{
byte[] chunk;
if (totalBytes - uploadedBytes < ChunkSize)
chunk = new byte[totalBytes - uploadedBytes];
else
chunk = new byte[ChunkSize];
int bytesRead = 0;
// This feels hacky, why do we need to do this?
// Only 32256 bytes of the file get read unless we
// loop through like this. Probably kills performance.
/*while (bytesRead < chunk.Length)
{
bytesRead += await stream.ReadAsync(chunk, bytesRead, chunk.Length - bytesRead);
}*/
using (FileStream fs = new FileStream(Path.Combine("Upload", Archive.Id.ToString()), FileMode.Append))
{
await stream.CopyToAsync(fs, ChunkSize);
//await fs.WriteAsync(chunk);
}
uploadedBytes += ChunkSize;
WatchBytesTransferred += ChunkSize;
Progress = (int)(uploadedBytes * 100 / totalBytes);
if (Watch.Elapsed.TotalSeconds >= 1)
{
Speed = WatchBytesTransferred * (1 / Watch.Elapsed.TotalSeconds);
WatchBytesTransferred = 0;
Watch.Restart();
}
if (Progress >= 100)
{
Watch.Stop();
Uploading = false;
await UploadComplete();
}
await InvokeAsync(StateHasChanged);
}
Uploading = false;
await UploadComplete(objectKey);
}
else
{
await MessageService.Error("Archive failed to upload!");
}
await InvokeAsync(StateHasChanged);
}
private async Task UploadComplete()
private async Task UploadComplete(Guid objectKey)
{
Archive.ObjectKey = Archive.Id.ToString();
Archive.ObjectKey = objectKey.ToString();
Archive.CompressedSize = File.Size;
await ArchiveService.Add(Archive);