This commit is contained in:
XorogVEVO 2021-11-28 11:28:59 +01:00
parent 800ec1a349
commit 72095914a9
2 changed files with 41 additions and 20 deletions

25
.editorconfig Normal file
View file

@ -0,0 +1,25 @@
[*.cs]
# CS1998: Async method lacks 'await' operators and will run synchronously
dotnet_diagnostic.CS1998.severity = none
# IDE0055: Fix formatting
dotnet_diagnostic.IDE0055.severity = none
# CA1822: Mark members as static
dotnet_diagnostic.CA1822.severity = none
# Default severity for all analyzer diagnostics
dotnet_analyzer_diagnostic.severity = none
# IDE0063: Use simple 'using' statement
csharp_prefer_simple_using_statement = false
# IDE0057: Use range operator
csharp_style_prefer_range_operator = false
# IDE0047: Remove unnecessary parentheses
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
# IDE0047: Remove unnecessary parentheses
dotnet_diagnostic.IDE0047.severity = none

View file

@ -106,15 +106,15 @@ public static class UniversalExtensions
/// <returns>The URL the redirect leads to</returns> /// <returns>The URL the redirect leads to</returns>
public static async Task<string> UnshortenUrl(string url) public static async Task<string> UnshortenUrl(string url)
{ {
HttpClient client = new HttpClient(new HttpClientHandler() { AllowAutoRedirect = false }); HttpClient client = new(new HttpClientHandler() { AllowAutoRedirect = false });
var request = await client.GetAsync(url); var request = await client.GetAsync(url);
if (request.StatusCode != HttpStatusCode.Found || if (request.StatusCode is HttpStatusCode.Found
request.StatusCode != HttpStatusCode.Redirect || or HttpStatusCode.Redirect
request.StatusCode != HttpStatusCode.RedirectKeepVerb || or HttpStatusCode.RedirectKeepVerb
request.StatusCode != HttpStatusCode.RedirectMethod || or HttpStatusCode.RedirectMethod
request.StatusCode != HttpStatusCode.PermanentRedirect || or HttpStatusCode.PermanentRedirect
request.StatusCode != HttpStatusCode.TemporaryRedirect) or HttpStatusCode.TemporaryRedirect)
{ {
if (request.Headers is not null && request.Headers.Location is not null) if (request.Headers is not null && request.Headers.Location is not null)
return await UnshortenUrl(request.Headers.Location.AbsoluteUri); return await UnshortenUrl(request.Headers.Location.AbsoluteUri);
@ -175,12 +175,10 @@ public static class UniversalExtensions
/// <returns></returns> /// <returns></returns>
public static string ComputeSHA256Hash(string filePath) public static string ComputeSHA256Hash(string filePath)
{ {
using (SHA256 _SHA256 = SHA256.Create()) using SHA256 _SHA256 = SHA256.Create();
{ using FileStream fileStream = File.OpenRead(filePath);
using (FileStream fileStream = File.OpenRead(filePath))
return BitConverter.ToString(_SHA256.ComputeHash(fileStream)).Replace("-", "").ToLowerInvariant(); return BitConverter.ToString(_SHA256.ComputeHash(fileStream)).Replace("-", "").ToLowerInvariant();
} }
}
@ -316,7 +314,7 @@ public static class UniversalExtensions
{ {
foreach (char c in str) foreach (char c in str)
{ {
if (c < '0' || c > '9') if (c is < '0' or > '9')
return false; return false;
} }
@ -331,7 +329,7 @@ public static class UniversalExtensions
/// <param name="str"></param> /// <param name="str"></param>
/// <returns></returns> /// <returns></returns>
public static string GetAllDigits(this string str) => public static string GetAllDigits(this string str) =>
new String(str.Where(Char.IsDigit).ToArray()); new(str.Where(Char.IsDigit).ToArray());
} }
public static class StringExt public static class StringExt
@ -345,7 +343,7 @@ public static class StringExt
public static string Truncate(this string value, int maxLength) public static string Truncate(this string value, int maxLength)
{ {
if (string.IsNullOrEmpty(value)) return value; if (string.IsNullOrEmpty(value)) return value;
return value.Length <= maxLength ? value : value.Substring(0, maxLength); return value.Length <= maxLength ? value : value[ ..maxLength ];
} }
@ -361,7 +359,7 @@ public static class StringExt
if (string.IsNullOrEmpty(value)) if (string.IsNullOrEmpty(value))
return value; return value;
return value.Length <= maxLength ? value : $"{value.Substring(0, maxLength)}.."; return value.Length <= maxLength ? value : $"{value[ ..maxLength ]}..";
} }
@ -389,9 +387,7 @@ public static class StringExt
/// <returns></returns> /// <returns></returns>
public static string ComputeSHA256Hash(string str) public static string ComputeSHA256Hash(string str)
{ {
using (SHA256 _SHA256 = SHA256.Create()) using SHA256 _SHA256 = SHA256.Create();
{
return BitConverter.ToString(_SHA256.ComputeHash(Encoding.ASCII.GetBytes(str))).Replace("-", "").ToLowerInvariant(); return BitConverter.ToString(_SHA256.ComputeHash(Encoding.ASCII.GetBytes(str))).Replace("-", "").ToLowerInvariant();
} }
} }
}