diff --git a/server/RdtClient.Data/Data/DownloadData.cs b/server/RdtClient.Data/Data/DownloadData.cs index 4488b9d..9d141f9 100644 --- a/server/RdtClient.Data/Data/DownloadData.cs +++ b/server/RdtClient.Data/Data/DownloadData.cs @@ -67,6 +67,23 @@ public class DownloadData(DataContext dataContext) await TorrentData.VoidCache(); } + public async Task UpdateFileName(Guid downloadId, String fileName) + { + var dbDownload = await dataContext.Downloads + .FirstOrDefaultAsync(m => m.DownloadId == downloadId); + + if (dbDownload == null) + { + return; + } + + dbDownload.FileName = fileName; + + await dataContext.SaveChangesAsync(); + + await TorrentData.VoidCache(); + } + public async Task UpdateDownloadStarted(Guid downloadId, DateTimeOffset? dateTime) { var dbDownload = await dataContext.Downloads diff --git a/server/RdtClient.Service/Helpers/DownloadHelper.cs b/server/RdtClient.Service/Helpers/DownloadHelper.cs index a4cec3a..06724ad 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, String? fileName = null) + public static String? GetDownloadPath(String downloadPath, Torrent torrent, Download download) { var fileUrl = download.Link; - if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null || fileName == null) + if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null) { return null; } @@ -19,6 +19,8 @@ public static class DownloadHelper var uri = new Uri(fileUrl); var torrentPath = Path.Combine(downloadPath, directory); + var fileName = download.FileName; + if (String.IsNullOrWhiteSpace(fileName)) { fileName = uri.Segments.Last(); @@ -66,11 +68,14 @@ public static class DownloadHelper var uri = new Uri(fileUrl); var torrentPath = RemoveInvalidPathChars(torrent.RdName); - var fileName = uri.Segments.Last(); + var fileName = download.FileName; - fileName = HttpUtility.UrlDecode(fileName); + if (String.IsNullOrWhiteSpace(fileName)) + { + fileName = uri.Segments.Last(); - fileName = FileHelper.RemoveInvalidFileNameChars(fileName); + fileName = HttpUtility.UrlDecode(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/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index 6b55c83..0774d0c 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -349,6 +349,9 @@ public class TorrentRunner(ILogger logger, Torrents torrents, Dow var downloadLink = await torrents.UnrestrictLink(download.DownloadId); download.Link = downloadLink; + + var fileName = await torrents.RetrieveFileName(download.DownloadId); + download.FileName = fileName; } } catch (Exception ex) diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index 0d3d7ae..4f7b0a5 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -370,6 +370,19 @@ public class Torrents( return unrestrictedLink; } + public async Task RetrieveFileName(Guid downloadId) + { + var download = await downloads.GetById(downloadId) ?? throw new($"Download with ID {downloadId} not found"); + + Log($"Unrestricting link", download, download.Torrent); + + var fileName = await TorrentClient.GetFileName(download.Link!); + + await downloads.UpdateFileName(downloadId, fileName); + + return fileName; + } + public async Task GetProfile() { var user = await TorrentClient.GetUser(); @@ -547,9 +560,7 @@ public class Torrents( var downloadPath = DownloadPath(download.Torrent!); - var fileName = await TorrentClient.GetFileName(download); - - var filePath = DownloadHelper.GetDownloadPath(downloadPath, download.Torrent!, download, fileName); + var filePath = DownloadHelper.GetDownloadPath(downloadPath, download.Torrent!, download); if (filePath != null) {