aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Transfers
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-10-06 18:06:22 -0500
committerGitHub <noreply@github.com>2024-10-06 18:06:22 -0500
commitbac18981efba4daa4e4668dd382b7fbed8765636 (patch)
tree86307843bd193b5eb5750e254e231dc3751d1f40 /src/slskd/Transfers
parent3fb1176c9788d2de5527df282702aefb0497a0ec (diff)
parent5dea82dbd647cdc28c89a1d37e168d3a47abeea7 (diff)
downloadslskd-bac18981efba4daa4e4668dd382b7fbed8765636.tar.gz
Merge pull request #1215 from slskd/tasks
Add 'Events'
Diffstat (limited to 'src/slskd/Transfers')
-rw-r--r--src/slskd/Transfers/Downloads/DownloadService.cs51
-rw-r--r--src/slskd/Transfers/Types/Transfer.cs4
-rw-r--r--src/slskd/Transfers/Types/Upload.cs2
3 files changed, 47 insertions, 10 deletions
diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs
index 32903bf9..37df355c 100644
--- a/src/slskd/Transfers/Downloads/DownloadService.cs
+++ b/src/slskd/Transfers/Downloads/DownloadService.cs
@@ -29,6 +29,7 @@ namespace slskd.Transfers.Downloads
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Serilog;
+ using slskd.Events;
using slskd.Files;
using slskd.Integrations.FTP;
using slskd.Relay;
@@ -122,7 +123,8 @@ namespace slskd.Transfers.Downloads
IDbContextFactory<TransfersDbContext> contextFactory,
FileService fileService,
IRelayService relayService,
- IFTPService ftpClient)
+ IFTPService ftpClient,
+ EventBus eventBus)
{
Client = soulseekClient;
OptionsMonitor = optionsMonitor;
@@ -130,6 +132,7 @@ namespace slskd.Transfers.Downloads
Files = fileService;
FTP = ftpClient;
Relay = relayService;
+ EventBus = eventBus;
}
private ConcurrentDictionary<Guid, CancellationTokenSource> CancellationTokens { get; } = new ConcurrentDictionary<Guid, CancellationTokenSource>();
@@ -140,6 +143,7 @@ namespace slskd.Transfers.Downloads
private IRelayService Relay { get; }
private ILogger Log { get; } = Serilog.Log.ForContext<DownloadService>();
private IOptionsMonitor<Options> OptionsMonitor { get; }
+ private EventBus EventBus { get; }
/// <summary>
/// Adds the specified <paramref name="transfer"/>. Supersedes any existing record for the same file and username.
@@ -281,7 +285,7 @@ namespace slskd.Transfers.Downloads
{
transfer = transfer.WithSoulseekTransfer(args.Transfer);
- // todo: broadcast
+ // todo: broadcast to signalr hub
SynchronizedUpdate(transfer);
}));
@@ -312,25 +316,54 @@ namespace slskd.Transfers.Downloads
transfer = transfer.WithSoulseekTransfer(completedTransfer);
- // todo: broadcast
+ // todo: broadcast to signalr hub
SynchronizedUpdate(transfer, cancellable: false);
- // this would be the ideal place to hook in a generic post-download task processor for now, we'll
- // just carry out hard coded behavior. these carry the risk of failing the transfer, and i could
- // argue both ways for that being the correct behavior. revisit this later.
+ var destinationDirectory = System.IO.Path.GetDirectoryName(file.Filename.ToLocalFilename(baseDirectory: OptionsMonitor.CurrentValue.Directories.Downloads));
+
var finalFilename = Files.MoveFile(
sourceFilename: file.Filename.ToLocalFilename(baseDirectory: OptionsMonitor.CurrentValue.Directories.Incomplete),
- destinationDirectory: System.IO.Path.GetDirectoryName(file.Filename.ToLocalFilename(baseDirectory: OptionsMonitor.CurrentValue.Directories.Downloads)),
+ destinationDirectory: destinationDirectory,
overwrite: false,
deleteSourceDirectoryIfEmptyAfterMove: true);
Log.Debug("Moved file to {Destination}", finalFilename);
+ // begin post-processing tasks; the file is downloaded, it has been removed from the client's download dictionary,
+ // and the file has been moved from the incomplete directory to the downloads directory
if (OptionsMonitor.CurrentValue.Relay.Enabled)
{
_ = Relay.NotifyFileDownloadCompleteAsync(finalFilename);
}
+ EventBus.Raise(new DownloadFileCompleteEvent
+ {
+ Timestamp = transfer.EndedAt.Value,
+ LocalFilename = finalFilename,
+ RemoteFilename = transfer.Filename,
+ Transfer = transfer,
+ });
+
+ // try to figure out if this file is the last of a directory, and if so, raise the associated
+ // event. this can be tricky because we want to be sure that this is the last file in this specific
+ // directory, excluding any pending downloads in a subdirectory.
+ var remoteDirectorySeparator = transfer.Filename.GuessDirectorySeparator();
+ var remoteDirectoryName = transfer.Filename.GetDirectoryName(directorySeparator: remoteDirectorySeparator);
+ var pendingDownloadsInDirectory = Client.Downloads
+ .Where(t => t.Username == transfer.Username)
+ .Where(t => t.Filename.GetDirectoryName(directorySeparator: remoteDirectorySeparator) == remoteDirectoryName);
+
+ if (!pendingDownloadsInDirectory.Any())
+ {
+ EventBus.Raise(new DownloadDirectoryCompleteEvent
+ {
+ Timestamp = transfer.EndedAt.Value,
+ Username = transfer.Username,
+ LocalDirectoryName = destinationDirectory,
+ RemoteDirectoryName = remoteDirectoryName,
+ });
+ }
+
if (OptionsMonitor.CurrentValue.Integration.Ftp.Enabled)
{
_ = FTP.UploadAsync(finalFilename);
@@ -342,7 +375,7 @@ namespace slskd.Transfers.Downloads
transfer.Exception = ex.Message;
transfer.State = TransferStates.Completed | TransferStates.Cancelled;
- // todo: broadcast
+ // todo: broadcast to signalr hub
SynchronizedUpdate(transfer, cancellable: false);
throw;
@@ -355,7 +388,7 @@ namespace slskd.Transfers.Downloads
transfer.Exception = ex.Message;
transfer.State = TransferStates.Completed | TransferStates.Errored;
- // todo: broadcast
+ // todo: broadcast to signalr hub
SynchronizedUpdate(transfer, cancellable: false);
throw;
diff --git a/src/slskd/Transfers/Types/Transfer.cs b/src/slskd/Transfers/Types/Transfer.cs
index 1797adcd..ddb3e017 100644
--- a/src/slskd/Transfers/Types/Transfer.cs
+++ b/src/slskd/Transfers/Types/Transfer.cs
@@ -29,6 +29,10 @@ namespace slskd.Transfers
public Guid Id { get; init; }
public string Username { get; init; }
public TransferDirection Direction { get; init; }
+
+ /// <summary>
+ /// Gets the remote filename.
+ /// </summary>
public string Filename { get; init; }
public long Size { get; set; }
public long StartOffset { get; init; }
diff --git a/src/slskd/Transfers/Types/Upload.cs b/src/slskd/Transfers/Types/Upload.cs
index bc2b69b1..9dbd6696 100644
--- a/src/slskd/Transfers/Types/Upload.cs
+++ b/src/slskd/Transfers/Types/Upload.cs
@@ -30,4 +30,4 @@ namespace slskd.Transfers
public TaskCompletionSource TaskCompletionSource { get; set; } = new TaskCompletionSource();
public string Username { get; set; }
}
-}
+} \ No newline at end of file