Use static GetSymlinkPath on AllDebridTorrentClient to get the symlink (since the behaviour is unique to the debrid service)

This commit is contained in:
Cucumberrbob 2025-01-29 12:11:25 +00:00
parent beb0299a0b
commit 82f0bb8bbe
No known key found for this signature in database
GPG key ID: 2B935C47401C3614
3 changed files with 85 additions and 31 deletions

View file

@ -16,20 +16,15 @@ public static class DownloadHelper
var directory = RemoveInvalidPathChars(torrent.RdName); var directory = RemoveInvalidPathChars(torrent.RdName);
var uri = new Uri(fileUrl);
var torrentPath = Path.Combine(downloadPath, directory); var torrentPath = Path.Combine(downloadPath, directory);
var fileName = download.FileName; var fileName = GetFileName(download);
if (String.IsNullOrWhiteSpace(fileName)) if (fileName == null)
{ {
fileName = uri.Segments.Last(); return null;
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(); var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList();
if (matchingTorrentFiles.Count > 0) if (matchingTorrentFiles.Count > 0)
@ -98,7 +93,24 @@ public static class DownloadHelper
return filePath; return filePath;
} }
private static String RemoveInvalidPathChars(String path) public static String? GetFileName(Download download)
{
if (String.IsNullOrWhiteSpace(download.Link))
{
return null;
}
var fileName = download.FileName;
if (String.IsNullOrWhiteSpace(fileName))
{
fileName = HttpUtility.UrlDecode(new Uri(download.Link).Segments.Last());
}
return FileHelper.RemoveInvalidFileNameChars(fileName);
}
public static String RemoveInvalidPathChars(String path)
{ {
return String.Concat(path.Split(Path.GetInvalidPathChars())); return String.Concat(path.Split(Path.GetInvalidPathChars()));
} }

View file

@ -1,6 +1,7 @@
using RdtClient.Data.Models.Data; using RdtClient.Data.Models.Data;
using RdtClient.Service.Helpers; using RdtClient.Service.Helpers;
using RdtClient.Service.Services.Downloaders; using RdtClient.Service.Services.Downloaders;
using RdtClient.Service.Services.TorrentClients;
namespace RdtClient.Service.Services; namespace RdtClient.Service.Services;
@ -44,6 +45,12 @@ public class DownloadClient(Download download, Torrent torrent, String destinati
throw new("Invalid download path"); throw new("Invalid download path");
} }
var symlinkPath = torrent.ClientKind switch
{
Torrent.TorrentClientKind.AllDebrid => AllDebridTorrentClient.GetSymlinkPath(torrent, download),
_ => downloadPath
};
Type = torrent.DownloadClient; Type = torrent.DownloadClient;
if (Type != Data.Enums.DownloadClient.Symlink) if (Type != Data.Enums.DownloadClient.Symlink)
@ -56,7 +63,7 @@ public class DownloadClient(Download download, Torrent torrent, String destinati
Data.Enums.DownloadClient.Internal => new InternalDownloader(download.Link, filePath), Data.Enums.DownloadClient.Internal => new InternalDownloader(download.Link, filePath),
Data.Enums.DownloadClient.Bezzad => new BezzadDownloader(download.Link, filePath), Data.Enums.DownloadClient.Bezzad => new BezzadDownloader(download.Link, filePath),
Data.Enums.DownloadClient.Aria2c => new Aria2cDownloader(download.RemoteId, download.Link, filePath, downloadPath, category), Data.Enums.DownloadClient.Aria2c => new Aria2cDownloader(download.RemoteId, download.Link, filePath, downloadPath, category),
Data.Enums.DownloadClient.Symlink => new SymlinkDownloader(download.Link, filePath, downloadPath, torrent.ClientKind), Data.Enums.DownloadClient.Symlink => new SymlinkDownloader(download.Link, filePath, symlinkPath, torrent.ClientKind),
_ => throw new($"Unknown download client {Type}") _ => throw new($"Unknown download client {Type}")
}; };

View file

@ -6,6 +6,7 @@ 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 System.Web;
using RdtClient.Data.Models.Data;
using File = AllDebridNET.File; using File = AllDebridNET.File;
using LogLevel = Microsoft.Extensions.Logging.LogLevel; using LogLevel = Microsoft.Extensions.Logging.LogLevel;
using Torrent = RdtClient.Data.Models.Data.Torrent; using Torrent = RdtClient.Data.Models.Data.Torrent;
@ -58,17 +59,18 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
OriginalBytes = torrent.Size, OriginalBytes = torrent.Size,
Host = null, Host = null,
Split = 0, Split = 0,
Progress = (Int64) Math.Round(torrent.Downloaded * 100.0 / torrent.Size), Progress = (Int64)Math.Round(torrent.Downloaded * 100.0 / torrent.Size),
Status = torrent.Status, Status = torrent.Status,
StatusCode = torrent.StatusCode, StatusCode = torrent.StatusCode,
Added = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(torrent.UploadDate), Added = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(torrent.UploadDate),
Files = torrent.Links.Select((m, i) => new TorrentClientFile Files = torrent.Links.Select((m, i) => new TorrentClientFile
{ {
Path = GetFiles(m.Files), Path = GetFiles(m.Files),
Bytes = m.Size, Bytes = m.Size,
Id = i, Id = i,
Selected = true, Selected = true,
}).ToList(), })
.ToList(),
Links = torrent.Links.Select(m => m.LinkUrl.ToString()).ToList(), Links = torrent.Links.Select(m => m.LinkUrl.ToString()).ToList(),
Ended = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(torrent.CompletionDate), Ended = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(torrent.CompletionDate),
Speed = torrent.DownloadSpeed, Speed = torrent.DownloadSpeed,
@ -79,6 +81,7 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
public async Task<IList<TorrentClientTorrent>> GetTorrents() public async Task<IList<TorrentClientTorrent>> GetTorrents()
{ {
var results = await GetClient().Magnet.StatusAllAsync(); var results = await GetClient().Magnet.StatusAllAsync();
return results.Select(Map).ToList(); return results.Select(Map).ToList();
} }
@ -251,9 +254,11 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
Log($"Using regular expression {torrent.IncludeRegex} to include only files matching this regex", torrent); Log($"Using regular expression {torrent.IncludeRegex} to include only files matching this regex", torrent);
var newLinks = new List<Link>(); var newLinks = new List<Link>();
foreach (var link in links) foreach (var link in links)
{ {
var path = GetFiles(link.Files); var path = GetFiles(link.Files);
if (Regex.IsMatch(path, torrent.IncludeRegex)) if (Regex.IsMatch(path, torrent.IncludeRegex))
{ {
Log($"* Including {path}", torrent); Log($"* Including {path}", torrent);
@ -274,9 +279,11 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
Log($"Using regular expression {torrent.IncludeRegex} to ignore files matching this regex", torrent); Log($"Using regular expression {torrent.IncludeRegex} to ignore files matching this regex", torrent);
var newLinks = new List<Link>(); var newLinks = new List<Link>();
foreach (var link in links) foreach (var link in links)
{ {
var path = GetFiles(link.Files); var path = GetFiles(link.Files);
if (!Regex.IsMatch(path, torrent.ExcludeRegex)) if (!Regex.IsMatch(path, torrent.ExcludeRegex))
{ {
Log($"* Including {path}", torrent); Log($"* Including {path}", torrent);
@ -314,6 +321,7 @@ 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(String downloadUrl) public Task<String> GetFileName(String downloadUrl)
{ {
if (String.IsNullOrWhiteSpace(downloadUrl)) if (String.IsNullOrWhiteSpace(downloadUrl))
@ -402,4 +410,31 @@ public class AllDebridTorrentClient(ILogger<AllDebridTorrentClient> logger, IHtt
logger.LogDebug(message); logger.LogDebug(message);
} }
public static String? GetSymlinkPath(Torrent torrent, Download download)
{
var fileName = DownloadHelper.GetFileName(download);
if (fileName == null || torrent.RdName == null)
{
return null;
}
var directory = DownloadHelper.RemoveInvalidPathChars(torrent.RdName);
var matchingTorrentFiles = torrent.Files.Where(m => m.Path.EndsWith(fileName)).Where(m => !String.IsNullOrWhiteSpace(m.Path)).ToList();
if (matchingTorrentFiles.Count == 0)
{
throw new Exception($"Could not find file {fileName} in {torrent.RdName}");
}
// Torrents with a single file in them don't need to have the `RdName` added
if (torrent.Files.Count == 1)
{
return matchingTorrentFiles[0].Path;
}
return Path.Combine(directory, matchingTorrentFiles[0].Path);
}
} }