From 6910402de1e870ebb2188b4bba474f8744de1bdf Mon Sep 17 00:00:00 2001 From: Andrii Annenko Date: Tue, 6 Aug 2024 19:04:32 +0200 Subject: [PATCH] Split AppRegex class into TrackersRegex and EndpointsRegex --- .../Endpoints/Constants/EndpointsRegex.cs | 8 ++++++++ .../Endpoints/Dto/TorrentPostRequest.cs | 7 ++++--- .../Endpoints/Dto/TorrentPutRequest.cs | 7 ++++--- .../Constants/TrackersRegex.cs} | 8 ++------ .../Trackers/Options/MagnetUriRetrieverOptions.cs | 4 ++-- .../Trackers/Services/MagnetUriRetriever.cs | 12 ++++++------ 6 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 src/TransmissionManager.Api/Endpoints/Constants/EndpointsRegex.cs rename src/TransmissionManager.Api/{Utilities/AppRegex.cs => Trackers/Constants/TrackersRegex.cs} (51%) diff --git a/src/TransmissionManager.Api/Endpoints/Constants/EndpointsRegex.cs b/src/TransmissionManager.Api/Endpoints/Constants/EndpointsRegex.cs new file mode 100644 index 0000000..2d1f812 --- /dev/null +++ b/src/TransmissionManager.Api/Endpoints/Constants/EndpointsRegex.cs @@ -0,0 +1,8 @@ +namespace TransmissionManager.Api.Endpoints.Constants; + +public static partial class EndpointsRegex +{ + // language=regex + internal const string IsCron = + @"^((\*(\/\d{1,2})?|\d{1,2}(\/\d{1,2})?|(\d{1,2}-\d{1,2})(\/\d{1,2})?|((\d{1,2},)+\d{1,2}))\s){4}(\*(\/\d{1,2})?|\d{1,2}(\/\d{1,2})?|(\d{1,2}-\d{1,2})(\/\d{1,2})?|((\d{1,2},)+\d{1,2}))$"; +} diff --git a/src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs b/src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs index 29b9acb..f5cbf57 100644 --- a/src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs +++ b/src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; -using TransmissionManager.Api.Utilities; +using TransmissionManager.Api.Endpoints.Constants; +using TransmissionManager.Api.Trackers.Constants; namespace TransmissionManager.Api.Endpoints.Dto; @@ -12,10 +13,10 @@ public sealed class TorrentPostRequest public required string WebPageUri { get; set; } [MinLength(1, ErrorMessage = $"The property {nameof(MagnetRegexPattern)} cannot be an empty string.")] - [RegularExpression(AppRegex.IsFindMagnet, MatchTimeoutInMilliseconds = 50)] + [RegularExpression(TrackersRegex.IsFindMagnet, MatchTimeoutInMilliseconds = 50)] public string? MagnetRegexPattern { get; set; } [MinLength(1, ErrorMessage = $"The property {nameof(Cron)} cannot be an empty string.")] - [RegularExpression(AppRegex.IsCron, MatchTimeoutInMilliseconds = 50)] + [RegularExpression(EndpointsRegex.IsCron, MatchTimeoutInMilliseconds = 50)] public string? Cron { get; set; } } diff --git a/src/TransmissionManager.Api/Endpoints/Dto/TorrentPutRequest.cs b/src/TransmissionManager.Api/Endpoints/Dto/TorrentPutRequest.cs index ea2ad01..cdda61c 100644 --- a/src/TransmissionManager.Api/Endpoints/Dto/TorrentPutRequest.cs +++ b/src/TransmissionManager.Api/Endpoints/Dto/TorrentPutRequest.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; -using TransmissionManager.Api.Utilities; +using TransmissionManager.Api.Endpoints.Constants; +using TransmissionManager.Api.Trackers.Constants; namespace TransmissionManager.Api.Endpoints.Dto; @@ -9,9 +10,9 @@ public sealed class TorrentPutRequest public string? DownloadDir { get; set; } - [RegularExpression(AppRegex.IsFindMagnet, MatchTimeoutInMilliseconds = 50)] // empty strings are considered valid + [RegularExpression(TrackersRegex.IsFindMagnet, MatchTimeoutInMilliseconds = 50)] // empty strings are considered valid public string? MagnetRegexPattern { get; set; } - [RegularExpression(AppRegex.IsCron, MatchTimeoutInMilliseconds = 50)] // empty strings are considered valid + [RegularExpression(EndpointsRegex.IsCron, MatchTimeoutInMilliseconds = 50)] // empty strings are considered valid public string? Cron { get; set; } } diff --git a/src/TransmissionManager.Api/Utilities/AppRegex.cs b/src/TransmissionManager.Api/Trackers/Constants/TrackersRegex.cs similarity index 51% rename from src/TransmissionManager.Api/Utilities/AppRegex.cs rename to src/TransmissionManager.Api/Trackers/Constants/TrackersRegex.cs index 753763e..a945fd6 100644 --- a/src/TransmissionManager.Api/Utilities/AppRegex.cs +++ b/src/TransmissionManager.Api/Trackers/Constants/TrackersRegex.cs @@ -1,13 +1,9 @@ using System.Text.RegularExpressions; -namespace TransmissionManager.Api.Utilities; +namespace TransmissionManager.Api.Trackers.Constants; -public static partial class AppRegex +public static partial class TrackersRegex { - // language=regex - internal const string IsCron = - @"^((\*(\/\d{1,2})?|\d{1,2}(\/\d{1,2})?|(\d{1,2}-\d{1,2})(\/\d{1,2})?|((\d{1,2},)+\d{1,2}))\s){4}(\*(\/\d{1,2})?|\d{1,2}(\/\d{1,2})?|(\d{1,2}-\d{1,2})(\/\d{1,2})?|((\d{1,2},)+\d{1,2}))$"; - // language=regex internal const string IsFindMagnet = $@"^.*\(\?<{MagnetGroup}>magnet:\\\?.*\).*$"; internal const string MagnetGroup = "magnet"; diff --git a/src/TransmissionManager.Api/Trackers/Options/MagnetUriRetrieverOptions.cs b/src/TransmissionManager.Api/Trackers/Options/MagnetUriRetrieverOptions.cs index 4ce065e..228f074 100644 --- a/src/TransmissionManager.Api/Trackers/Options/MagnetUriRetrieverOptions.cs +++ b/src/TransmissionManager.Api/Trackers/Options/MagnetUriRetrieverOptions.cs @@ -1,12 +1,12 @@ using System.ComponentModel.DataAnnotations; -using TransmissionManager.Api.Utilities; +using TransmissionManager.Api.Trackers.Constants; namespace TransmissionManager.Api.Trackers.Options; public sealed class MagnetUriRetrieverOptions { [Required] - [RegularExpression(AppRegex.IsFindMagnet)] + [RegularExpression(TrackersRegex.IsFindMagnet)] public required string DefaultRegexPattern { get; set; } [Required] diff --git a/src/TransmissionManager.Api/Trackers/Services/MagnetUriRetriever.cs b/src/TransmissionManager.Api/Trackers/Services/MagnetUriRetriever.cs index 938a053..6dcadaf 100644 --- a/src/TransmissionManager.Api/Trackers/Services/MagnetUriRetriever.cs +++ b/src/TransmissionManager.Api/Trackers/Services/MagnetUriRetriever.cs @@ -1,7 +1,7 @@ using Microsoft.Extensions.Options; using System.Text.RegularExpressions; +using TransmissionManager.Api.Trackers.Constants; using TransmissionManager.Api.Trackers.Options; -using TransmissionManager.Api.Utilities; namespace TransmissionManager.Api.Trackers.Services; @@ -23,7 +23,7 @@ public sealed partial class MagnetUriRetriever( while ((line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false)) is not null) { var match = regex.Match(line); - if (match.Success && match.Groups.TryGetValue(AppRegex.MagnetGroup, out var group)) + if (match.Success && match.Groups.TryGetValue(TrackersRegex.MagnetGroup, out var group)) return group.Value; } @@ -36,17 +36,17 @@ private Regex GetMagnetSearchRegexWithValidation(string? regexPattern) if (regexPattern is null) { finalRegexPattern = options.CurrentValue.DefaultRegexPattern; - if (finalRegexPattern is null || !AppRegex.IsFindMagnetRegex().IsMatch(finalRegexPattern)) + if (finalRegexPattern is null || !TrackersRegex.IsFindMagnetRegex().IsMatch(finalRegexPattern)) throw new InvalidOperationException( $"Invalid {nameof(options.CurrentValue.DefaultRegexPattern)} config value. " + - $"The value must match '{AppRegex.IsFindMagnet}'."); + $"The value must match '{TrackersRegex.IsFindMagnet}'."); } else { finalRegexPattern = regexPattern; - if (!AppRegex.IsFindMagnetRegex().IsMatch(finalRegexPattern)) + if (!TrackersRegex.IsFindMagnetRegex().IsMatch(finalRegexPattern)) throw new ArgumentException( - $"Invalid magnet-matching regex provided. The value must match '{AppRegex.IsFindMagnet}'.", + $"Invalid magnet-matching regex provided. The value must match '{TrackersRegex.IsFindMagnet}'.", nameof(regexPattern)); }