diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-05-14 14:34:46 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-14 14:34:46 -0500 |
| commit | e22472791be56eb3b44d7af7064400aa5e6ef9fc (patch) | |
| tree | 8b6629c2a81de5889a0084a8f12d5c73b0cfdf36 /src/slskd/Common | |
| parent | 5926fd44420da613b4c4c00d043151916f24b989 (diff) | |
| parent | a0458827d19f21fe3efe40b5630bc18d0e28fd38 (diff) | |
| download | slskd-e22472791be56eb3b44d7af7064400aa5e6ef9fc.tar.gz | |
Merge pull request #70 from slskd/incomplete-downloads
Save incomplete downloads in a separate directory
Diffstat (limited to 'src/slskd/Common')
| -rw-r--r-- | src/slskd/Common/Extensions.cs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index b7243549..4e08ed02 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -87,6 +87,41 @@ namespace slskd } /// <summary> + /// 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). + /// </summary> + /// <param name="remoteFilename">The fully qualified remote filename to convert.</param> + /// <returns>The converted filename.</returns> + 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('\\').TrimStart('/'); + } + + /// <summary> + /// Converts a fully qualified remote filename to a local filename based in the provided <paramref name="baseDirectory"/>, 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). + /// </summary> + /// <param name="remoteFilename">The fully qualified remote filename to convert.</param> + /// <param name="baseDirectory">The base directory for the local filename.</param> + /// <returns>The converted filename.</returns> + public static string ToLocalFilename(this string remoteFilename, string baseDirectory) + { + return Path.Combine(baseDirectory, remoteFilename.ToLocalRelativeFilename()); + } + + /// <summary> /// Converts the given path to the local format (normalizes path separators). /// </summary> /// <param name="path">The path to convert.</param> |
