From ebf7c753f385c558cc1442609eaa17d2eb32e6da Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 30 May 2026 15:40:13 -0500 Subject: add get filename/directory name safely methods, overload sanitizefilename with sanitizePathRoot --- src/slskd/Common/FileSafety.cs | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs index dc27a8b4..93a91faa 100644 --- a/src/slskd/Common/FileSafety.cs +++ b/src/slskd/Common/FileSafety.cs @@ -216,6 +216,54 @@ public static class FileSafety /// True if the path is relative, false otherwise. public static bool IsPathRelative(string path, OSPlatform? os = null) => !IsPathAbsolute(path, os); + /// + /// Returns the filename from the specified , properly + /// handling both forward and backslashes and removing invalid characters. + /// + /// The path from which to extract the filename. + /// An optional value indicating that invalid characters should not be replaced. + /// An optional operating system override, for testing. + /// The extracted filename. + /// Thrown if the specified path is null. + public static string GetFileNameSafely(string path, bool sanitize = true, OSPlatform? os = null) + { + ArgumentNullException.ThrowIfNull(path); + + return LocalizePath(path, os) + .Split(Path.DirectorySeparatorChar) + .TakeLast(1) + .Select(s => sanitize == true ? SanitizePathSegment(s, os: os) : s) + .Single(); + } + + /// + /// Returns the full directory name from the specified , properly handling both + /// forward and backslashes, removing Windows drive, UNC and Soulseek QT root segments and sanitizing + /// each of the remaining segments. + /// + /// The path from which to extract the directory name. + /// An optional value indicating that root segments should not be stripped. + /// An optional value indicating that path segments should not be sanitized. + /// An optional operating system override, for testing. + /// The directory name. + /// Thrown if the specified path is null. + public static string GetDirectoryNameSafely(string path, bool stripRoot = true, bool sanitize = true, OSPlatform? os = null) + { + ArgumentNullException.ThrowIfNull(path); + + var local = LocalizePath(path, os: os); + + if (stripRoot) + { + local = StripPathRoot(local); + } + + return string.Join(Path.DirectorySeparatorChar, local + .Split(Path.DirectorySeparatorChar) + .SkipLast(1) + .Select(s => sanitize == true ? SanitizePathSegment(s, os: os) : s)); + } + /// /// Converts the given path to the local format (normalizes path separators to Path.DirectorySeparatorChar). /// @@ -270,6 +318,21 @@ public static class FileSafety .Replace('\\', replacement); } + /// + /// Sanitizes the specified to make it suitable and safe on the local operating + /// system by stripping all slashes (forward or back) and replacing any invalid characters with the specified + /// . + /// + /// + /// This is an overload for that exists to improve understanding of intent at the + /// call site. + /// + /// The path segment. + /// The character to substitute for invalid characters. + /// An optional operating system override, for testing. + /// The sanitized path segment. + public static string SanitizePathSegment(string segment, char replacement = '_', OSPlatform? os = null) => SanitizeFilename(segment, replacement, os); + /// /// Sanitizes the specified to make it suitable and safe on the local operating system /// by converting to the correct slashes, dropping a root (if present), replacing any invalid characters with -- cgit v1.2.3