From 60c78206b2f9abfd05aa4f7390fbb0546537440d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 6 Aug 2022 08:15:02 -0500 Subject: add AddOrSupersedeAsync() --- src/slskd/Transfers/Downloads/DownloadService.cs | 26 ++++++++++++++++++++--- src/slskd/Transfers/Downloads/IDownloadService.cs | 8 +++++++ src/slskd/Transfers/Uploads/IUploadService.cs | 8 +++++++ src/slskd/Transfers/Uploads/UploadService.cs | 26 ++++++++++++++++++++--- 4 files changed, 62 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 80104593..58b44c37 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -56,6 +56,28 @@ namespace slskd.Transfers.Downloads private ILogger Log { get; } = Serilog.Log.ForContext(); private IOptionsMonitor OptionsMonitor { get; } + /// + /// Adds the specified . Supersedes any existing record for the same file and username. + /// + /// This should generally not be called; use instead. + /// + /// + public async Task AddOrSupersedeAsync(Transfer transfer) + { + var existing = await FindAsync(t => t.Username == transfer.Username && t.Filename == transfer.Filename); + + using var context = await ContextFactory.CreateDbContextAsync(); + + if (existing != default) + { + existing.Removed = true; + context.Update(existing); + } + + context.Add(transfer); + await context.SaveChangesAsync(); + } + /// /// Enqueues the requested list of . /// @@ -113,9 +135,7 @@ namespace slskd.Transfers.Downloads }; // persist the transfer to the database so we have a record that it was attempted - using var context = await ContextFactory.CreateDbContextAsync(); - context.Add(transfer); - await context.SaveChangesAsync(); + await AddOrSupersedeAsync(transfer); var cts = new CancellationTokenSource(); CancellationTokens.TryAdd(id, cts); diff --git a/src/slskd/Transfers/Downloads/IDownloadService.cs b/src/slskd/Transfers/Downloads/IDownloadService.cs index df17391d..1c1d8202 100644 --- a/src/slskd/Transfers/Downloads/IDownloadService.cs +++ b/src/slskd/Transfers/Downloads/IDownloadService.cs @@ -27,6 +27,14 @@ namespace slskd.Transfers.Downloads /// public interface IDownloadService { + /// + /// Adds the specified . Supersedes any existing record for the same file and username. + /// + /// This should generally not be called; use instead. + /// + /// + Task AddOrSupersedeAsync(Transfer transfer); + /// /// Enqueues the requested list of . /// diff --git a/src/slskd/Transfers/Uploads/IUploadService.cs b/src/slskd/Transfers/Uploads/IUploadService.cs index 0d829227..b902f20c 100644 --- a/src/slskd/Transfers/Uploads/IUploadService.cs +++ b/src/slskd/Transfers/Uploads/IUploadService.cs @@ -37,6 +37,14 @@ namespace slskd.Transfers.Uploads /// IUploadQueue Queue { get; } + /// + /// Adds the specified . Supersedes any existing record for the same file and username. + /// + /// This should generally not be called; use instead. + /// + /// + Task AddOrSupersedeAsync(Transfer transfer); + /// /// Enqueues the requested file. /// diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 8b530bc0..bb2da3d1 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -71,6 +71,28 @@ namespace slskd.Transfers.Uploads private IShareService Shares { get; set; } private IUserService Users { get; set; } + /// + /// Adds the specified . Supersedes any existing record for the same file and username. + /// + /// This should generally not be called; use instead. + /// + /// + public async Task AddOrSupersedeAsync(Transfer transfer) + { + var existing = await FindAsync(t => t.Username == transfer.Username && t.Filename == transfer.Filename); + + using var context = await ContextFactory.CreateDbContextAsync(); + + if (existing != default) + { + existing.Removed = true; + context.Update(existing); + } + + context.Add(transfer); + await context.SaveChangesAsync(); + } + /// /// Enqueues the requested file. /// @@ -123,9 +145,7 @@ namespace slskd.Transfers.Uploads }; // persist the transfer to the database so we have a record that it was attempted - using var context = await ContextFactory.CreateDbContextAsync(); - context.Add(transfer); - await context.SaveChangesAsync(); + await AddOrSupersedeAsync(transfer); // create a new cancellation token source so that we can cancel the upload from the UI. var cts = new CancellationTokenSource(); -- cgit v1.2.3