aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/CommonExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Common/CommonExtensions.cs')
-rw-r--r--src/slskd/Common/CommonExtensions.cs84
1 files changed, 10 insertions, 74 deletions
diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs
index 77db91ef..95721163 100644
--- a/src/slskd/Common/CommonExtensions.cs
+++ b/src/slskd/Common/CommonExtensions.cs
@@ -382,20 +382,6 @@ namespace slskd
.Deserialize<T>(str);
/// <summary>
- /// Converts a fully qualified remote filename to a local filename based in the provided
- /// <paramref name="baseDirectory"/>, swapping directory characters for those specific to the local OS, removing any
- /// characters that are invalid for the local OS, and making the path relative to the remote store (including the
- /// filename and the parent folder).
- /// </summary>
- /// <param name="remoteFilename">The fully qualified remote filename to convert.</param>
- /// <param name="baseDirectory">The base directory for the local filename.</param>
- /// <returns>The converted filename.</returns>
- public static string ToLocalFilename(this string remoteFilename, string baseDirectory)
- {
- return Path.Combine(baseDirectory, remoteFilename.ToLocalRelativeFilename());
- }
-
- /// <summary>
/// Converts the given path to the normalized format (normalizes path separators to backslashes).
/// </summary>
/// <remarks>
@@ -409,16 +395,6 @@ namespace slskd
}
/// <summary>
- /// Converts the given path to the local format (normalizes path separators to Path.DirectorySeparatorChar).
- /// </summary>
- /// <param name="path">The path to convert.</param>
- /// <returns>The converted path.</returns>
- public static string LocalizePath(this string path)
- {
- return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar);
- }
-
- /// <summary>
/// <see cref="Path.GetDirectoryName(string)"/>, but for paths normalized to use backslashes.
/// </summary>
/// <param name="path"></param>
@@ -439,54 +415,6 @@ namespace slskd
}
/// <summary>
- /// Converts a fully qualified remote filename to a local filename, swapping directory characters for those specific
- /// to the local OS, removing any characters that are invalid for the local OS, and making the path relative to the
- /// remote store (including the filename and the parent folder).
- /// </summary>
- /// <param name="remoteFilename">The fully qualified remote filename to convert.</param>
- /// <returns>The converted filename.</returns>
- public static string ToLocalRelativeFilename(this string remoteFilename)
- {
- if (string.IsNullOrWhiteSpace(remoteFilename))
- {
- throw new ArgumentException($"Invalid remote filename; expected a non-whitespace value, received '{remoteFilename}'", nameof(remoteFilename));
- }
-
- // normalize path separators
- var localizedRemoteFilename = remoteFilename.LocalizePath();
-
- var parts = localizedRemoteFilename.Split(Path.DirectorySeparatorChar);
-
- if (parts.Length == 1)
- {
- return parts.First().ReplaceInvalidFileNameCharacters();
- }
-
- var file = parts.Last().ReplaceInvalidFileNameCharacters();
- var directory = parts.Reverse().Skip(1).Take(1).Single().ReplaceInvalidFileNameCharacters();
-
- return Path.Combine(directory, file);
- }
-
- /// <summary>
- /// Replaces any occurrence of an invalid filename character with the specified <see paramref="replacement"/>.
- /// </summary>
- /// <param name="path">The path to sanitize.</param>
- /// <param name="replacement">The character with which to replace invalid characters.</param>
- /// <returns>The sanitized path.</returns>
- public static string ReplaceInvalidFileNameCharacters(this string path, char replacement = '_')
- {
- var sanitized = path;
-
- foreach (var c in Path.GetInvalidFileNameChars())
- {
- sanitized = sanitized.Replace(c, replacement);
- }
-
- return sanitized;
- }
-
- /// <summary>
/// Converts a ModelStateDictionary into a human-readable format.
/// </summary>
/// <param name="dictionary">The ModelStateDictionary to format.</param>
@@ -555,9 +483,17 @@ namespace slskd
/// <returns>The new object deserialized from the string.</returns>
public static T FromJson<T>(this string str) => JsonSerializer.Deserialize<T>(str, GetJsonSerializerOptions());
- private static JsonSerializerOptions GetJsonSerializerOptions()
+ /// <summary>
+ /// Applies the standard json serializer options to the specified instance.
+ /// </summary>
+ /// <param name="opt">The instance.</param>
+ /// <returns>The instances with the standard options applied.</returns>
+ public static JsonSerializerOptions WithStandardOptions(this JsonSerializerOptions opt)
+ => GetJsonSerializerOptions(opt);
+
+ private static JsonSerializerOptions GetJsonSerializerOptions(JsonSerializerOptions opt = null)
{
- var options = new JsonSerializerOptions();
+ var options = opt ?? new JsonSerializerOptions();
options.Converters.Add(new IPAddressConverter());
options.Converters.Add(new JsonStringEnumConverter());
options.Converters.Add(new KnownUnsupportedTypeConverter());