Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve path fixes for Alldebrid & only apply them to Alldebrid #652

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace RdtClient.Data.Migrations
{
/// <inheritdoc />
public partial class Torrent_Add_ClientKind : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "ClientKind",
table: "Torrents",
type: "INTEGER",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ClientKind",
table: "Torrents");
}
}
}
3 changes: 3 additions & 0 deletions server/RdtClient.Data/Migrations/DataContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<string>("Category")
.HasColumnType("TEXT");

b.Property<int?>("ClientKind")
.HasColumnType("INTEGER");

b.Property<DateTimeOffset?>("Completed")
.HasColumnType("TEXT");

Expand Down
9 changes: 9 additions & 0 deletions server/RdtClient.Data/Models/Data/Torrent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class Torrent
[InverseProperty("Torrent")]
public IList<Download> Downloads { get; set; } = [];

public TorrentClientKind? ClientKind { get; set; }
public String? RdId { get; set; }
public String? RdName { get; set; }
public Int64? RdSize { get; set; }
Expand Down Expand Up @@ -92,4 +93,12 @@ public IList<String> ManualFiles
return DownloadManualFiles.Split(",");
}
}

public enum TorrentClientKind
{
AllDebrid,
Premiumize,
RealDebrid,
TorBox
}
}
22 changes: 11 additions & 11 deletions server/RdtClient.Service/Helpers/DownloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public static class DownloadHelper
if (matchingTorrentFiles.Count > 0)
{
var matchingTorrentFile = matchingTorrentFiles[0];

var subPath = Path.GetDirectoryName(matchingTorrentFile.Path);

if (!String.IsNullOrWhiteSpace(subPath))
Expand All @@ -49,22 +48,23 @@ public static class DownloadHelper

torrentPath = Path.Combine(torrentPath, subPath);
}
else if (torrent.Files.Count == 1)
{
if (directory != fileName)
{
throw new($"Torrent path {torrentPath} does not match file name {fileName}. This is a requirement for single file torrents.");
}

return torrentPath;
}
}
}
else if (torrent.Files.Count == 1)
{
// Debrid servers such as RealDebrid store single file torrents in a subfolder, but AllDebrid doesn't.
// We should replicate this behavior so that both folder structures are equal.
// See issue: https://github.com/rogerfar/rdt-client/issues/648
if (torrent.ClientKind != Torrent.TorrentClientKind.AllDebrid)
{
torrentPath = Path.Combine(downloadPath, directory);
}

var torrentFile = torrent.Files[0];
var subPath = Path.GetDirectoryName(torrentFile.Path);


// What we think is a single file torrent may also be a folder with a single file in it.
// So make sure we handle that here. If this is not the case, torrentPath will be empty below.
if (!String.IsNullOrWhiteSpace(subPath))
{
subPath = subPath.Trim('/').Trim('\\');
Expand Down
2 changes: 1 addition & 1 deletion server/RdtClient.Service/Services/DownloadClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task<String> Start()
Data.Enums.DownloadClient.Internal => new InternalDownloader(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.Symlink => new SymlinkDownloader(download.Link, filePath, downloadPath),
Data.Enums.DownloadClient.Symlink => new SymlinkDownloader(download.Link, filePath, downloadPath, torrent.ClientKind),
_ => throw new($"Unknown download client {Type}")
};

Expand Down
34 changes: 28 additions & 6 deletions server/RdtClient.Service/Services/Downloaders/SymlinkDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using RdtClient.Service.Helpers;
using RdtClient.Data.Models.Data;
using RdtClient.Service.Helpers;
using Serilog;

namespace RdtClient.Service.Services.Downloaders;

public class SymlinkDownloader(String uri, String destinationPath, String path) : IDownloader
public class SymlinkDownloader(String uri, String destinationPath, String path, Torrent.TorrentClientKind? clientKind) : IDownloader
{
public event EventHandler<DownloadCompleteEventArgs>? DownloadComplete;
public event EventHandler<DownloadProgressEventArgs>? DownloadProgress;
Expand All @@ -16,7 +17,7 @@ public class SymlinkDownloader(String uri, String destinationPath, String path)

public async Task<String> Download()
{
_logger.Debug($"Starting symlink resolving of {uri}, writing to path: {path}");
_logger.Debug($"Starting symlink resolving of {path} (uri = {uri}), writing to path: {destinationPath}");

try
{
Expand Down Expand Up @@ -56,8 +57,28 @@ public async Task<String> Download()
BytesTotal = 0,
Speed = 0
});

String? file = null;

// When resolving symlinks for AllDebrid, we know the exact file path, so we can skip the search.
if (clientKind == Torrent.TorrentClientKind.AllDebrid)
{
var potentialFilePath = Path.Combine(rcloneMountPath, path);

// Make sure the file exists before making any assumptions.
// If this somehow fails, fallback to the search below.
Sculas marked this conversation as resolved.
Show resolved Hide resolved
if (File.Exists(potentialFilePath))
{
_logger.Debug($"Found file {path} at {potentialFilePath} using direct search");
file = potentialFilePath;
goto skipFileSearch;
}

// Log if the file wasn't found and continue searching.
_logger.Warning($"Expected file {path} to be at {potentialFilePath} but it wasn't found. Continuing search (this will probably fail).");
}

var potentialFilePaths = new List<String>();
var potentialFilePaths = new List<String> { searchPath };

var directoryInfo = new DirectoryInfo(searchPath);
while (directoryInfo.Parent != null)
Expand All @@ -78,8 +99,6 @@ public async Task<String> Download()

potentialFilePaths = potentialFilePaths.Distinct().ToList();

String? file = null;

for (var retryCount = 0; retryCount < MaxRetries; retryCount++)
{
DownloadProgress?.Invoke(this,
Expand Down Expand Up @@ -135,6 +154,9 @@ public async Task<String> Download()

throw new("Could not find file from rclone mount!");
}

// Used by Alldebrid since we know the exact file path.
skipFileSearch:

_logger.Debug($"Creating symbolic link from {file} to {destinationPath}");

Expand Down
9 changes: 8 additions & 1 deletion server/RdtClient.Service/Services/QBittorrent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,14 @@ public async Task<IList<TorrentInfo>> TorrentInfo()
var torrentPath = downloadPath;
if (!String.IsNullOrWhiteSpace(torrent.RdName))
{
torrentPath = Path.Combine(downloadPath, torrent.RdName) + Path.DirectorySeparatorChar;
// Alldebrid stores single file torrents at the root folder.
if (torrent is { ClientKind: Torrent.TorrentClientKind.AllDebrid, Files.Count: 1 })
{
torrentPath = Path.Combine(downloadPath, torrent.Files[0].Path);
} else
{
torrentPath = Path.Combine(downloadPath, torrent.RdName) + Path.DirectorySeparatorChar;
}
}

Int64 bytesDone = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public async Task<Torrent> UpdateData(Torrent torrent, TorrentClientTorrent? tor
torrent.RdFiles = JsonConvert.SerializeObject(torrentClientTorrent.Files);
}

torrent.ClientKind = Torrent.TorrentClientKind.AllDebrid;
torrent.RdHost = torrentClientTorrent.Host;
torrent.RdSplit = torrentClientTorrent.Split;
torrent.RdProgress = torrentClientTorrent.Progress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public async Task<Torrent> UpdateData(Torrent torrent, TorrentClientTorrent? tor
torrent.RdFiles = JsonConvert.SerializeObject(torrentClientTorrent.Files);
}

torrent.ClientKind = Torrent.TorrentClientKind.Premiumize;
torrent.RdHost = torrentClientTorrent.Host;
torrent.RdSplit = torrentClientTorrent.Split;
torrent.RdProgress = torrentClientTorrent.Progress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public async Task<String> Unrestrict(String link)
torrent.RdFiles = JsonConvert.SerializeObject(torrentClientTorrent.Files);
}

torrent.ClientKind = Data.Models.Data.Torrent.TorrentClientKind.RealDebrid;
torrent.RdHost = torrentClientTorrent.Host;
torrent.RdSplit = torrentClientTorrent.Split;
torrent.RdProgress = torrentClientTorrent.Progress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using RdtClient.Data.Enums;
using RdtClient.Data.Models.TorrentClient;
using System.Web;
using RdtClient.Data.Models.Data;

namespace RdtClient.Service.Services.TorrentClients;

Expand Down Expand Up @@ -167,7 +168,7 @@ public async Task<IList<TorrentClientAvailableFile>> GetAvailableFiles(String ha
return [];
}

public Task SelectFiles(Data.Models.Data.Torrent torrent)
public Task SelectFiles(Torrent torrent)
{
return Task.CompletedTask;
}
Expand All @@ -194,7 +195,7 @@ public async Task<String> Unrestrict(String link)
return result.Data!;
}

public async Task<Data.Models.Data.Torrent> UpdateData(Data.Models.Data.Torrent torrent, TorrentClientTorrent? torrentClientTorrent)
public async Task<Torrent> UpdateData(Torrent torrent, TorrentClientTorrent? torrentClientTorrent)
{
try
{
Expand Down Expand Up @@ -229,6 +230,7 @@ public async Task<String> Unrestrict(String link)
torrent.RdFiles = JsonConvert.SerializeObject(rdTorrent.Files);
}

torrent.ClientKind = Torrent.TorrentClientKind.TorBox;
torrent.RdHost = rdTorrent.Host;
torrent.RdSplit = rdTorrent.Split;
torrent.RdProgress = rdTorrent.Progress;
Expand Down Expand Up @@ -280,7 +282,7 @@ public async Task<String> Unrestrict(String link)
return torrent;
}

public async Task<IList<String>?> GetDownloadLinks(Data.Models.Data.Torrent torrent)
public async Task<IList<String>?> GetDownloadLinks(Torrent torrent)
{
var files = new List<String>();

Expand Down