diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-05-03 18:37:00 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-05-03 18:37:00 -0500 |
| commit | 08380a1a150f58839798ca3289aa8b5542f41086 (patch) | |
| tree | 02ba182d6e76391e37cfb436bdf4d2d60dcbfcb2 /src | |
| parent | c935b990b7979491b03d7b5d861d75a67ad6f3f9 (diff) | |
| download | slskd-08380a1a150f58839798ca3289aa8b5542f41086.tar.gz | |
shuffle some things around
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Program.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Transfers/Batches/BatchService.cs | 179 | ||||
| -rw-r--r-- | src/slskd/Transfers/Downloads/BatchService.cs | 122 | ||||
| -rw-r--r-- | src/slskd/Transfers/Downloads/DownloadService.cs | 12 |
4 files changed, 135 insertions, 179 deletions
diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 42f43918..d4baf276 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -780,6 +780,7 @@ namespace slskd services.AddSingleton<ITransferService, TransferService>(); services.AddSingleton<IDownloadService, DownloadService>(); services.AddSingleton<IUploadService, UploadService>(); + services.AddSingleton<IBatchService, BatchService>(); services.AddSingleton<FileService>(); services.AddSingleton<IRelayService, RelayService>(); diff --git a/src/slskd/Transfers/Batches/BatchService.cs b/src/slskd/Transfers/Batches/BatchService.cs deleted file mode 100644 index aadaa820..00000000 --- a/src/slskd/Transfers/Batches/BatchService.cs +++ /dev/null @@ -1,179 +0,0 @@ -// <copyright file="BatchService.cs" company="JP Dillingham"> -// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ -// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ -// █__ --█ █__ --█ ◄█ - █ -// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ -// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ -// │ Copyright (c) JP Dillingham. -// │ -// │ This program is free software: you can redistribute it and/or modify -// │ it under the terms of the GNU Affero General Public License as published -// │ by the Free Software Foundation, version 3. -// │ -// │ This program is distributed in the hope that it will be useful, -// │ but WITHOUT ANY WARRANTY; without even the implied warranty of -// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// │ GNU Affero General Public License for more details. -// │ -// │ You should have received a copy of the GNU Affero General Public License -// │ along with this program. If not, see https://www.gnu.org/licenses/. -// │ -// │ This program is distributed with Additional Terms pursuant to Section 7 -// │ of the AGPLv3. See the LICENSE file in the root directory of this -// │ project for the complete terms and conditions. -// │ -// │ https://slskd.org -// │ -// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ -// │ SPDX-FileCopyrightText: JP Dillingham -// │ SPDX-License-Identifier: AGPL-3.0-only -// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ -// </copyright> - -namespace slskd.Transfers -{ - using System; - using System.Collections.Generic; - using System.Linq; - using System.Linq.Expressions; - using System.Threading.Tasks; - using Microsoft.EntityFrameworkCore; - using Serilog; - - /// <summary> - /// Manages transfer batches. - /// </summary> - public interface IBatchService - { - /// <summary> - /// Creates a new batch record. - /// </summary> - /// <param name="batch">The batch to create.</param> - /// <returns>The created batch.</returns> - Batch Create(Batch batch); - - /// <summary> - /// Finds a single batch matching the specified <paramref name="expression"/>. - /// </summary> - /// <param name="expression">The expression to use to match batches.</param> - /// <returns>The found batch, or default if not found.</returns> - /// <exception cref="ArgumentException">Thrown when an expression is not supplied.</exception> - Task<Batch> FindAsync(Expression<Func<Batch, bool>> expression); - - /// <summary> - /// Returns a list of all batches matching the optional <paramref name="expression"/>. - /// </summary> - /// <param name="expression">An optional expression used to match batches.</param> - /// <returns>The list of batches matching the specified expression, or all batches if no expression is specified.</returns> - Task<List<Batch>> ListAsync(Expression<Func<Batch, bool>> expression = null); - - /// <summary> - /// Marks all transfers associated with the specified batch as removed. - /// </summary> - /// <param name="id">The unique identifier of the batch.</param> - /// <exception cref="NotFoundException">Thrown when the batch does not exist.</exception> - void Remove(Guid id); - } - - /// <summary> - /// Manages transfer batches. - /// </summary> - public class BatchService : IBatchService - { - /// <summary> - /// Initializes a new instance of the <see cref="BatchService"/> class. - /// </summary> - /// <param name="contextFactory">The database context factory to use.</param> - public BatchService(IDbContextFactory<TransfersDbContext> contextFactory) - { - ContextFactory = contextFactory; - } - - private IDbContextFactory<TransfersDbContext> ContextFactory { get; } - private ILogger Log { get; } = Serilog.Log.ForContext<BatchService>(); - - /// <summary> - /// Creates a new batch record. - /// </summary> - /// <param name="batch">The batch to create.</param> - /// <returns>The created batch.</returns> - public Batch Create(Batch batch) - { - if (batch == default) - { - throw new ArgumentNullException(nameof(batch)); - } - - using var context = ContextFactory.CreateDbContext(); - context.Batches.Add(batch); - context.SaveChanges(); - - Log.Debug("Created batch {Id}", batch.Id); - - return batch; - } - - /// <summary> - /// Finds a single batch matching the specified <paramref name="expression"/>. - /// </summary> - /// <param name="expression">The expression to use to match batches.</param> - /// <returns>The found batch, or default if not found.</returns> - /// <exception cref="ArgumentException">Thrown when an expression is not supplied.</exception> - public async Task<Batch> FindAsync(Expression<Func<Batch, bool>> expression) - { - if (expression == default) - { - throw new ArgumentException("An expression must be supplied.", nameof(expression)); - } - - using var context = ContextFactory.CreateDbContext(); - - return await context.Batches - .AsNoTracking() - .Include(b => b.Transfers) - .Where(expression) - .SingleOrDefaultAsync(); - } - - /// <summary> - /// Returns a list of all batches matching the optional <paramref name="expression"/>. - /// </summary> - /// <param name="expression">An optional expression used to match batches.</param> - /// <returns>The list of batches matching the specified expression, or all batches if no expression is specified.</returns> - public async Task<List<Batch>> ListAsync(Expression<Func<Batch, bool>> expression = null) - { - expression ??= b => true; - - using var context = ContextFactory.CreateDbContext(); - - return await context.Batches - .AsNoTracking() - .Include(b => b.Transfers) - .Where(expression) - .ToListAsync(); - } - - /// <summary> - /// Marks all transfers associated with the specified batch as removed. - /// </summary> - /// <param name="id">The unique identifier of the batch.</param> - /// <exception cref="NotFoundException">Thrown when the batch does not exist.</exception> - public void Remove(Guid id) - { - using var context = ContextFactory.CreateDbContext(); - - var exists = context.Batches.Any(b => b.Id == id); - - if (!exists) - { - throw new NotFoundException($"No batch with id {id}"); - } - - context.Transfers - .Where(t => t.BatchId == id) - .ExecuteUpdate(s => s.SetProperty(t => t.Removed, true)); - - Log.Debug("Marked transfers for batch {Id} as removed", id); - } - } -} diff --git a/src/slskd/Transfers/Downloads/BatchService.cs b/src/slskd/Transfers/Downloads/BatchService.cs new file mode 100644 index 00000000..7a4ba055 --- /dev/null +++ b/src/slskd/Transfers/Downloads/BatchService.cs @@ -0,0 +1,122 @@ +// <copyright file="BatchService.cs" company="JP Dillingham"> +// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄ +// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █ +// █__ --█ █__ --█ ◄█ - █ +// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█ +// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉ +// │ Copyright (c) JP Dillingham. +// │ +// │ This program is free software: you can redistribute it and/or modify +// │ it under the terms of the GNU Affero General Public License as published +// │ by the Free Software Foundation, version 3. +// │ +// │ This program is distributed in the hope that it will be useful, +// │ but WITHOUT ANY WARRANTY; without even the implied warranty of +// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// │ GNU Affero General Public License for more details. +// │ +// │ You should have received a copy of the GNU Affero General Public License +// │ along with this program. If not, see https://www.gnu.org/licenses/. +// │ +// │ This program is distributed with Additional Terms pursuant to Section 7 +// │ of the AGPLv3. See the LICENSE file in the root directory of this +// │ project for the complete terms and conditions. +// │ +// │ https://slskd.org +// │ +// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌ +// │ SPDX-FileCopyrightText: JP Dillingham +// │ SPDX-License-Identifier: AGPL-3.0-only +// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈ +// </copyright> + +namespace slskd.Transfers.Downloads; + +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Serilog; + +/// <summary> +/// Manages transfer batches. +/// </summary> +public interface IBatchService +{ + /// <summary> + /// Creates a new batch record. + /// </summary> + /// <param name="batch">The batch to create.</param> + /// <returns>The created batch.</returns> + Batch Create(Batch batch); + + /// <summary> + /// Finds a single batch matching the specified <paramref name="expression"/>. + /// </summary> + /// <param name="expression">The expression to use to match batches.</param> + /// <returns>The found batch, or default if not found.</returns> + /// <exception cref="ArgumentException">Thrown when an expression is not supplied.</exception> + Task<Batch> FindAsync(Expression<Func<Batch, bool>> expression); +} + +/// <summary> +/// Manages transfer batches. +/// </summary> +public class BatchService : IBatchService +{ + /// <summary> + /// Initializes a new instance of the <see cref="BatchService"/> class. + /// </summary> + /// <param name="contextFactory">The database context factory to use.</param> + public BatchService(IDbContextFactory<TransfersDbContext> contextFactory) + { + ContextFactory = contextFactory; + } + + private IDbContextFactory<TransfersDbContext> ContextFactory { get; } + private ILogger Log { get; } = Serilog.Log.ForContext<BatchService>(); + + /// <summary> + /// Creates a new batch record. + /// </summary> + /// <param name="batch">The batch to create.</param> + /// <returns>The created batch.</returns> + public Batch Create(Batch batch) + { + if (batch == default) + { + throw new ArgumentNullException(nameof(batch)); + } + + using var context = ContextFactory.CreateDbContext(); + context.Batches.Add(batch); + context.SaveChanges(); + + Log.Debug("Created batch {Id}", batch.Id); + + return batch; + } + + /// <summary> + /// Finds a single batch matching the specified <paramref name="expression"/>. + /// </summary> + /// <param name="expression">The expression to use to match batches.</param> + /// <returns>The found batch, or default if not found.</returns> + /// <exception cref="ArgumentException">Thrown when an expression is not supplied.</exception> + public async Task<Batch> FindAsync(Expression<Func<Batch, bool>> expression) + { + if (expression == default) + { + throw new ArgumentException("An expression must be supplied.", nameof(expression)); + } + + using var context = ContextFactory.CreateDbContext(); + + return await context.Batches + .AsNoTracking() + .Include(b => b.Transfers) + .Where(expression) + .SingleOrDefaultAsync(); + } +} diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index aba6ca9d..9e898570 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -56,6 +56,11 @@ namespace slskd.Transfers.Downloads public interface IDownloadService { /// <summary> + /// Gets download batch service. + /// </summary> + IBatchService Batches { get; } + + /// <summary> /// Adds the specified <paramref name="transfer"/>. Supersedes any existing record for the same file and username. /// </summary> /// <remarks>This should generally not be called; use EnqueueAsync() instead.</remarks> @@ -158,6 +163,7 @@ namespace slskd.Transfers.Downloads public class DownloadService : IDownloadService { public DownloadService( + IBatchService batchService, IOptionsMonitor<Options> optionsMonitor, ISoulseekClient soulseekClient, IDbContextFactory<TransfersDbContext> contextFactory, @@ -166,6 +172,7 @@ namespace slskd.Transfers.Downloads IFTPService ftpClient, EventBus eventBus) { + Batches = batchService; Client = soulseekClient; OptionsMonitor = optionsMonitor; ContextFactory = contextFactory; @@ -178,6 +185,11 @@ namespace slskd.Transfers.Downloads } /// <summary> + /// Gets download batch service. + /// </summary> + public IBatchService Batches { get; } + + /// <summary> /// These tokens give users the ability to cancel transfers via the UI (or API). /// /// The lifecycle is: |
