From 64fda245a090e78b89935e33294556a4f012388a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 13 May 2021 18:30:54 -0500 Subject: make local filename resolution extension methods --- src/slskd/Common/Extensions.cs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src') 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 @@ -86,6 +86,41 @@ namespace slskd sizeSuffixes[mag]); } + /// + /// 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). + /// + /// The fully qualified remote filename to convert. + /// The converted filename. + 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('\\'); + } + + /// + /// Converts a fully qualified remote filename to a local filename based in the provided , 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). + /// + /// The fully qualified remote filename to convert. + /// The base directory for the local filename. + /// The converted filename. + public static string ToLocalFilename(this string remoteFilename, string baseDirectory) + { + return Path.Combine(baseDirectory, remoteFilename.ToLocalRelativeFilename()); + } + /// /// Converts the given path to the local format (normalizes path separators). /// -- cgit v1.2.3