Files
tubearchivist-jf-plugin/Jellyfin.Plugin.TubeArchivistMetadata/Utils/Utils.cs
T
2024-03-13 10:31:29 +01:00

33 lines
1.1 KiB
C#

using System.Text.RegularExpressions;
namespace Jellyfin.Plugin.TubeArchivistMetadata.Utilities
{
/// <summary>
/// Class containing common utils.
/// </summary>
public static class Utils
{
/// <summary>
/// Sanitize the given URL.
/// </summary>
/// <param name="inputUrl">An URL string.</param>
/// <returns>The URL string without spaces, doubled slashes and with a trailing slash.</returns>
public static string SanitizeUrl(string inputUrl)
{
// Extract the schema part
Match schemaMatch = Regex.Match(inputUrl, @"^(?<schema>https?://)");
// Remove double slashes and spaces from the remaining part of the URL
string cleanedPath = Regex.Replace(inputUrl.Substring(schemaMatch.Length), @"[/\s]+", "/");
// Add a trailing slash if not already present
cleanedPath = cleanedPath.TrimEnd('/') + "/";
// Combine the schema and cleaned path
string cleanedUrl = schemaMatch.Groups["schema"].Value + cleanedPath;
return cleanedUrl;
}
}
}