From beb0299a0bb7d01feb08a543fb8da83ff0b618db Mon Sep 17 00:00:00 2001 From: Cucumberrbob <128094686+Cucumberrbob@users.noreply.github.com> Date: Wed, 29 Jan 2025 11:07:20 +0000 Subject: [PATCH] revert changes to DownloadHelper.cs --- .../Helpers/DownloadHelper.cs | 78 +++++++++++-------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/server/RdtClient.Service/Helpers/DownloadHelper.cs b/server/RdtClient.Service/Helpers/DownloadHelper.cs index bac745f..06724ad 100644 --- a/server/RdtClient.Service/Helpers/DownloadHelper.cs +++ b/server/RdtClient.Service/Helpers/DownloadHelper.cs @@ -17,6 +17,7 @@ public static class DownloadHelper var directory = RemoveInvalidPathChars(torrent.RdName); var uri = new Uri(fileUrl); + var torrentPath = Path.Combine(downloadPath, directory); var fileName = download.FileName; @@ -29,42 +30,14 @@ public static class DownloadHelper fileName = FileHelper.RemoveInvalidFileNameChars(fileName); - var torrentPath = downloadPath; + var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList(); - if (torrent.Files.Count > 1) + if (matchingTorrentFiles.Count > 0) { - torrentPath = Path.Combine(downloadPath, directory); + var matchingTorrentFile = matchingTorrentFiles[0]; - var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList(); + var subPath = Path.GetDirectoryName(matchingTorrentFile.Path); - if (matchingTorrentFiles.Count > 0) - { - var matchingTorrentFile = matchingTorrentFiles[0]; - var subPath = Path.GetDirectoryName(matchingTorrentFile.Path); - - if (!String.IsNullOrWhiteSpace(subPath)) - { - subPath = subPath.Trim('/').Trim('\\'); - - torrentPath = Path.Combine(torrentPath, subPath); - } - } - } - else if (torrent.Files.Count == 1) - { - // Debrid servers such as RealDebrid store single file torrents in a subfolder, but AllDebrid doesn't. - // We should replicate this behavior so that both folder structures are equal. - // See issue: https://github.com/rogerfar/rdt-client/issues/648 - if (torrent.ClientKind != Torrent.TorrentClientKind.AllDebrid) - { - torrentPath = Path.Combine(downloadPath, directory); - } - - var torrentFile = torrent.Files[0]; - var subPath = Path.GetDirectoryName(torrentFile.Path); - - // What we think is a single file torrent may also be a folder with a single file in it. - // So make sure we handle that here. If this is not the case, torrentPath will be empty below. if (!String.IsNullOrWhiteSpace(subPath)) { subPath = subPath.Trim('/').Trim('\\'); @@ -73,7 +46,7 @@ public static class DownloadHelper } } - if (!String.IsNullOrWhiteSpace(torrentPath) && !Directory.Exists(torrentPath)) + if (!Directory.Exists(torrentPath)) { Directory.CreateDirectory(torrentPath); } @@ -85,7 +58,44 @@ public static class DownloadHelper public static String? GetDownloadPath(Torrent torrent, Download download) { - return GetDownloadPath("", torrent, download); + var fileUrl = download.Link; + + if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null) + { + return null; + } + + var uri = new Uri(fileUrl); + var torrentPath = RemoveInvalidPathChars(torrent.RdName); + + var fileName = download.FileName; + + if (String.IsNullOrWhiteSpace(fileName)) + { + fileName = uri.Segments.Last(); + + fileName = HttpUtility.UrlDecode(fileName); + } + + var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList(); + + if (matchingTorrentFiles.Count > 0) + { + var matchingTorrentFile = matchingTorrentFiles[0]; + + var subPath = Path.GetDirectoryName(matchingTorrentFile.Path); + + if (!String.IsNullOrWhiteSpace(subPath)) + { + subPath = subPath.Trim('/').Trim('\\'); + + torrentPath = Path.Combine(torrentPath, subPath); + } + } + + var filePath = Path.Combine(torrentPath, fileName); + + return filePath; } private static String RemoveInvalidPathChars(String path)