diff --git a/server/RdtClient.Data/Models/Internal/DbSettings.cs b/server/RdtClient.Data/Models/Internal/DbSettings.cs index 09d9360..e1769fc 100644 --- a/server/RdtClient.Data/Models/Internal/DbSettings.cs +++ b/server/RdtClient.Data/Models/Internal/DbSettings.cs @@ -70,6 +70,10 @@ Supports the following parameters: [DisplayName("Authentication Type")] [Description("How to authenticate with the client. WARNING: when set to None anyone with access to the URL can use the client without any credentials.")] public AuthenticationType AuthenticationType { get; set; } = AuthenticationType.UserNamePassword; + + [DisplayName("Copy added torrent files")] + [Description("When a torrent file or magnet is added, create a copy in this directory.")] + public String? CopyAddedTorrents { get; set; } = null; } public class DbSettingsDownloadClient diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index f366547..f62f72d 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -1,7 +1,6 @@ using System.Diagnostics; using System.Globalization; using System.Text; -using System.IO; using System.Text.Json; using Microsoft.Extensions.Logging; using MonoTorrent; @@ -134,6 +133,23 @@ public class Torrents Log($"Adding {hash} magnet link {magnetLink}", newTorrent); + if (!String.IsNullOrWhiteSpace(Settings.Get.General.CopyAddedTorrents)) + { + try + { + if (!Directory.Exists(Settings.Get.General.CopyAddedTorrents)) + { + Directory.CreateDirectory(Settings.Get.General.CopyAddedTorrents); + } + + await File.WriteAllTextAsync(Path.Combine(Settings.Get.General.CopyAddedTorrents, $"{torrent.Hash}.magnet"), magnetLink); + } + catch (Exception ex) + { + _logger.LogError(ex, $"Unable to create torrent blackhole directory: {Settings.Get.General.CopyAddedTorrents}: {ex.Message}"); + } + } + return newTorrent; } @@ -159,11 +175,22 @@ public class Torrents var newTorrent = await Add(id, hash, fileAsBase64, true, torrent); - if (!Directory.Exists("/data/db/blackhole/")) + if (!String.IsNullOrWhiteSpace(Settings.Get.General.CopyAddedTorrents)) { - Directory.CreateDirectory("/data/db/blackhole/"); + try + { + if (!Directory.Exists(Settings.Get.General.CopyAddedTorrents)) + { + Directory.CreateDirectory(Settings.Get.General.CopyAddedTorrents); + } + + await File.WriteAllBytesAsync(Path.Combine(Settings.Get.General.CopyAddedTorrents, $"{torrent.Hash}.torrent"), bytes); + } + catch (Exception ex) + { + _logger.LogError(ex, $"Unable to create torrent blackhole directory: {Settings.Get.General.CopyAddedTorrents}: {ex.Message}"); + } } - File.WriteAllBytes ("/data/db/blackhole/" + hash + ".torrent", bytes); return newTorrent; }