[TB] Move handling filenames from DownloadHelper to TorrentClients

This commit is contained in:
Sam Heinz 2024-12-10 15:06:51 +10:00
parent 1a9a0ddf3c
commit c34c62f879
7 changed files with 71 additions and 7 deletions

View file

@ -5,11 +5,11 @@ namespace RdtClient.Service.Helpers;
public static class DownloadHelper 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; var fileUrl = download.Link;
if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null) if (String.IsNullOrWhiteSpace(fileUrl) || torrent.RdName == null || fileName == null)
{ {
return null; return null;
} }
@ -19,10 +19,6 @@ public static class DownloadHelper
var uri = new Uri(fileUrl); var uri = new Uri(fileUrl);
var torrentPath = Path.Combine(downloadPath, directory); var torrentPath = Path.Combine(downloadPath, directory);
var fileName = uri.Segments.Last();
fileName = HttpUtility.UrlDecode(fileName);
fileName = FileHelper.RemoveInvalidFileNameChars(fileName); fileName = FileHelper.RemoveInvalidFileNameChars(fileName);
var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList(); var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList();

View file

@ -4,6 +4,7 @@ using Newtonsoft.Json;
using RdtClient.Data.Enums; using RdtClient.Data.Enums;
using RdtClient.Data.Models.TorrentClient; using RdtClient.Data.Models.TorrentClient;
using RdtClient.Service.Helpers; using RdtClient.Service.Helpers;
using System.Web;
using File = AllDebridNET.File; using File = AllDebridNET.File;
using Torrent = RdtClient.Data.Models.Data.Torrent; using Torrent = RdtClient.Data.Models.Data.Torrent;
@ -262,6 +263,17 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
return links.Select(m => m.LinkUrl.ToString()).ToList(); return links.Select(m => m.LinkUrl.ToString()).ToList();
} }
public Task<String?> GetFileName(Data.Models.Data.Download download)
{
if (String.IsNullOrWhiteSpace(download.Link))
{
return Task.FromResult<String?>(null);
}
var uri = new Uri(download.Link);
return Task.FromResult<String?>(HttpUtility.UrlDecode(uri.Segments.Last()));
}
private async Task<TorrentClientTorrent> GetInfo(String torrentId) private async Task<TorrentClientTorrent> GetInfo(String torrentId)
{ {

View file

@ -15,4 +15,5 @@ public interface ITorrentClient
Task<String> Unrestrict(String link); Task<String> Unrestrict(String link);
Task<Torrent> UpdateData(Torrent torrent, TorrentClientTorrent? torrentClientTorrent); Task<Torrent> UpdateData(Torrent torrent, TorrentClientTorrent? torrentClientTorrent);
Task<IList<String>?> GetDownloadLinks(Torrent torrent); Task<IList<String>?> GetDownloadLinks(Torrent torrent);
Task<String?> GetFileName(Download download);
} }

View file

@ -4,6 +4,7 @@ using PremiumizeNET;
using RdtClient.Data.Enums; using RdtClient.Data.Enums;
using RdtClient.Data.Models.TorrentClient; using RdtClient.Data.Models.TorrentClient;
using RdtClient.Service.Helpers; using RdtClient.Service.Helpers;
using System.Web;
using Torrent = RdtClient.Data.Models.Data.Torrent; using Torrent = RdtClient.Data.Models.Data.Torrent;
namespace RdtClient.Service.Services.TorrentClients; namespace RdtClient.Service.Services.TorrentClients;
@ -241,6 +242,18 @@ public class PremiumizeTorrentClient(ILogger<PremiumizeTorrentClient> logger, IH
return downloadLinks; return downloadLinks;
} }
public Task<String?> GetFileName(Data.Models.Data.Download download)
{
if (String.IsNullOrWhiteSpace(download.Link))
{
return Task.FromResult<String?>(null);
}
var uri = new Uri(download.Link);
return Task.FromResult<String?>(HttpUtility.UrlDecode(uri.Segments.Last()));
}
private async Task<TorrentClientTorrent> GetInfo(String id) private async Task<TorrentClientTorrent> GetInfo(String id)
{ {
var results = await GetClient().Transfers.ListAsync(); var results = await GetClient().Transfers.ListAsync();

View file

@ -1,4 +1,5 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Web;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using RDNET; using RDNET;
@ -395,6 +396,18 @@ public class RealDebridTorrentClient(ILogger<RealDebridTorrentClient> logger, IH
return null; return null;
} }
public Task<String?> GetFileName(Data.Models.Data.Download download)
{
if (String.IsNullOrWhiteSpace(download.Link))
{
return Task.FromResult<String?>(null);
}
var uri = new Uri(download.Link);
return Task.FromResult<String?>(HttpUtility.UrlDecode(uri.Segments.Last()));
}
private DateTimeOffset? ChangeTimeZone(DateTimeOffset? dateTimeOffset) private DateTimeOffset? ChangeTimeZone(DateTimeOffset? dateTimeOffset)
{ {
if (_offset == null) if (_offset == null)

View file

@ -3,6 +3,7 @@ using Newtonsoft.Json;
using TorBoxNET; using TorBoxNET;
using RdtClient.Data.Enums; using RdtClient.Data.Enums;
using RdtClient.Data.Models.TorrentClient; using RdtClient.Data.Models.TorrentClient;
using System.Web;
namespace RdtClient.Service.Services.TorrentClients; namespace RdtClient.Service.Services.TorrentClients;
@ -300,6 +301,32 @@ public class TorBoxTorrentClient(ILogger<TorBoxTorrentClient> logger, IHttpClien
return files; return files;
} }
public async Task<String?> 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) private DateTimeOffset? ChangeTimeZone(DateTimeOffset? dateTimeOffset)
{ {
if (_offset == null) if (_offset == null)

View file

@ -546,8 +546,10 @@ public class Torrents(
} }
var downloadPath = DownloadPath(download.Torrent!); 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) if (filePath != null)
{ {