From c246537d63049d08509938558e393da6d6b532d7 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 25 Jul 2022 22:01:09 -0500 Subject: rough upload implementation --- src/slskd/Transfers/Downloads/DownloadService.cs | 10 +- src/slskd/Transfers/Uploads/IUploadService.cs | 35 +++++++ src/slskd/Transfers/Uploads/UploadService.cs | 112 +++++++++++++++++++++++ 3 files changed, 156 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 8e2c3585..5890d741 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -327,13 +327,21 @@ namespace slskd.Transfers.Downloads try { using var context = await ContextFactory.CreateDbContextAsync(); - var transfer = await context.Transfers.FindAsync(id); + var transfer = await context.Transfers + .Where(t => t.Direction == TransferDirection.Download) + .Where(t => t.Id == id) + .FirstOrDefaultAsync(); if (transfer == default) { throw new NotFoundException($"No download matching id ${id}"); } + if (!transfer.State.HasFlag(TransferStates.Completed)) + { + throw new InvalidOperationException($"Invalid attempt to remove a download before it is complete"); + } + transfer.Removed = true; await context.SaveChangesAsync(); diff --git a/src/slskd/Transfers/Uploads/IUploadService.cs b/src/slskd/Transfers/Uploads/IUploadService.cs index 71fc6067..12a2edaf 100644 --- a/src/slskd/Transfers/Uploads/IUploadService.cs +++ b/src/slskd/Transfers/Uploads/IUploadService.cs @@ -17,6 +17,11 @@ namespace slskd.Transfers.Uploads { + using System; + using System.Collections.Generic; + using System.Linq.Expressions; + using System.Threading.Tasks; + /// /// Manages uploads. /// @@ -31,5 +36,35 @@ namespace slskd.Transfers.Uploads /// Gets the upload queue. /// IUploadQueue Queue { get; } + + /// + /// Finds a single upload matching the specified . + /// + /// The expression to use to match uploads. + /// The found transfer, or default if not found. + Task FindAsync(Expression> expression); + + /// + /// Returns a list of all uploads matching the optional . + /// + /// An optional expression used to match uploads. + /// Optionally include uploads that have been removed previously. + /// The list of uploads matching the specified expression, or all uploads if no expression is specified. + Task> ListAsync(Expression> expression = null, bool includeRemoved = false); + + /// + /// Removes the upload matching the specified . + /// + /// This is a soft delete; the record is retained for historical retrieval. + /// The unique identifier of the upload. + /// + Task RemoveAsync(Guid id); + + /// + /// Cancels the upload matching the specified , if it is in progress. + /// + /// The unique identifier for the upload. + /// A value indicating whether the upload was successfully cancelled. + bool TryCancel(Guid id); } } \ No newline at end of file diff --git a/src/slskd/Transfers/Uploads/UploadService.cs b/src/slskd/Transfers/Uploads/UploadService.cs index 10e82d7f..c21e107d 100644 --- a/src/slskd/Transfers/Uploads/UploadService.cs +++ b/src/slskd/Transfers/Uploads/UploadService.cs @@ -17,11 +17,20 @@ using Microsoft.Extensions.Options; +using Soulseek; namespace slskd.Transfers.Uploads { + using System; + using System.Collections.Concurrent; + using System.Threading; + using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using slskd.Users; + using Serilog; + using System.Linq; + using System.Linq.Expressions; + using System.Collections.Generic; /// /// Manages uploads. @@ -39,7 +48,10 @@ namespace slskd.Transfers.Uploads Queue = new UploadQueue(userService, optionsMonitor); } + private ConcurrentDictionary CancellationTokens { get; } = new ConcurrentDictionary(); private IDbContextFactory ContextFactory { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); + /// /// Gets the upload governor. @@ -50,5 +62,105 @@ namespace slskd.Transfers.Uploads /// Gets the upload queue. /// public IUploadQueue Queue { get; init; } + + /// + /// Finds a single upload matching the specified . + /// + /// The expression to use to match uploads. + /// The found transfer, or default if not found. + public async Task FindAsync(Expression> expression) + { + try + { + var context = await ContextFactory.CreateDbContextAsync(); + return await context.Transfers + .Where(t => t.Direction == TransferDirection.Upload) + .Where(expression).FirstOrDefaultAsync(); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to find upload: {Message}", ex.Message); + throw; + } + } + + /// + /// Returns a list of all uploads matching the optional . + /// + /// An optional expression used to match uploads. + /// Optionally include uploads that have been removed previously. + /// The list of uploads matching the specified expression, or all uploads if no expression is specified. + public async Task> ListAsync(Expression> expression = null, bool includeRemoved = false) + { + expression ??= t => true; + + try + { + var context = await ContextFactory.CreateDbContextAsync(); + return await context.Transfers + .Where(t => t.Direction == TransferDirection.Upload) + .Where(t => !t.Removed || includeRemoved) + .Where(expression) + .ToListAsync(); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to list uploads: {Message}", ex.Message); + throw; + } + } + + /// + /// Removes the upload matching the specified . + /// + /// This is a soft delete; the record is retained for historical retrieval. + /// The unique identifier of the upload. + /// + public async Task RemoveAsync(Guid id) + { + try + { + using var context = await ContextFactory.CreateDbContextAsync(); + var transfer = await context.Transfers + .Where(t => t.Direction == TransferDirection.Upload) + .Where(t => t.Id == id) + .FirstOrDefaultAsync(); + + if (transfer == default) + { + throw new NotFoundException($"No upload matching id ${id}"); + } + + if (!transfer.State.HasFlag(TransferStates.Completed)) + { + throw new InvalidOperationException($"Invalid attempt to remove an upload before it is complete"); + } + + transfer.Removed = true; + + await context.SaveChangesAsync(); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to remove upload {Id}: {Message}", id, ex.Message); + throw; + } + } + + /// + /// Cancels the upload matching the specified , if it is in progress. + /// + /// The unique identifier for the upload. + /// A value indicating whether the upload was successfully cancelled. + public bool TryCancel(Guid id) + { + if (CancellationTokens.TryRemove(id, out var cts)) + { + cts.Cancel(); + return true; + } + + return false; + } } } -- cgit v1.2.3