Projektdateien hinzufügen.
This commit is contained in:
53
UmlautAdaptarr/Utilities/Extensions.cs
Normal file
53
UmlautAdaptarr/Utilities/Extensions.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace UmlautAdaptarr.Utilities
|
||||
{
|
||||
public static class Extensions
|
||||
{
|
||||
public static string GetQuery(this HttpContext context, string key)
|
||||
{
|
||||
return context.Request.Query[key].FirstOrDefault() ?? string.Empty;
|
||||
}
|
||||
|
||||
public static string RemoveAccentButKeepGermanUmlauts(this string text)
|
||||
{
|
||||
// TODO: evaluate if this is needed (here)
|
||||
var stringWithoutSz = text.Replace("ß", "ss");
|
||||
|
||||
var normalizedString = stringWithoutSz.Normalize(NormalizationForm.FormD);
|
||||
var stringBuilder = new StringBuilder();
|
||||
|
||||
foreach (var c in normalizedString)
|
||||
{
|
||||
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
|
||||
|
||||
if (unicodeCategory != UnicodeCategory.NonSpacingMark || c == '\u0308')
|
||||
{
|
||||
stringBuilder.Append(c);
|
||||
}
|
||||
}
|
||||
|
||||
return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
|
||||
}
|
||||
|
||||
public static string ReplaceGermanUmlautsWithLatinEquivalents(this string text)
|
||||
{
|
||||
return text
|
||||
.Replace("Ö", "Oe")
|
||||
.Replace("Ä", "Ae")
|
||||
.Replace("Ü", "Ue")
|
||||
.Replace("ö", "oe")
|
||||
.Replace("ä", "ae")
|
||||
.Replace("ü", "ue")
|
||||
.Replace("ß", "ss");
|
||||
}
|
||||
|
||||
public static bool HasGermanUmlauts(this string text)
|
||||
{
|
||||
if (text == null) return false;
|
||||
var umlauts = new[] { 'ö', 'ä', 'ü', 'Ä', 'Ü', 'Ö', 'ß' };
|
||||
return umlauts.Any(text.Contains);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
UmlautAdaptarr/Utilities/UrlUtilities.cs
Normal file
44
UmlautAdaptarr/Utilities/UrlUtilities.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
|
||||
namespace UmlautAdaptarr.Utilities
|
||||
{
|
||||
public partial class UrlUtilities
|
||||
{
|
||||
[GeneratedRegex(@"^(?!http:\/\/)([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+.*)$")]
|
||||
private static partial Regex UrlMatchingRegex();
|
||||
public static bool IsValidDomain(string domain)
|
||||
{
|
||||
// RegEx für eine einfache URL-Validierung ohne http:// und ohne abschließenden Schrägstrich
|
||||
// Erlaubt optionale Subdomains, Domainnamen und TLDs, aber keine Pfade oder Protokolle
|
||||
var regex = UrlMatchingRegex();
|
||||
return regex.IsMatch(domain) && !domain.EndsWith("/");
|
||||
}
|
||||
|
||||
public static string BuildUrl(string domain, IDictionary<string, string> queryParameters)
|
||||
{
|
||||
var uriBuilder = new UriBuilder("https", domain);
|
||||
|
||||
var query = HttpUtility.ParseQueryString(string.Empty);
|
||||
foreach (var param in queryParameters)
|
||||
{
|
||||
query[param.Key] = param.Value;
|
||||
}
|
||||
|
||||
uriBuilder.Query = query.ToString();
|
||||
return uriBuilder.ToString();
|
||||
}
|
||||
|
||||
public static string BuildUrl(string domain, string tParameter, string? apiKey = null)
|
||||
{
|
||||
var queryParameters = new Dictionary<string, string>() { { "t", tParameter } };
|
||||
|
||||
if (!string.IsNullOrEmpty(apiKey))
|
||||
{
|
||||
queryParameters["apiKey"] = apiKey;
|
||||
}
|
||||
|
||||
return BuildUrl(domain, queryParameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user