diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-10-18 17:58:53 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-10-18 17:58:53 -0500 |
| commit | c4a7d11ea63a8dbb1faa0e27a65a244f2420d72b (patch) | |
| tree | 135fccbedf2bab93d2695a3ebb2b01ca5cf7bdd1 /src | |
| parent | 46eea44db6c9e3a142f808b4aae86a31fbc51b4b (diff) | |
| parent | 90a0d24a84a132a998b937b6a60de945caa267cc (diff) | |
| download | slskd-c4a7d11ea63a8dbb1faa0e27a65a244f2420d72b.tar.gz | |
Merge branch 'master' into mutex
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 13 | ||||
| -rw-r--r-- | src/slskd/Transfers/API/Controllers/TransfersController.cs | 2 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 87 |
3 files changed, 73 insertions, 29 deletions
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<int?>(null); } 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 bda47641..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; @@ -96,11 +97,11 @@ namespace slskd.Transfers.Uploads Task<Transfer> UploadAsync(Transfer transfer, CancellationToken cancellationToken = default); /// <summary> - /// Finds a single upload matching the specified <paramref name="expression"/>. + /// Finds a single upload matching the specified <paramref name="id"/>. /// </summary> - /// <param name="expression">The expression to use to match uploads.</param> + /// <param name="id">The unique identifier of the upload.</param> /// <returns>The found transfer, or default if not found.</returns> - Transfer Find(Expression<Func<Transfer, bool>> expression); + Transfer Find(Guid id); /// <summary> /// Gets upload statistics for the specified <paramref name="username"/>. @@ -111,9 +112,9 @@ namespace slskd.Transfers.Uploads UserUploadStatistics GetUserStatistics(string username, DateTime? utcNow = null); /// <summary> - /// Returns a list of all uploads matching the optional <paramref name="expression"/>. + /// Returns a list of all uploads matching the specified <paramref name="expression"/>. /// </summary> - /// <param name="expression">An optional expression used to match uploads.</param> + /// <param name="expression">An expression used to match uploads.</param> /// <param name="includeRemoved">A value indicating whether to include uploads that have been removed previously.</param> /// <returns>The list of uploads matching the specified expression, or all uploads if no expression is specified.</returns> List<Transfer> List(Expression<Func<Transfer, bool>> expression, bool includeRemoved); @@ -127,6 +128,15 @@ namespace slskd.Transfers.Uploads int Prune(int age, params TransferStates[] states); /// <summary> + /// Returns a list of all uploads matching the specified <paramref name="query"/>, projected to type <typeparamref name="TResult"/>. + /// </summary> + /// <typeparam name="TResult">The Type of the returned projection.</typeparam> + /// <param name="query">The query expression.</param> + /// <param name="includeRemoved">A value indicating whether to include uploads that have been removed previously.</param> + /// <returns>The list of uploads matching the specified query.</returns> + List<TResult> Query<TResult>(Func<IQueryable<Transfer>, IQueryable<TResult>> query, bool includeRemoved = false); + + /// <summary> /// Removes the completed upload matching the specified <paramref name="id"/>. /// </summary> /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks> @@ -297,7 +307,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)) @@ -795,21 +805,15 @@ namespace slskd.Transfers.Uploads } /// <summary> - /// Finds a single upload matching the specified <paramref name="expression"/>. + /// Finds a single upload matching the specified <paramref name="id"/>. /// </summary> - /// <param name="expression">The expression to use to match uploads.</param> + /// <param name="id">The unique identifier of the upload.</param> /// <returns>The found transfer, or default if not found.</returns> - public Transfer Find(Expression<Func<Transfer, bool>> 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) { @@ -819,9 +823,9 @@ namespace slskd.Transfers.Uploads } /// <summary> - /// Returns a list of all uploads matching the optional <paramref name="expression"/>. + /// Returns a list of all uploads matching the specified <paramref name="expression"/>. /// </summary> - /// <param name="expression">An optional expression used to match uploads.</param> + /// <param name="expression">An expression used to match uploads.</param> /// <param name="includeRemoved">A value indicating whether to include uploads that have been removed previously.</param> /// <returns>The list of uploads matching the specified expression, or all uploads if no expression is specified.</returns> public List<Transfer> List(Expression<Func<Transfer, bool>> expression, bool includeRemoved) @@ -830,18 +834,51 @@ namespace slskd.Transfers.Uploads try { + return Query(q => q.Where(expression), includeRemoved); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to list uploads: {Message}", ex.Message); + throw; + } + } + + /// <summary> + /// Returns a list of all uploads matching the specified <paramref name="query"/>, projected to type <typeparamref name="TResult"/>. + /// </summary> + /// <typeparam name="TResult">The Type of the returned projection.</typeparam> + /// <param name="query">The query expression.</param> + /// <param name="includeRemoved">A value indicating whether to include uploads that have been removed previously.</param> + /// <returns>The list of uploads matching the specified query.</returns> + public List<TResult> Query<TResult>(Func<IQueryable<Transfer>, IQueryable<TResult>> 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; } } @@ -1081,7 +1118,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) { |
