aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2022-07-25 22:01:09 -0500
committerJP Dillingham <jpdillingham@gmail.com>2022-07-25 22:01:09 -0500
commitc246537d63049d08509938558e393da6d6b532d7 (patch)
treeb61a93bb5162bbd33cc1c46d347d9bc4b0698a50 /src
parentcde15c5166e4efd06f86cbc3f7f6f05132d7361b (diff)
downloadslskd-c246537d63049d08509938558e393da6d6b532d7.tar.gz
rough upload implementation
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs10
-rw-r--r--src/slskd/Transfers/Uploads/IUploadService.cs35
-rw-r--r--src/slskd/Transfers/Uploads/UploadService.cs112
3 files changed, 156 insertions, 1 deletions
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;
+
/// <summary>
/// Manages uploads.
/// </summary>
@@ -31,5 +36,35 @@ namespace slskd.Transfers.Uploads
/// Gets the upload queue.
/// </summary>
IUploadQueue Queue { get; }
+
+ /// <summary>
+ /// Finds a single upload matching the specified <paramref name="expression"/>.
+ /// </summary>
+ /// <param name="expression">The expression to use to match uploads.</param>
+ /// <returns>The found transfer, or default if not found.</returns>
+ Task<Transfer> FindAsync(Expression<Func<Transfer, bool>> expression);
+
+ /// <summary>
+ /// Returns a list of all uploads matching the optional <paramref name="expression"/>.
+ /// </summary>
+ /// <param name="expression">An optional expression used to match uploads.</param>
+ /// <param name="includeRemoved">Optionally 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>
+ Task<List<Transfer>> ListAsync(Expression<Func<Transfer, bool>> expression = null, bool includeRemoved = false);
+
+ /// <summary>
+ /// Removes the upload matching the specified <paramref name="id"/>.
+ /// </summary>
+ /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks>
+ /// <param name="id">The unique identifier of the upload.</param>
+ /// <returns></returns>
+ Task RemoveAsync(Guid id);
+
+ /// <summary>
+ /// Cancels the upload matching the specified <paramref name="id"/>, if it is in progress.
+ /// </summary>
+ /// <param name="id">The unique identifier for the upload.</param>
+ /// <returns>A value indicating whether the upload was successfully cancelled.</returns>
+ 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;
/// <summary>
/// Manages uploads.
@@ -39,7 +48,10 @@ namespace slskd.Transfers.Uploads
Queue = new UploadQueue(userService, optionsMonitor);
}
+ private ConcurrentDictionary<Guid, CancellationTokenSource> CancellationTokens { get; } = new ConcurrentDictionary<Guid, CancellationTokenSource>();
private IDbContextFactory<TransfersDbContext> ContextFactory { get; }
+ private ILogger Log { get; } = Serilog.Log.ForContext<UploadService>();
+
/// <summary>
/// Gets the upload governor.
@@ -50,5 +62,105 @@ namespace slskd.Transfers.Uploads
/// Gets the upload queue.
/// </summary>
public IUploadQueue Queue { get; init; }
+
+ /// <summary>
+ /// Finds a single upload matching the specified <paramref name="expression"/>.
+ /// </summary>
+ /// <param name="expression">The expression to use to match uploads.</param>
+ /// <returns>The found transfer, or default if not found.</returns>
+ public async Task<Transfer> FindAsync(Expression<Func<Transfer, bool>> 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;
+ }
+ }
+
+ /// <summary>
+ /// Returns a list of all uploads matching the optional <paramref name="expression"/>.
+ /// </summary>
+ /// <param name="expression">An optional expression used to match uploads.</param>
+ /// <param name="includeRemoved">Optionally 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 async Task<List<Transfer>> ListAsync(Expression<Func<Transfer, bool>> 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;
+ }
+ }
+
+ /// <summary>
+ /// Removes the upload matching the specified <paramref name="id"/>.
+ /// </summary>
+ /// <remarks>This is a soft delete; the record is retained for historical retrieval.</remarks>
+ /// <param name="id">The unique identifier of the upload.</param>
+ /// <returns></returns>
+ 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;
+ }
+ }
+
+ /// <summary>
+ /// Cancels the upload matching the specified <paramref name="id"/>, if it is in progress.
+ /// </summary>
+ /// <param name="id">The unique identifier for the upload.</param>
+ /// <returns>A value indicating whether the upload was successfully cancelled.</returns>
+ public bool TryCancel(Guid id)
+ {
+ if (CancellationTokens.TryRemove(id, out var cts))
+ {
+ cts.Cancel();
+ return true;
+ }
+
+ return false;
+ }
}
}