From c34c62f87924abc5912d1b888bd1cef227961f37 Mon Sep 17 00:00:00 2001 From: Sam Heinz <54530346+asylumexp@users.noreply.github.com> Date: Tue, 10 Dec 2024 15:06:51 +1000 Subject: [PATCH] [TB] Move handling filenames from DownloadHelper to TorrentClients --- .../Helpers/DownloadHelper.cs | 8 ++---- .../TorrentClients/AllDebridTorrentClient.cs | 12 +++++++++ .../Services/TorrentClients/ITorrentClient.cs | 1 + .../TorrentClients/PremiumizeTorrentClient.cs | 13 +++++++++ .../TorrentClients/RealDebridTorrentClient.cs | 13 +++++++++ .../TorrentClients/TorBoxTorrentClient.cs | 27 +++++++++++++++++++ server/RdtClient.Service/Services/Torrents.cs | 4 ++- 7 files changed, 71 insertions(+), 7 deletions(-) diff --git a/server/RdtClient.Service/Helpers/DownloadHelper.cs b/server/RdtClient.Service/Helpers/DownloadHelper.cs index bbb1775..02d26f0 100644 --- a/server/RdtClient.Service/Helpers/DownloadHelper.cs +++ b/server/RdtClient.Service/Helpers/DownloadHelper.cs @@ -5,11 +5,11 @@ namespace RdtClient.Service.Helpers; public static class DownloadHelper { - public static String? GetDownloadPath(String downloadPath, Torrent torrent, Download download) + public static String? GetDownloadPath(String downloadPath, Torrent torrent, Download download, String? fileName) { var fileUrl = download.Link; - if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null) + if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null || fileName == null) { return null; } @@ -19,10 +19,6 @@ public static class DownloadHelper var uri = new Uri(fileUrl); var torrentPath = Path.Combine(downloadPath, directory); - 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(); diff --git a/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs b/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs index bc0102c..540b3d3 100644 --- a/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs +++ b/server/RdtClient.Service/Services/TorrentClients/AllDebridTorrentClient.cs @@ -4,6 +4,7 @@ using Newtonsoft.Json; using RdtClient.Data.Enums; using RdtClient.Data.Models.TorrentClient; using RdtClient.Service.Helpers; +using System.Web; using File = AllDebridNET.File; using Torrent = RdtClient.Data.Models.Data.Torrent; @@ -262,6 +263,17 @@ public class AllDebridTorrentClient(ILogger logger, IHtt return links.Select(m => m.LinkUrl.ToString()).ToList(); } + public Task GetFileName(Data.Models.Data.Download download) + { + if (String.IsNullOrWhiteSpace(download.Link)) + { + return Task.FromResult(null); + } + + var uri = new Uri(download.Link); + + return Task.FromResult(HttpUtility.UrlDecode(uri.Segments.Last())); + } private async Task GetInfo(String torrentId) { diff --git a/server/RdtClient.Service/Services/TorrentClients/ITorrentClient.cs b/server/RdtClient.Service/Services/TorrentClients/ITorrentClient.cs index da4aa39..95fb597 100644 --- a/server/RdtClient.Service/Services/TorrentClients/ITorrentClient.cs +++ b/server/RdtClient.Service/Services/TorrentClients/ITorrentClient.cs @@ -15,4 +15,5 @@ public interface ITorrentClient Task Unrestrict(String link); Task UpdateData(Torrent torrent, TorrentClientTorrent? torrentClientTorrent); Task?> GetDownloadLinks(Torrent torrent); + Task GetFileName(Download download); } \ No newline at end of file diff --git a/server/RdtClient.Service/Services/TorrentClients/PremiumizeTorrentClient.cs b/server/RdtClient.Service/Services/TorrentClients/PremiumizeTorrentClient.cs index cb170c2..ab33428 100644 --- a/server/RdtClient.Service/Services/TorrentClients/PremiumizeTorrentClient.cs +++ b/server/RdtClient.Service/Services/TorrentClients/PremiumizeTorrentClient.cs @@ -4,6 +4,7 @@ using PremiumizeNET; using RdtClient.Data.Enums; using RdtClient.Data.Models.TorrentClient; using RdtClient.Service.Helpers; +using System.Web; using Torrent = RdtClient.Data.Models.Data.Torrent; namespace RdtClient.Service.Services.TorrentClients; @@ -241,6 +242,18 @@ public class PremiumizeTorrentClient(ILogger logger, IH return downloadLinks; } + public Task GetFileName(Data.Models.Data.Download download) + { + if (String.IsNullOrWhiteSpace(download.Link)) + { + return Task.FromResult(null); + } + + var uri = new Uri(download.Link); + + return Task.FromResult(HttpUtility.UrlDecode(uri.Segments.Last())); + } + private async Task GetInfo(String id) { var results = await GetClient().Transfers.ListAsync(); diff --git a/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs b/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs index 9cc8ad0..ecf4337 100644 --- a/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs +++ b/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs @@ -1,4 +1,5 @@ using System.Text.RegularExpressions; +using System.Web; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using RDNET; @@ -395,6 +396,18 @@ public class RealDebridTorrentClient(ILogger logger, IH return null; } + public Task GetFileName(Data.Models.Data.Download download) + { + if (String.IsNullOrWhiteSpace(download.Link)) + { + return Task.FromResult(null); + } + + var uri = new Uri(download.Link); + + return Task.FromResult(HttpUtility.UrlDecode(uri.Segments.Last())); + } + private DateTimeOffset? ChangeTimeZone(DateTimeOffset? dateTimeOffset) { if (_offset == null) diff --git a/server/RdtClient.Service/Services/TorrentClients/TorBoxTorrentClient.cs b/server/RdtClient.Service/Services/TorrentClients/TorBoxTorrentClient.cs index 088c928..485d8ce 100644 --- a/server/RdtClient.Service/Services/TorrentClients/TorBoxTorrentClient.cs +++ b/server/RdtClient.Service/Services/TorrentClients/TorBoxTorrentClient.cs @@ -3,6 +3,7 @@ using Newtonsoft.Json; using TorBoxNET; using RdtClient.Data.Enums; using RdtClient.Data.Models.TorrentClient; +using System.Web; namespace RdtClient.Service.Services.TorrentClients; @@ -300,6 +301,32 @@ public class TorBoxTorrentClient(ILogger logger, IHttpClien return files; } + public async Task GetFileName(Data.Models.Data.Download download) + { + if (String.IsNullOrWhiteSpace(download.Link)) + { + return null; + } + + var uri = new Uri(download.Link); + + using (HttpClient client = new HttpClient()) + { + HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, uri); + HttpResponseMessage response = await client.SendAsync(request); + if (response.Content.Headers.ContentDisposition != null) + { + var fileName = response.Content.Headers.ContentDisposition.FileName; + if (!String.IsNullOrWhiteSpace(fileName)) + { + return fileName.Trim('"'); + } + } + } + + return HttpUtility.UrlDecode(uri.Segments.Last()); + } + private DateTimeOffset? ChangeTimeZone(DateTimeOffset? dateTimeOffset) { if (_offset == null) diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index 25817cf..0d3d7ae 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -546,8 +546,10 @@ public class Torrents( } var downloadPath = DownloadPath(download.Torrent!); + + var fileName = await TorrentClient.GetFileName(download); - var filePath = DownloadHelper.GetDownloadPath(downloadPath, download.Torrent!, download); + var filePath = DownloadHelper.GetDownloadPath(downloadPath, download.Torrent!, download, fileName); if (filePath != null) {