From 3e52de31aad47a3a333c64ef1b68f8abcf726fe8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 31 May 2026 14:55:07 -0500 Subject: fix GetFileNameSafely test cases --- src/slskd/Common/FileSafety.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/FileSafety.cs b/src/slskd/Common/FileSafety.cs index 011a7a70..9c8fac74 100644 --- a/src/slskd/Common/FileSafety.cs +++ b/src/slskd/Common/FileSafety.cs @@ -224,17 +224,22 @@ public static class FileSafety /// 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); + if (path is null or "" || path.EndsWith('/') || path.EndsWith('\\')) + { + return null; + } - if (path.EndsWith('/') || path.EndsWith('\\')) + if (StripPathRoot(path, os) == string.Empty) { return null; } + var localPath = LocalizePath(path, os); var segments = localPath.Split('/', '\\'); + + return segments .TakeLast(1) .Select(s => sanitize == true ? SanitizePathSegment(s, os: os) : s) .Single(); @@ -254,7 +259,7 @@ public static class FileSafety { ArgumentNullException.ThrowIfNull(path); - var local = LocalizePath(path, os: os); + var local = LocalizePath(path, os); return string.Join(Path.DirectorySeparatorChar, local .Split('/', '\\') -- cgit v1.2.3