diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-05-31 14:55:07 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-05-31 14:55:07 -0500 |
| commit | 3e52de31aad47a3a333c64ef1b68f8abcf726fe8 (patch) | |
| tree | 6c5a731fe19f8ad7a73e1798f8a3d1ae495d5b8e | |
| parent | 2926ef6ee1c9151d72ae9ab5d713630b299224ed (diff) | |
| download | slskd-3e52de31aad47a3a333c64ef1b68f8abcf726fe8.tar.gz | |
fix GetFileNameSafely test cases
| -rw-r--r-- | src/slskd/Common/FileSafety.cs | 13 |
1 files changed, 9 insertions, 4 deletions
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 /// <param name="sanitize">An optional value indicating that invalid characters should not be replaced.</param> /// <param name="os">An optional operating system override, for testing.</param> /// <returns>The extracted filename.</returns> - /// <exception cref="ArgumentNullException">Thrown if the specified path is null.</exception> 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('/', '\\') |
