diff --git a/CHANGELOG.md b/CHANGELOG.md index 76a71b6..ebf941a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [2.0.60] - 2024-01-21 ### Changed - Fixed bug where downloads could get stuck in active state while deleted. +- Use the RD folder structure when downloading a file. ## [2.0.59] - 2024-01-21 ### Changed diff --git a/server/RdtClient.Service/Helpers/DownloadHelper.cs b/server/RdtClient.Service/Helpers/DownloadHelper.cs index 7630f4e..1dc9830 100644 --- a/server/RdtClient.Service/Helpers/DownloadHelper.cs +++ b/server/RdtClient.Service/Helpers/DownloadHelper.cs @@ -19,17 +19,33 @@ public static class DownloadHelper var uri = new Uri(fileUrl); var torrentPath = Path.Combine(downloadPath, directory); - if (!Directory.Exists(torrentPath)) - { - Directory.CreateDirectory(torrentPath); - } - var fileName = uri.Segments.Last(); fileName = HttpUtility.UrlDecode(fileName); fileName = FileHelper.RemoveInvalidFileNameChars(fileName); + var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList(); + + if (matchingTorrentFiles.Any()) + { + var matchingTorrentFile = matchingTorrentFiles[0]; + + var subPath = Path.GetDirectoryName(matchingTorrentFile.Path); + + if (!String.IsNullOrWhiteSpace(subPath)) + { + subPath = subPath.Trim('/').Trim('\\'); + + torrentPath = Path.Combine(torrentPath, subPath); + } + } + + if (!Directory.Exists(torrentPath)) + { + Directory.CreateDirectory(torrentPath); + } + var filePath = Path.Combine(torrentPath, fileName); return filePath; @@ -46,11 +62,6 @@ public static class DownloadHelper var uri = new Uri(fileUrl); var torrentPath = RemoveInvalidPathChars(torrent.RdName); - - if (!Directory.Exists(torrentPath)) - { - Directory.CreateDirectory(torrentPath); - } var fileName = uri.Segments.Last(); @@ -58,6 +69,27 @@ public static class DownloadHelper fileName = FileHelper.RemoveInvalidFileNameChars(fileName); + var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList(); + + if (matchingTorrentFiles.Any()) + { + var matchingTorrentFile = matchingTorrentFiles[0]; + + var subPath = Path.GetDirectoryName(matchingTorrentFile.Path); + + if (!String.IsNullOrWhiteSpace(subPath)) + { + subPath = subPath.Trim('/').Trim('\\'); + + torrentPath = Path.Combine(torrentPath, subPath); + } + } + + if (!Directory.Exists(torrentPath)) + { + Directory.CreateDirectory(torrentPath); + } + var filePath = Path.Combine(torrentPath, fileName); return filePath; diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index a7e32f3..bb35dee 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -384,7 +384,7 @@ public class TorrentRunner Log($"Setting download path to {downloadPath}", download, torrent); // Start the download process - var downloadClient = new DownloadClient(_loggerFactory, download, torrent, downloadPath); + var downloadClient = new DownloadClient(download, torrent, downloadPath); Log($"Starting download", download, torrent);