Skip to content

Commit

Permalink
Split AppRegex class into TrackersRegex and EndpointsRegex
Browse files Browse the repository at this point in the history
  • Loading branch information
aannenko committed Aug 6, 2024
1 parent f0a7824 commit 6910402
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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}))$";
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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.")]

Check warning on line 15 in src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs

View workflow job for this annotation

GitHub Actions / build

Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved.

Check warning on line 15 in src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs

View workflow job for this annotation

GitHub Actions / build

Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved.

Check warning on line 15 in src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved.

Check warning on line 15 in src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved.
[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.")]

Check warning on line 19 in src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs

View workflow job for this annotation

GitHub Actions / build

Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved.

Check warning on line 19 in src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs

View workflow job for this annotation

GitHub Actions / build

Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved.

Check warning on line 19 in src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved.

Check warning on line 19 in src/TransmissionManager.Api/Endpoints/Dto/TorrentPostRequest.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved.
[RegularExpression(AppRegex.IsCron, MatchTimeoutInMilliseconds = 50)]
[RegularExpression(EndpointsRegex.IsCron, MatchTimeoutInMilliseconds = 50)]
public string? Cron { get; set; }
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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; }
}
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}

Expand All @@ -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));
}

Expand Down

0 comments on commit 6910402

Please sign in to comment.