From 89b6bdc087db0fc442492fcc52d0ca7d68e415aa Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 3 May 2026 18:48:24 -0500 Subject: rough in batch endpoints --- .../API/Controllers/TransfersController.cs | 173 +++++++++++++-------- 1 file changed, 112 insertions(+), 61 deletions(-) (limited to 'src/slskd/Transfers/API/Controllers/TransfersController.cs') diff --git a/src/slskd/Transfers/API/Controllers/TransfersController.cs b/src/slskd/Transfers/API/Controllers/TransfersController.cs index 9f2fba6d..2e42c6cf 100644 --- a/src/slskd/Transfers/API/Controllers/TransfersController.cs +++ b/src/slskd/Transfers/API/Controllers/TransfersController.cs @@ -216,68 +216,8 @@ namespace slskd.Transfers.API } } - // [HttpPost("downloads")] - // [Authorize(Policy = AuthPolicy.Any)] - // [ProducesResponseType(201)] - // [ProducesResponseType(typeof(string), 403)] - // [ProducesResponseType(typeof(string), 500)] - // public async Task EnqueueBatchAsync([FromBody] EnqueueDownloadBatchRequest request) - // { - // if (Program.IsRelayAgent) - // { - // return Forbid(); - // } - - // // todo, check: username null or whitespace - // if (!ModelState.IsValid) - // { - // return BadRequest(ModelState.GetReadableString()); - // } - - // if ((request.Files?.Count ?? 0) == 0) - // { - // return BadRequest("At least one file is required"); - // } - - // // todo, check: - // // * filename null or whitespace - // // * size -1 - // if (request.Files.Any(r => r is null)) - // { - // return BadRequest("One or more files in the request are invalid"); - // } - - // if (!DownloadRequestLimiter.Wait(0)) - // { - // return StatusCode(429, "Only one concurrent operation is permitted. Wait until the previous request completes"); - // } - - // try - // { - // var endpoint = await Users.GetIPEndPointAsync(request.Username); - - // if (Users.IsBlacklisted(request.Username, endpoint.Address)) - // { - // throw new UserOfflineException($"User {request.Username} appears to be offline"); - // } - - // var (enqueued, failed) = await Transfers.Downloads.EnqueueAsync(request.Username, requests.Select(r => (r.Filename, r.Size))); - - // return StatusCode(201, new { Enqueued = enqueued, Failed = failed }); - // } - // catch (Exception ex) - // { - // Log.Error(ex, "Failed to enqueue {Count} files for {Username}: {Message}", requests.Count(), username, ex.Message); - // return StatusCode(500, ex.Message); - // } - // finally - // { - // DownloadRequestLimiter.Release(); - // } - // } - /// - /// Enqueues the specified download. + /// (Obsolete) Enqueues the specified download. /// /// The username of the download source. /// The list of download requests. @@ -285,6 +225,7 @@ namespace slskd.Transfers.API /// The download was successfully enqueued. /// The download was rejected. /// An unexpected error was encountered. + [Obsolete("Will be phased out in future versions; use batches")] [HttpPost("downloads/{username}")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(201)] @@ -341,6 +282,75 @@ namespace slskd.Transfers.API } } + [HttpPost("downloads/batches")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(201)] + [ProducesResponseType(typeof(string), 403)] + [ProducesResponseType(typeof(string), 500)] + public async Task EnqueueBatchAsync([FromBody] EnqueueDownloadBatchRequest request) + { + if (Program.IsRelayAgent) + { + return Forbid(); + } + + if (!ModelState.IsValid) + { + return BadRequest(ModelState.GetReadableString()); + } + + if ((request.Files?.Count ?? 0) == 0) + { + return BadRequest("At least one file is required"); + } + + if (request.Files.Any( + r => r is null + || string.IsNullOrWhiteSpace(r.Filename) + || r.Size < 0)) + { + return BadRequest("One or more files in the request are invalid"); + } + + if (!DownloadRequestLimiter.Wait(0)) + { + return StatusCode(429, "Only one concurrent operation is permitted. Wait until the previous request completes"); + } + + try + { + var endpoint = await Users.GetIPEndPointAsync(request.Username); + + if (Users.IsBlacklisted(request.Username, endpoint.Address)) + { + throw new UserOfflineException($"User {request.Username} appears to be offline"); + } + + // todo: finish this + // await Transfers.Downloads.Batches.Create(new() + // { + // Id = request.BatchId, + // SearchId = request.SearchId, + // Options = request.Options, + // }); + + var (enqueued, failed) = await Transfers.Downloads.EnqueueAsync(request.Username, request.Files.Select(r => (r.Filename, r.Size))); + + var batch = await Transfers.Downloads.Batches.FindAsync(b => b.Id == request.BatchId); + + return StatusCode(201, batch); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to enqueue {Count} files for {Username}: {Message}", request.Files.Count(), request.Username, ex.Message); + return StatusCode(500, ex.Message); + } + finally + { + DownloadRequestLimiter.Release(); + } + } + /// /// Gets all downloads. /// @@ -372,6 +382,47 @@ namespace slskd.Transfers.API return Ok(response); } + /// + /// Gets the specified batch and associated transfers. + /// + /// The id of the batch. + /// + /// The specified id is not valid. + /// The request completed successfully. + /// The specified batch was not found. + [HttpGet("downloads/batches/{id}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(200)] + public async Task Get([FromRoute, Required] string id) + { + if (Program.IsRelayAgent) + { + return Forbid(); + } + + if (!Guid.TryParse(id, out var guid)) + { + return BadRequest(); + } + + try + { + var found = await Transfers.Downloads.Batches.FindAsync(b => b.Id == guid); + + if (found is null) + { + return NotFound(); + } + + return Ok(found); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to get batch with ID {Id}: {Message}", guid, ex.Message); + return StatusCode(500, ex.Message); + } + } + /// /// Gets all downloads for the specified username. /// -- cgit v1.2.3