From eec77d30a76c2ab4809aeb56f635903d81ef6355 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 18 Oct 2026 15:39:56 -0500 Subject: add Query() --- src/slskd/Transfers/Uploads/UploadService.cs | 33 +++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index bda47641..a298f523 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -827,21 +827,42 @@ namespace slskd.Transfers.Uploads public List List(Expression> expression, bool includeRemoved) { expression ??= t => true; + /// + /// Returns a list of all uploads matching the specified , projected to type . + /// + /// The Type of the returned projection. + /// The query expression. + /// A value indicating whether to include uploads that have been removed previously. + /// The list of uploads matching the specified query. + public List Query(Func, IQueryable> query, bool includeRemoved = false) + { + ArgumentNullException.ThrowIfNull(query); try { using var context = ContextFactory.CreateDbContext(); - return context.Transfers + var transfers = context.Transfers .AsNoTracking() - .Where(t => t.Direction == TransferDirection.Upload) - .Where(t => !t.Removed || includeRemoved) - .Where(expression) - .ToList(); + .Where(t => t.Direction == TransferDirection.Upload); + + if (!includeRemoved) + { + transfers = transfers.Where(t => !t.Removed); + } + + var result = query(transfers); + + if (result.Provider is not IAsyncQueryProvider) + { + throw new InvalidOperationException("Query must be fully translatable to SQL; in-memory evaluation is not allowed (remove any AsEnumerable() present in the query)"); + } + + return result.ToList(); } catch (Exception ex) { - Log.Error(ex, "Failed to list uploads: {Message}", ex.Message); + Log.Error(ex, "Failed to execute upload queury: {Message}", ex.Message); throw; } } -- cgit v1.2.3 From ab1c7066f7b8b7ad55082b27a40c36501b923aa1 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 18 Oct 2026 15:40:41 -0500 Subject: add Query() to interface --- src/slskd/Transfers/Uploads/UploadService.cs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index a298f523..4361ece1 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -126,6 +126,15 @@ namespace slskd.Transfers.Uploads /// The number of pruned uploads. int Prune(int age, params TransferStates[] states); + /// + /// Returns a list of all uploads matching the specified , projected to type . + /// + /// The Type of the returned projection. + /// The query expression. + /// A value indicating whether to include uploads that have been removed previously. + /// The list of uploads matching the specified query. + List Query(Func, IQueryable> query, bool includeRemoved = false); + /// /// Removes the completed upload matching the specified . /// -- cgit v1.2.3 From 075f690419fc4fa0c706dfbdf1797e35a02f7a7e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 18 Oct 2026 15:41:58 -0500 Subject: update Find() to accept only an id (remove footgun) --- .../API/Controllers/TransfersController.cs | 2 +- src/slskd/Transfers/Uploads/UploadService.cs | 24 ++++++++-------------- 2 files changed, 10 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/API/Controllers/TransfersController.cs b/src/slskd/Transfers/API/Controllers/TransfersController.cs index 83eb04ef..3dd2f124 100644 --- a/src/slskd/Transfers/API/Controllers/TransfersController.cs +++ b/src/slskd/Transfers/API/Controllers/TransfersController.cs @@ -709,7 +709,7 @@ namespace slskd.Transfers.API return BadRequest(); } - var upload = Transfers.Uploads.Find(t => t.Id == guid); + var upload = Transfers.Uploads.Find(guid); if (upload == default) { diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 4361ece1..c72c96ee 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -96,11 +96,11 @@ namespace slskd.Transfers.Uploads Task UploadAsync(Transfer transfer, CancellationToken cancellationToken = default); /// - /// Finds a single upload matching the specified . + /// Finds a single upload matching the specified . /// - /// The expression to use to match uploads. + /// The unique identifier of the upload. /// The found transfer, or default if not found. - Transfer Find(Expression> expression); + Transfer Find(Guid id); /// /// Gets upload statistics for the specified . @@ -306,7 +306,7 @@ namespace slskd.Transfers.Uploads we're not using DbContext and tracked changes here because we don't want to hold a connection open for the duration of the upload */ - transfer = Find(t => t.Id == transfer.Id) + transfer = Find(transfer.Id) ?? throw new TransferNotFoundException($"Transfer with ID {transfer.Id} not found"); if (transfer.State != (TransferStates.Queued | TransferStates.Locally)) @@ -804,21 +804,15 @@ namespace slskd.Transfers.Uploads } /// - /// Finds a single upload matching the specified . + /// Finds a single upload matching the specified . /// - /// The expression to use to match uploads. + /// The unique identifier of the upload. /// The found transfer, or default if not found. - public Transfer Find(Expression> expression) + public Transfer Find(Guid id) { try { - using var context = ContextFactory.CreateDbContext(); - - return context.Transfers - .AsNoTracking() - .Where(t => t.Direction == TransferDirection.Upload) - .Where(expression) - .SingleOrDefault(); + return Query(q => q.Where(t => t.Id == id), includeRemoved: true).SingleOrDefault(); } catch (Exception ex) { @@ -1111,7 +1105,7 @@ namespace slskd.Transfers.Uploads private bool TryFail(Guid id, string exception, TransferStates state) { - var t = Find(t => t.Id == id); + var t = Find(id); if (t is null) { -- cgit v1.2.3 From c5c2597ef63a02f59d6f1872c3bed4f7e9336c0f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 18 Oct 2026 15:42:46 -0500 Subject: use Query() within List() --- src/slskd/Transfers/Uploads/UploadService.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index c72c96ee..35bb9468 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -45,6 +45,7 @@ namespace slskd.Transfers.Uploads using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; + using Microsoft.EntityFrameworkCore.Query; using Serilog; using slskd.Events; using slskd.Relay; @@ -111,9 +112,9 @@ namespace slskd.Transfers.Uploads UserUploadStatistics GetUserStatistics(string username, DateTime? utcNow = null); /// - /// Returns a list of all uploads matching the optional . + /// Returns a list of all uploads matching the specified . /// - /// An optional expression used to match uploads. + /// An expression used to match uploads. /// A value indicating whether to include uploads that have been removed previously. /// The list of uploads matching the specified expression, or all uploads if no expression is specified. List List(Expression> expression, bool includeRemoved); @@ -822,14 +823,26 @@ namespace slskd.Transfers.Uploads } /// - /// Returns a list of all uploads matching the optional . + /// Returns a list of all uploads matching the specified . /// - /// An optional expression used to match uploads. + /// An expression used to match uploads. /// A value indicating whether to include uploads that have been removed previously. /// The list of uploads matching the specified expression, or all uploads if no expression is specified. public List List(Expression> expression, bool includeRemoved) { expression ??= t => true; + + try + { + return Query(q => q.Where(expression), includeRemoved); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to list uploads: {Message}", ex.Message); + throw; + } + } + /// /// Returns a list of all uploads matching the specified , projected to type . /// -- cgit v1.2.3 From 9f5cbe1ee6321f41ba6bb4d4fe9ac50ae1a95aa4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 18 Oct 2026 15:43:21 -0500 Subject: use Query() to find transfer for estimation --- src/slskd/Application.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 51289970..e055fcd8 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1445,9 +1445,16 @@ namespace slskd try { - var upload = Transfers.Uploads.Find(u => u.Username == username && u.Filename == filename); - - if (upload is null) // transfer doesn't exist at all + var upload = Transfers.Uploads.Query(q => + q + .Where(t => t.Username == username && t.Filename == filename && t.EnqueuedAt != null) + .OrderByDescending(t => t.EnqueuedAt) + .Take(1), + includeRemoved: false) + .FirstOrDefault(); + + // transfer might not be in the queue, or might not exist at all + if (upload is null) { return Task.FromResult(null); } -- cgit v1.2.3