Add download cleaner and dry run (#58)

This commit is contained in:
Marius Nechifor
2025-02-16 03:09:07 +02:00
parent 19b3675701
commit 596a5aed8d
87 changed files with 2507 additions and 413 deletions

View File

@@ -11,9 +11,9 @@ public abstract record ArrConfig
public required List<ArrInstance> Instances { get; init; }
}
public record Block
public readonly record struct Block
{
public BlocklistType Type { get; set; }
public BlocklistType Type { get; init; }
public string? Path { get; set; }
public string? Path { get; init; }
}

View File

@@ -0,0 +1,45 @@
using Common.Exceptions;
using Microsoft.Extensions.Configuration;
namespace Common.Configuration.DownloadCleaner;
public sealed record Category : IConfig
{
public required string Name { get; init; }
/// <summary>
/// Max ratio before removing a download.
/// </summary>
[ConfigurationKeyName("MAX_RATIO")]
public required double MaxRatio { get; init; } = -1;
/// <summary>
/// Min number of hours to seed before removing a download, if the ratio has been met.
/// </summary>
[ConfigurationKeyName("MIN_SEED_TIME")]
public required double MinSeedTime { get; init; } = 0;
/// <summary>
/// Number of hours to seed before removing a download.
/// </summary>
[ConfigurationKeyName("MAX_SEED_TIME")]
public required double MaxSeedTime { get; init; } = -1;
public void Validate()
{
if (string.IsNullOrWhiteSpace(Name))
{
throw new ValidationException($"{nameof(Name)} can not be empty");
}
if (MaxRatio < 0 && MaxSeedTime < 0)
{
throw new ValidationException($"both {nameof(MaxRatio)} and {nameof(MaxSeedTime)} are disabled");
}
if (MinSeedTime < 0)
{
throw new ValidationException($"{nameof(MinSeedTime)} can not be negative");
}
}
}

View File

@@ -0,0 +1,36 @@
using Common.Exceptions;
using Microsoft.Extensions.Configuration;
namespace Common.Configuration.DownloadCleaner;
public sealed record DownloadCleanerConfig : IJobConfig
{
public const string SectionName = "DownloadCleaner";
public bool Enabled { get; init; }
public List<Category>? Categories { get; init; }
[ConfigurationKeyName("DELETE_PRIVATE")]
public bool DeletePrivate { get; set; }
public void Validate()
{
if (!Enabled)
{
return;
}
if (Categories?.Count is null or 0)
{
throw new ValidationException("no categories configured");
}
if (Categories?.GroupBy(x => x.Name).Any(x => x.Count() > 1) is true)
{
throw new ValidationException("duplicated categories found");
}
Categories?.ForEach(x => x.Validate());
}
}

View File

@@ -1,4 +1,6 @@
namespace Common.Configuration.DownloadClient;
using Common.Exceptions;
namespace Common.Configuration.DownloadClient;
public sealed record DelugeConfig : IConfig
{
@@ -12,7 +14,7 @@ public sealed record DelugeConfig : IConfig
{
if (Url is null)
{
throw new ArgumentNullException(nameof(Url));
throw new ValidationException($"{nameof(Url)} is empty");
}
}
}

View File

@@ -1,4 +1,6 @@
namespace Common.Configuration.DownloadClient;
using Common.Exceptions;
namespace Common.Configuration.DownloadClient;
public sealed class QBitConfig : IConfig
{
@@ -14,7 +16,7 @@ public sealed class QBitConfig : IConfig
{
if (Url is null)
{
throw new ArgumentNullException(nameof(Url));
throw new ValidationException($"{nameof(Url)} is empty");
}
}
}

View File

@@ -1,4 +1,6 @@
namespace Common.Configuration.DownloadClient;
using Common.Exceptions;
namespace Common.Configuration.DownloadClient;
public record TransmissionConfig : IConfig
{
@@ -14,7 +16,7 @@ public record TransmissionConfig : IConfig
{
if (Url is null)
{
throw new ArgumentNullException(nameof(Url));
throw new ValidationException($"{nameof(Url)} is empty");
}
}
}

View File

@@ -0,0 +1,9 @@
using Microsoft.Extensions.Configuration;
namespace Common.Configuration.General;
public sealed record DryRunConfig
{
[ConfigurationKeyName("DRY_RUN")]
public bool IsDryRun { get; init; }
}

View File

@@ -1,8 +1,9 @@
using Microsoft.Extensions.Configuration;
using Common.Exceptions;
using Microsoft.Extensions.Configuration;
namespace Common.Configuration.General;
public class HttpConfig : IConfig
public sealed record HttpConfig : IConfig
{
[ConfigurationKeyName("HTTP_MAX_RETRIES")]
public ushort MaxRetries { get; init; }
@@ -14,7 +15,7 @@ public class HttpConfig : IConfig
{
if (Timeout is 0)
{
throw new ArgumentException("HTTP_TIMEOUT must be greater than 0");
throw new ValidationException("HTTP_TIMEOUT must be greater than 0");
}
}
}

View File

@@ -7,4 +7,6 @@ public sealed class TriggersConfig
public required string QueueCleaner { get; init; }
public required string ContentBlocker { get; init; }
public required string DownloadCleaner { get; init; }
}

View File

@@ -10,10 +10,13 @@ public abstract record NotificationConfig
[ConfigurationKeyName("ON_STALLED_STRIKE")]
public bool OnStalledStrike { get; init; }
[ConfigurationKeyName("ON_QUEUE_ITEM_DELETE")]
public bool OnQueueItemDelete { get; init; }
[ConfigurationKeyName("ON_QUEUE_ITEM_DELETED")]
public bool OnQueueItemDeleted { get; init; }
[ConfigurationKeyName("ON_DOWNLOAD_CLEANED")]
public bool OnDownloadCleaned { get; init; }
public bool IsEnabled => OnImportFailedStrike || OnStalledStrike || OnQueueItemDelete;
public bool IsEnabled => OnImportFailedStrike || OnStalledStrike || OnQueueItemDeleted || OnDownloadCleaned;
public abstract bool IsValid();
}