aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Relay/RelayClient.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2022-12-31 16:13:46 -0600
committerJP Dillingham <jp@dillingham.me>2022-12-31 16:13:46 -0600
commit614170cb68d52af7b9e627444f3758b1809190c1 (patch)
treeb4dff9daa5f901630dc66e802a90d40fbc8b132f /src/slskd/Relay/RelayClient.cs
parent6425c0032b2745ca2bd966f1668aba6415bb6f8f (diff)
downloadslskd-614170cb68d52af7b9e627444f3758b1809190c1.tar.gz
implement download logic
Diffstat (limited to 'src/slskd/Relay/RelayClient.cs')
-rw-r--r--src/slskd/Relay/RelayClient.cs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs
index f1a1c51d..cdc4f9e3 100644
--- a/src/slskd/Relay/RelayClient.cs
+++ b/src/slskd/Relay/RelayClient.cs
@@ -392,6 +392,50 @@ namespace slskd.Relay
}
}
+ private Task HandleNotifyFileDownloadCompleted(string filename, Guid token)
+ {
+ Log.Information("Relay controller sent a download notification for {Filename} ({Token})", filename, token);
+
+ _ = Task.Run(async () =>
+ {
+ var destinationFile = Path.Combine(OptionsMonitor.CurrentValue.Directories.Downloads, filename);
+
+ if (OptionsMonitor.CurrentValue.Relay.Mode.ToEnum<RelayMode>() == RelayMode.Debug)
+ {
+ // if we're debugging, we're referencing the same file for both the controller and agent
+ // which will lead to an access violation. prefix the destination file to avoid this.
+ destinationFile = Path.Combine(OptionsMonitor.CurrentValue.Directories.Downloads, $"_{filename}");
+ }
+
+ await Retry.Do(task: async () =>
+ {
+ using var request = new HttpRequestMessage(HttpMethod.Get, $"api/v0/relay/downloads/{token}");
+
+ request.Headers.Add("X-API-Key", OptionsMonitor.CurrentValue.Relay.Controller.ApiKey);
+ request.Headers.Add("X-Relay-Agent", OptionsMonitor.CurrentValue.InstanceName);
+ request.Headers.Add("X-Relay-Credential", ComputeCredential(token));
+ request.Headers.Add("X-Relay-Filename", filename);
+
+ using var client = CreateHttpClient();
+ using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
+
+ response.EnsureSuccessStatusCode();
+
+ using var remoteStream = await response.Content.ReadAsStreamAsync();
+ using var localStream = new FileStream(destinationFile, FileMode.Create);
+ await remoteStream.CopyToAsync(localStream);
+ },
+ isRetryable: (_, _) => true,
+ onFailure: (_, ex) => Log.Error(ex, "Failed to handle file download notification for {Filename} ({Token})", filename, token),
+ maxAttempts: 3,
+ maxDelayInMilliseconds: 60000);
+
+ Log.Information("File {Filename} successfully downloaded to {Destination}", filename, destinationFile);
+ });
+
+ return Task.CompletedTask;
+ }
+
private Task HubConnection_Closed(Exception arg)
{
Log.Warning("Relay controller connection closed: {Message}", arg.Message);
@@ -491,6 +535,7 @@ namespace slskd.Relay
HubConnection.On<string, long, Guid>(nameof(IRelayHub.RequestFileUpload), HandleFileUploadRequest);
HubConnection.On<string, Guid>(nameof(IRelayHub.RequestFileInfo), HandleFileInfoRequest);
HubConnection.On<string>(nameof(IRelayHub.Challenge), HandleAuthenticationChallenge);
+ HubConnection.On<string, Guid>(nameof(IRelayHub.NotifyFileDownloadCompleted), HandleNotifyFileDownloadCompleted);
LastOptionsHash = optionsHash;