aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-05-13 18:30:54 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-05-13 18:30:54 -0500
commit64fda245a090e78b89935e33294556a4f012388a (patch)
tree892e21a1525cb9dea02b5de6939c0dc5813ef4e9 /src
parent7e16e265091480ba197c21563b9ebc30029cae1f (diff)
downloadslskd-64fda245a090e78b89935e33294556a4f012388a.tar.gz
make local filename resolution extension methods
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Extensions.cs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs
index b7243549..1fb8f761 100644
--- a/src/slskd/Common/Extensions.cs
+++ b/src/slskd/Common/Extensions.cs
@@ -87,6 +87,41 @@ 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)
+ {
+ var localFilename = remoteFilename.ToLocalOSPath();
+ var path = $"{Path.GetDirectoryName(localFilename).Replace(Path.GetDirectoryName(Path.GetDirectoryName(localFilename)), string.Empty)}";
+
+ var sanitizedFilename = Path.GetFileName(localFilename);
+
+ foreach (var c in Path.GetInvalidFileNameChars())
+ {
+ sanitizedFilename = sanitizedFilename.Replace(c, '_');
+ }
+
+ return Path.Combine(path, sanitizedFilename).TrimStart('\\');
+ }
+
+ /// <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 local format (normalizes path separators).
/// </summary>
/// <param name="path">The path to convert.</param>