From 38a3bc1537be3c76367017d4f787c54e3e6149ff Mon Sep 17 00:00:00 2001 From: Sculas Date: Tue, 7 Jan 2025 14:34:02 +0100 Subject: [PATCH] perf(SymlinkDownloader): Skip file search when using Alldebrid --- .../Services/DownloadClient.cs | 2 +- .../Services/Downloaders/SymlinkDownloader.cs | 26 ++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/server/RdtClient.Service/Services/DownloadClient.cs b/server/RdtClient.Service/Services/DownloadClient.cs index 3ab400b..f35ba1c 100644 --- a/server/RdtClient.Service/Services/DownloadClient.cs +++ b/server/RdtClient.Service/Services/DownloadClient.cs @@ -51,7 +51,7 @@ public class DownloadClient(Download download, Torrent torrent, String destinati Data.Enums.DownloadClient.Internal => new InternalDownloader(download.Link, filePath), Data.Enums.DownloadClient.Bezzad => new BezzadDownloader(download.Link, filePath), Data.Enums.DownloadClient.Aria2c => new Aria2cDownloader(download.RemoteId, download.Link, filePath, downloadPath, category), - Data.Enums.DownloadClient.Symlink => new SymlinkDownloader(download.Link, filePath, downloadPath), + Data.Enums.DownloadClient.Symlink => new SymlinkDownloader(download.Link, filePath, downloadPath, torrent.ClientKind), _ => throw new($"Unknown download client {Type}") }; diff --git a/server/RdtClient.Service/Services/Downloaders/SymlinkDownloader.cs b/server/RdtClient.Service/Services/Downloaders/SymlinkDownloader.cs index 318b7ee..406f5ed 100644 --- a/server/RdtClient.Service/Services/Downloaders/SymlinkDownloader.cs +++ b/server/RdtClient.Service/Services/Downloaders/SymlinkDownloader.cs @@ -1,9 +1,10 @@ -using RdtClient.Service.Helpers; +using RdtClient.Data.Models.Data; +using RdtClient.Service.Helpers; using Serilog; namespace RdtClient.Service.Services.Downloaders; -public class SymlinkDownloader(String uri, String destinationPath, String path) : IDownloader +public class SymlinkDownloader(String uri, String destinationPath, String path, Torrent.TorrentClientKind? clientKind) : IDownloader { public event EventHandler? DownloadComplete; public event EventHandler? DownloadProgress; @@ -56,6 +57,22 @@ public class SymlinkDownloader(String uri, String destinationPath, String path) BytesTotal = 0, Speed = 0 }); + + String? file = null; + + // When resolving symlinks for AllDebrid, we know the exact file path, so we can skip the search. + if (clientKind == Torrent.TorrentClientKind.AllDebrid) + { + var potentialFilePath = Path.Combine(rcloneMountPath, path); + + // Make sure the file exists before making any assumptions. + // If this somehow fails, fallback to the search below. + if (File.Exists(potentialFilePath)) + { + file = potentialFilePath; + goto skipFileSearch; + } + } var potentialFilePaths = new List { searchPath }; @@ -78,8 +95,6 @@ public class SymlinkDownloader(String uri, String destinationPath, String path) potentialFilePaths = potentialFilePaths.Distinct().ToList(); - String? file = null; - for (var retryCount = 0; retryCount < MaxRetries; retryCount++) { DownloadProgress?.Invoke(this, @@ -135,6 +150,9 @@ public class SymlinkDownloader(String uri, String destinationPath, String path) throw new("Could not find file from rclone mount!"); } + + // Used by Alldebrid since we know the exact file path. + skipFileSearch: _logger.Debug($"Creating symbolic link from {file} to {destinationPath}");