diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-09-15 15:44:21 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-15 15:44:21 -0500 |
| commit | 81abf6110379fa778f8bee2d7ae032b530a71b85 (patch) | |
| tree | a883623a7fd217f11c1c5f88ab3d410378d384e9 | |
| parent | e71870c765c5b8be077c7fc12891322f8f679cb8 (diff) | |
| parent | 8b1281862de1464db3be7500505f7c2f2f59259a (diff) | |
| download | slskd-81abf6110379fa778f8bee2d7ae032b530a71b85.tar.gz | |
Merge pull request #1216 from slskd/slashes
Rssume remote filenames use a backslash, unless forward slashes are present
| -rw-r--r-- | src/slskd/Common/CommonExtensions.cs | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index a68ce2f2..44e3c034 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -133,15 +133,46 @@ namespace slskd } /// <summary> + /// Makes a best-guess determination of the directory separator character used by a remote system, + /// based on the characters present in the specified <paramref name="remoteFilename"/>. + /// </summary> + /// <param name="remoteFilename">The fully qualified remote filename to inspect.</param> + /// <returns>The guessed directory separator.</returns> + public static char GuessDirectorySeparator(this string remoteFilename) + { + // forward slash is a forbidden character on all operating systems; if the specified string contains it, + // then it is coming from the OS's directory separator + if (remoteFilename.Contains('/')) + { + return '/'; + } + + // if the given string doesn't contain any forward slashes, it either lacks a directory separator at all + // (potentially impossible?) or it is using backspaces. backspaces are used by all major clients, so + // this should be a safe assumption. + return '\\'; + } + + /// <summary> + /// Returns the directory name of the given <paramref name="path"/>, using the specified <paramref name="directorySeparator"/> + /// to split and join directories and filename. + /// </summary> + /// <param name="path">The path for which to return the directory name.</param> + /// <param name="directorySeparator">The directory separator character.</param> + /// <returns>The specified path, less the last segment.</returns> + public static string GetDirectoryName(this string path, char directorySeparator) + { + return string.Join(directorySeparator, path.Split(directorySeparator).SkipLast(1)); + } + + /// <summary> /// Returns the directory from the given path, regardless of separator format. /// </summary> /// <param name="path">The path.</param> /// <returns>The directory.</returns> public static string DirectoryName(this string path) { - var separator = path.Contains('\\') ? '\\' : '/'; - var parts = path.Split(separator); - return string.Join(separator, parts.Take(parts.Length - 1)); + return path.GetDirectoryName(path.GuessDirectorySeparator()); } /// <summary> |
