Skip to content

Commit

Permalink
Add recursive searching for the symlink downloader.
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerfar committed Apr 20, 2024
1 parent 0bfb497 commit dbb502a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.74] - 2024-04-20
### Added
- Added support for symlink recursive searching.

## [2.0.73] - 2024-04-11
### Changed
- Fixed another issue with the symlinker and file resolver.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rdt-client",
"version": "2.0.73",
"version": "2.0.74",
"description": "This is a web interface to manage your torrents on Real-Debrid.",
"main": "index.js",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion server/RdtClient.Data/Models/Internal/DbSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public class DbSettingsDownloadClient
public String? Aria2cDownloadPath { get; set; } = null;

[DisplayName("Rclone mount path (only used for the Symlink Downloader)")]
[Description("Path where Rclone is mounted. Required for Symlink Downloader.")]
[Description("Path where Rclone is mounted. Required for Symlink Downloader. Suffix this path with a * to search subdirectories too.")]
public String RcloneMountPath { get; set; } = "/mnt/rd/";

[DisplayName("Log level")]
Expand Down
2 changes: 0 additions & 2 deletions server/RdtClient.Service/Services/DownloadClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public async Task<String> Start()
};

var result = await Downloader.Download();

await Task.Delay(1000);

return result;
}
Expand Down
60 changes: 43 additions & 17 deletions server/RdtClient.Service/Services/Downloaders/SymlinkDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public async Task<String> Download()
var filePath = new FileInfo(path);

var rcloneMountPath = Settings.Get.DownloadClient.RcloneMountPath.TrimEnd(['\\', '/']);
var searchSubDirectories = rcloneMountPath.EndsWith("*");
rcloneMountPath = rcloneMountPath.TrimEnd('*').TrimEnd(['\\', '/']);

if (!Directory.Exists(rcloneMountPath))
{
throw new($"Mount path {rcloneMountPath} does not exist!");
}

var fileName = filePath.Name;
var fileExtension = filePath.Extension;
var fileNameWithoutExtension = fileName.Replace(fileExtension, "");
Expand All @@ -31,9 +39,9 @@ public async Task<String> Download()

List<String> unWantedExtensions =
[
"zip",
"rar",
"tar"
".zip",
".rar",
".tar"
];

if (unWantedExtensions.Any(m => fileExtension == m))
Expand All @@ -54,7 +62,7 @@ public async Task<String> Download()
var directoryInfo = new DirectoryInfo(searchPath);
while (directoryInfo.Parent != null)
{
potentialFilePaths.Add(directoryInfo.FullName);
potentialFilePaths.Add(directoryInfo.Name);
directoryInfo = directoryInfo.Parent;

if (directoryInfo.FullName.TrimEnd(['\\', '/']) == rcloneMountPath)
Expand All @@ -63,10 +71,12 @@ public async Task<String> Download()
}
}

potentialFilePaths.Add(Path.Combine(rcloneMountPath, fileName));
potentialFilePaths.Add(Path.Combine(rcloneMountPath, fileNameWithoutExtension));
potentialFilePaths.Add(fileName);
potentialFilePaths.Add(fileNameWithoutExtension);

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

FileInfo? file = null;
String? file = null;

for (var retryCount = 0; retryCount < MaxRetries; retryCount++)
{
Expand All @@ -80,16 +90,15 @@ public async Task<String> Download()

_logger.Debug($"Searching {rcloneMountPath} for {fileName} (attempt #{retryCount})...");

foreach (var potentialFilePath in potentialFilePaths)
{
var potentialFilePathWithFileName = Path.Combine(potentialFilePath, fileName);
file = FindFile(rcloneMountPath, potentialFilePaths, fileName);

_logger.Debug($"Searching {potentialFilePathWithFileName}...");
if (file == null && searchSubDirectories)
{
var subDirectories = Directory.GetDirectories(rcloneMountPath, "*.*", SearchOption.TopDirectoryOnly);

if (File.Exists(potentialFilePathWithFileName))
foreach (var subDirectory in subDirectories)
{
file = new(potentialFilePathWithFileName);
break;
FindFile(Path.Combine(rcloneMountPath, subDirectory), potentialFilePaths, fileName);
}
}

Expand Down Expand Up @@ -120,9 +129,9 @@ public async Task<String> Download()
throw new("Could not find file from rclone mount!");
}

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

var result = TryCreateSymbolicLink(file.FullName, destinationPath);
var result = TryCreateSymbolicLink(file, destinationPath);

if (!result)
{
Expand All @@ -131,7 +140,7 @@ public async Task<String> Download()

DownloadComplete?.Invoke(this, new());

return file.FullName;
return file;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -161,6 +170,23 @@ public Task Resume()
return Task.CompletedTask;
}

private String? FindFile(String rootPath, List<String> filePaths, String fileName)
{
foreach (var potentialFilePath in filePaths)
{
var potentialFilePathWithFileName = Path.Combine(rootPath, potentialFilePath, fileName);

_logger.Debug($"Searching {potentialFilePathWithFileName}...");

if (File.Exists(potentialFilePathWithFileName))
{
return potentialFilePathWithFileName;
}
}

return null;
}

private Boolean TryCreateSymbolicLink(String sourcePath, String symlinkPath)
{
try
Expand Down
10 changes: 0 additions & 10 deletions server/RdtClient.Service/Services/TorrentRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,6 @@ public async Task Tick()
return;
}

if (Settings.Get.DownloadClient.Client == Data.Enums.DownloadClient.Symlink)
{
var rcloneMountPath = Settings.Get.DownloadClient.RcloneMountPath;

if (!Directory.Exists(rcloneMountPath))
{
throw new($"Rclone mount path ({rcloneMountPath}) was not found!");
}
}

var sw = new Stopwatch();
sw.Start();

Expand Down

0 comments on commit dbb502a

Please sign in to comment.