diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-11-23 16:30:21 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-23 16:30:21 -0600 |
| commit | 31694fc9b2cdf69413853ce2c362e4b5d3c47401 (patch) | |
| tree | 6be2eb4e82aee664f5d03e7f0645f6ccf55c76e4 /src/slskd/Relay | |
| parent | e6c12a2ffb9260db7236b5d79f474b1c69ef6bc9 (diff) | |
| parent | ec9ea5821f71d6c3643babc8483cc06ca6e52f75 (diff) | |
| download | slskd-31694fc9b2cdf69413853ce2c362e4b5d3c47401.tar.gz | |
Merge pull request #780 from slskd/relay-offset
Add support for resume-able uploads from Relay Agents
Diffstat (limited to 'src/slskd/Relay')
| -rw-r--r-- | src/slskd/Relay/API/Controllers/RelayController.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Relay/API/Hubs/RelayHub.cs | 3 | ||||
| -rw-r--r-- | src/slskd/Relay/RelayClient.cs | 7 | ||||
| -rw-r--r-- | src/slskd/Relay/RelayService.cs | 8 |
4 files changed, 14 insertions, 6 deletions
diff --git a/src/slskd/Relay/API/Controllers/RelayController.cs b/src/slskd/Relay/API/Controllers/RelayController.cs index d6f4f840..ae87969e 100644 --- a/src/slskd/Relay/API/Controllers/RelayController.cs +++ b/src/slskd/Relay/API/Controllers/RelayController.cs @@ -134,6 +134,8 @@ namespace slskd.Relay catch (Exception ex) { Log.Warning("Failed to handle file upload for token {Token} from a caller claiming to be agent {Agent}: {Message}", token, agentName, ex.Message); + Log.Debug(ex, "Failed to handle file upload"); + return BadRequest(); } diff --git a/src/slskd/Relay/API/Hubs/RelayHub.cs b/src/slskd/Relay/API/Hubs/RelayHub.cs index 885ba453..b76f67b9 100644 --- a/src/slskd/Relay/API/Hubs/RelayHub.cs +++ b/src/slskd/Relay/API/Hubs/RelayHub.cs @@ -50,9 +50,10 @@ namespace slskd.Relay /// Requests the specified <paramref name="filename"/> from the agent. /// </summary> /// <param name="filename">The name of the file.</param> + /// <param name="startOffset">The starting offset for the transfer.</param> /// <param name="id">The unique identifier for the request.</param> /// <returns>The operation context.</returns> - Task RequestFileUpload(string filename, Guid id); + Task RequestFileUpload(string filename, long startOffset, Guid id); } /// <summary> diff --git a/src/slskd/Relay/RelayClient.cs b/src/slskd/Relay/RelayClient.cs index 515d30ca..f1a1c51d 100644 --- a/src/slskd/Relay/RelayClient.cs +++ b/src/slskd/Relay/RelayClient.cs @@ -289,7 +289,7 @@ namespace slskd.Relay private string ComputeCredential(Guid token) => ComputeCredential(token.ToString()); - private Task HandleFileUploadRequest(string filename, Guid token) + private Task HandleFileUploadRequest(string filename, long startOffset, Guid token) { _ = Task.Run(async () => { @@ -308,6 +308,9 @@ namespace slskd.Relay } using var stream = new FileStream(localFileInfo.FullName, FileMode.Open, FileAccess.Read); + + stream.Seek(startOffset, SeekOrigin.Begin); + using var request = new HttpRequestMessage(HttpMethod.Post, $"api/v0/relay/files/{token}"); using var content = new MultipartFormDataContent { @@ -485,7 +488,7 @@ namespace slskd.Relay HubConnection.Reconnecting += HubConnection_Reconnecting; HubConnection.Closed += HubConnection_Closed; - HubConnection.On<string, Guid>(nameof(IRelayHub.RequestFileUpload), HandleFileUploadRequest); + HubConnection.On<string, long, Guid>(nameof(IRelayHub.RequestFileUpload), HandleFileUploadRequest); HubConnection.On<string, Guid>(nameof(IRelayHub.RequestFileInfo), HandleFileInfoRequest); HubConnection.On<string>(nameof(IRelayHub.Challenge), HandleAuthenticationChallenge); diff --git a/src/slskd/Relay/RelayService.cs b/src/slskd/Relay/RelayService.cs index 3b872816..c4fb13a9 100644 --- a/src/slskd/Relay/RelayService.cs +++ b/src/slskd/Relay/RelayService.cs @@ -143,11 +143,12 @@ namespace slskd.Relay /// </remarks> /// <param name="agentName">The agent from which to retrieve the file.</param> /// <param name="filename">The file to retrieve.</param> + /// <param name="startOffset">The starting offset for the transfer.</param> /// <param name="id">A unique ID for the stream.</param> /// <param name="timeout">An optional timeout value.</param> /// <param name="cancellationToken">An optional token to monitor for cancellation requests.</param> /// <returns>The operation context, including a stream containing the requested file.</returns> - Task<Stream> GetFileStreamAsync(string agentName, string filename, Guid id, int timeout = 3000, CancellationToken cancellationToken = default); + Task<Stream> GetFileStreamAsync(string agentName, string filename, long startOffset, Guid id, int timeout = 3000, CancellationToken cancellationToken = default); /// <summary> /// Handles the client response for a <see cref="GetFileInfoAsync"/> request. @@ -444,11 +445,12 @@ namespace slskd.Relay /// </remarks> /// <param name="agentName">The agent from which to retrieve the file.</param> /// <param name="filename">The file to retrieve.</param> + /// <param name="startOffset">The starting offset for the transfer.</param> /// <param name="id">A unique ID for the stream.</param> /// <param name="timeout">An optional timeout value.</param> /// <param name="cancellationToken">An optional token to monitor for cancellation requests.</param> /// <returns>The operation context, including a stream containing the requested file.</returns> - public async Task<Stream> GetFileStreamAsync(string agentName, string filename, Guid id, int timeout = 3000, CancellationToken cancellationToken = default) + public async Task<Stream> GetFileStreamAsync(string agentName, string filename, long startOffset, Guid id, int timeout = 3000, CancellationToken cancellationToken = default) { if (!RegisteredAgentDictionary.TryGetValue(agentName, out var record)) { @@ -468,7 +470,7 @@ namespace slskd.Relay Log.Information("Created wait {Key}", key); - await RelayHub.Clients.Client(record.ConnectionId).RequestFileUpload(filename, id); + await RelayHub.Clients.Client(record.ConnectionId).RequestFileUpload(filename, startOffset, id); Log.Information("Requested file {Filename} from Agent {Agent} with ID {Id}. Waiting for incoming connection.", filename, agentName, id); var task = await Task.WhenAny(wait, Task.Delay(timeout, cancellationToken)); |
