diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-07-05 17:51:23 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-05 17:51:23 -0500 |
| commit | 8551ea1930b6639007d18fdca35bfb4073b16914 (patch) | |
| tree | 2430f701f8359e602f4c16847331c588cb473cca /src/slskd/Transfers | |
| parent | 8ab0cda52fda8b7e4f0353065c6f0dc5caeeb1f1 (diff) | |
| parent | 4914c3832358218d28f977baed2ddfb1e7e92723 (diff) | |
| download | slskd-8551ea1930b6639007d18fdca35bfb4073b16914.tar.gz | |
Merge branch 'master' into dependabot/npm_and_yarn/src/web/http-proxy-middleware-2.0.9
Diffstat (limited to 'src/slskd/Transfers')
| -rw-r--r-- | src/slskd/Transfers/API/Controllers/TransfersController.cs | 7 | ||||
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 16 | ||||
| -rw-r--r-- | src/slskd/Transfers/TransfersDbContext.cs | 19 | ||||
| -rw-r--r-- | src/slskd/Transfers/Types/Transfer.cs | 12 | ||||
| -rw-r--r-- | src/slskd/Transfers/Uploads/UploadService.cs | 22 |
5 files changed, 44 insertions, 32 deletions
diff --git a/src/slskd/Transfers/API/Controllers/TransfersController.cs b/src/slskd/Transfers/API/Controllers/TransfersController.cs index f143e4a7..fdbca1dc 100644 --- a/src/slskd/Transfers/API/Controllers/TransfersController.cs +++ b/src/slskd/Transfers/API/Controllers/TransfersController.cs @@ -111,9 +111,7 @@ namespace slskd.Transfers.API return Forbid(); } - var transfers = Transfers.Downloads - .List() // https://github.com/dotnet/efcore/issues/10434 - .Where(t => t.State.HasFlag(Soulseek.TransferStates.Completed)); + var transfers = Transfers.Downloads.List(t => t.State.HasFlag(Soulseek.TransferStates.Completed)); foreach (var id in transfers.Select(t => t.Id)) { @@ -182,8 +180,7 @@ namespace slskd.Transfers.API // get all the transfers that aren't removed var transfers = Transfers.Uploads - .List(t => true, includeRemoved: false) // https://github.com/dotnet/efcore/issues/10434 - .Where(t => t.State.HasFlag(Soulseek.TransferStates.Completed)); + .List(t => t.State.HasFlag(Soulseek.TransferStates.Completed), includeRemoved: false); foreach (var id in transfers.Select(t => t.Id)) { diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 37df355c..63b04394 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -87,9 +87,9 @@ namespace slskd.Transfers.Downloads /// Removes <see cref="TransferStates.Completed"/> downloads older than the specified <paramref name="age"/>. /// </summary> /// <param name="age">The age after which downloads are eligible for pruning, in hours.</param> - /// <param name="state">An optional, additional state by which downloads are filtered for pruning.</param> + /// <param name="stateHasFlag">An optional, additional state by which downloads are filtered for pruning.</param> /// <returns>The number of pruned downloads.</returns> - int Prune(int age, TransferStates state = TransferStates.Completed); + int Prune(int age, TransferStates stateHasFlag = TransferStates.Completed); /// <summary> /// Removes the download matching the specified <paramref name="id"/>. @@ -531,13 +531,13 @@ namespace slskd.Transfers.Downloads /// Removes <see cref="TransferStates.Completed"/> downloads older than the specified <paramref name="age"/>. /// </summary> /// <param name="age">The age after which downloads are eligible for pruning, in hours.</param> - /// <param name="state">An optional, additional state by which downloads are filtered for pruning.</param> + /// <param name="stateHasFlag">An optional, additional state by which downloads are filtered for pruning.</param> /// <returns>The number of pruned downloads.</returns> - public int Prune(int age, TransferStates state = TransferStates.Completed) + public int Prune(int age, TransferStates stateHasFlag = TransferStates.Completed) { - if (!state.HasFlag(TransferStates.Completed)) + if (!stateHasFlag.HasFlag(TransferStates.Completed)) { - throw new ArgumentException($"State must include {TransferStates.Completed}", nameof(state)); + throw new ArgumentException($"State must include {TransferStates.Completed}", nameof(stateHasFlag)); } try @@ -550,7 +550,7 @@ namespace slskd.Transfers.Downloads .Where(t => t.Direction == TransferDirection.Download) .Where(t => !t.Removed) .Where(t => t.EndedAt.HasValue && t.EndedAt.Value < cutoffDateTime) - .Where(t => t.State == state) // https://github.com/dotnet/efcore/issues/20094 + .Where(t => t.State == stateHasFlag) .ToList(); foreach (var tx in expired) @@ -562,7 +562,7 @@ namespace slskd.Transfers.Downloads if (pruned > 0) { - Log.Debug("Pruned {Count} expired downloads with state {State}", pruned, state); + Log.Debug("Pruned {Count} expired downloads with state {State}", pruned, stateHasFlag); } return pruned; diff --git a/src/slskd/Transfers/TransfersDbContext.cs b/src/slskd/Transfers/TransfersDbContext.cs index f31b6da8..cd721539 100644 --- a/src/slskd/Transfers/TransfersDbContext.cs +++ b/src/slskd/Transfers/TransfersDbContext.cs @@ -31,6 +31,20 @@ namespace slskd.Transfers public DbSet<Transfer> Transfers { get; set; } + public override int SaveChanges(bool acceptAllChangesOnSuccess) + { + // this is absolutely NOT IDEAL and will accellerate the move away from EF + foreach (var entry in ChangeTracker.Entries<Transfer>()) + { + if (entry.State == EntityState.Added || entry.State == EntityState.Modified) + { + entry.Entity.StateDescription = entry.Entity.State.ToString(); + } + } + + return base.SaveChanges(acceptAllChangesOnSuccess); + } + protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder @@ -47,11 +61,6 @@ namespace slskd.Transfers .Entity<Transfer>() .Property(d => d.Direction) .HasConversion(new EnumToStringConverter<Soulseek.TransferDirection>()); - - modelBuilder - .Entity<Transfer>() - .Property(d => d.State) - .HasConversion(new EnumToStringConverter<Soulseek.TransferStates>()); } } } diff --git a/src/slskd/Transfers/Types/Transfer.cs b/src/slskd/Transfers/Types/Transfer.cs index ddb3e017..338aba38 100644 --- a/src/slskd/Transfers/Types/Transfer.cs +++ b/src/slskd/Transfers/Types/Transfer.cs @@ -37,7 +37,17 @@ namespace slskd.Transfers public long Size { get; set; } public long StartOffset { get; init; } - public TransferStates State { get; set; } + public TransferStates State { get; set; } = TransferStates.None; + + /// <summary> + /// Gets the string representation of the transfer <see cref="State"/>. + /// </summary> + /// <remarks> + /// This is a hack to get the string into the database. *DO NOT* use this property in code + /// and especially **DO NOT** set the value. The getter and setter can't be protected because + /// EF Core needs them to be public. + /// </remarks> + public string StateDescription { get; set; } public DateTime RequestedAt { get; set; } public DateTime? EnqueuedAt { get; set; } public DateTime? StartedAt { get; set; } diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index dff1252b..7e96533e 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -95,9 +95,9 @@ namespace slskd.Transfers.Uploads /// Removes <see cref="TransferStates.Completed"/> uploads older than the specified <paramref name="age"/>. /// </summary> /// <param name="age">The age after which uploads are eligible for pruning, in minutes.</param> - /// <param name="state">An optional, additional state by which uploads are filtered for pruning.</param> + /// <param name="stateHasFlag">An optional, additional state by which uploads are filtered for pruning.</param> /// <returns>The number of pruned uploads.</returns> - int Prune(int age, TransferStates state = TransferStates.Completed); + int Prune(int age, TransferStates stateHasFlag = TransferStates.Completed); /// <summary> /// Removes the upload matching the specified <paramref name="id"/>. @@ -489,7 +489,7 @@ namespace slskd.Transfers.Uploads .AsNoTracking() .Where(t => t.Direction == TransferDirection.Upload) .Where(expression) - .GroupBy(t => true) // https://stackoverflow.com/a/25489456 + .GroupBy(t => true) // https://stackoverflow.com/a/25489456: The GroupBy(x => true) statement places all items into a single group. The Select statement the allows operations against each group. .Select(t => new { Files = t.Count(), @@ -541,13 +541,13 @@ namespace slskd.Transfers.Uploads /// Removes <see cref="TransferStates.Completed"/> uploads older than the specified <paramref name="age"/>. /// </summary> /// <param name="age">The age after which uploads are eligible for pruning, in minutes.</param> - /// <param name="state">An optional, additional state by which uploads are filtered for pruning.</param> + /// <param name="stateHasFlag">An optional, additional state by which uploads are filtered for pruning.</param> /// <returns>The number of pruned uploads.</returns> - public int Prune(int age, TransferStates state = TransferStates.Completed) + public int Prune(int age, TransferStates stateHasFlag = TransferStates.Completed) { - if (!state.HasFlag(TransferStates.Completed)) + if (!stateHasFlag.HasFlag(TransferStates.Completed)) { - throw new ArgumentException($"State must include {TransferStates.Completed}", nameof(state)); + throw new ArgumentException($"State must include {TransferStates.Completed}", nameof(stateHasFlag)); } try @@ -560,11 +560,7 @@ namespace slskd.Transfers.Uploads .Where(t => t.Direction == TransferDirection.Upload) .Where(t => !t.Removed) .Where(t => t.EndedAt.HasValue && t.EndedAt.Value < cutoffDateTime) - - // note: don't try HasFlag() here: https://github.com/dotnet/efcore/issues/20094 - // this won't work because the state is stored as a comma separated string (which we've done deliberately) - // and EF won't do the necessary work to generate the required SQL - .Where(t => t.State == state) + .Where(t => t.State.HasFlag(stateHasFlag)) .ToList(); foreach (var tx in expired) @@ -576,7 +572,7 @@ namespace slskd.Transfers.Uploads if (pruned > 0) { - Log.Debug("Pruned {Count} expired uploads with state {State}", pruned, state); + Log.Debug("Pruned {Count} expired uploads with state {State}", pruned, stateHasFlag); } return pruned; |
