aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-10-23 13:55:15 -0500
committerJP Dillingham <jp@dillingham.me>2022-10-23 13:55:15 -0500
commit92fab4ce25ad8b23fc14f946490d05d8d1da9c4c (patch)
tree91b7e61dd2cd1f2940a6f4d44826f51ad9ec7938 /src
parent8d0c182748158a5a954ad9efc1f237c63eafb681 (diff)
downloadslskd-92fab4ce25ad8b23fc14f946490d05d8d1da9c4c.tar.gz
run uploads as a separate task
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Network/NetworkClient.cs71
1 files changed, 38 insertions, 33 deletions
diff --git a/src/slskd/Network/NetworkClient.cs b/src/slskd/Network/NetworkClient.cs
index 6a2a374d..5d8ab100 100644
--- a/src/slskd/Network/NetworkClient.cs
+++ b/src/slskd/Network/NetworkClient.cs
@@ -165,51 +165,56 @@ namespace slskd.Network
return Aes.Encrypt(tokenBytes, key).ToBase62();
}
- private async Task HandleFileRequest(string filename, Guid token)
+ private Task HandleFileRequest(string filename, Guid token)
{
- Log.Information("Network controller requested file {Filename} with ID {Id}", filename, token);
-
- try
+ _ = Task.Run(async () =>
{
- var (_, localFilename) = await Shares.ResolveFileAsync(filename);
-
- var localFileInfo = new FileInfo(localFilename);
+ Log.Information("Network controller requested file {Filename} with ID {Id}", filename, token);
- if (!localFileInfo.Exists)
+ try
{
- Shares.RequestScan();
- throw new NotFoundException($"The file '{localFilename}' could not be located on disk. A share scan should be performed.");
- }
+ var (_, localFilename) = await Shares.ResolveFileAsync(filename);
- using var stream = new FileStream(localFileInfo.FullName, FileMode.Open, FileAccess.Read);
- using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/network/files/{OptionsMonitor.CurrentValue.InstanceName}/{token}");
- using var content = new MultipartFormDataContent
- {
- { new StringContent(ComputeCredential(token)), "credential" },
- { new StreamContent(stream), "file", filename },
- };
+ var localFileInfo = new FileInfo(localFilename);
- request.Content = content;
+ if (!localFileInfo.Exists)
+ {
+ Shares.RequestScan();
+ throw new NotFoundException($"The file '{localFilename}' could not be located on disk. A share scan should be performed.");
+ }
- Log.Information("Beginning upload of file {Filename} with ID {Id}", filename, token);
- var response = await CreateHttpClient().SendAsync(request);
+ using var stream = new FileStream(localFileInfo.FullName, FileMode.Open, FileAccess.Read);
+ using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/network/files/{OptionsMonitor.CurrentValue.InstanceName}/{token}");
+ using var content = new MultipartFormDataContent
+ {
+ { new StringContent(ComputeCredential(token)), "credential" },
+ { new StreamContent(stream), "file", filename },
+ };
- if (!response.IsSuccessStatusCode)
- {
- Log.Error("Upload of file {Filename} with ID {Id} failed: {StatusCode}", response.StatusCode);
+ request.Content = content;
+
+ Log.Information("Beginning upload of file {Filename} with ID {Id}", filename, token);
+ var response = await CreateHttpClient().SendAsync(request);
+
+ if (!response.IsSuccessStatusCode)
+ {
+ Log.Error("Upload of file {Filename} with ID {Id} failed: {StatusCode}", response.StatusCode);
+ }
+ else
+ {
+ Log.Information("Upload of file {Filename} with ID {Id} succeeded.", filename);
+ }
}
- else
+ catch (Exception ex)
{
- Log.Information("Upload of file {Filename} with ID {Id} succeeded.", filename);
+ Log.Error(ex, "Failed to handle file request: {Message}", ex.Message);
+
+ // report the failure to the controller. this avoids a failure due to timeout.
+ await HubConnection.InvokeAsync(nameof(NetworkHub.NotifyUploadFailed), token);
}
- }
- catch (Exception ex)
- {
- Log.Error(ex, "Failed to handle file request: {Message}", ex.Message);
+ });
- // report the failure to the controller. this avoids a failure due to timeout.
- await HubConnection.InvokeAsync(nameof(NetworkHub.NotifyUploadFailed), token);
- }
+ return Task.CompletedTask;
}
private async Task HandleFileInfoRequest(string filename, Guid id)