From 6a2e68e562c32f0f7028a99cde1bb4b21f505773 Mon Sep 17 00:00:00 2001 From: fiso64 Date: Thu, 3 Oct 2024 23:11:55 +0200 Subject: [PATCH] fix invalid filename chars --- slsk-batchdl/Download.cs | 2 ++ slsk-batchdl/FileManager.cs | 4 ++-- slsk-batchdl/Program.cs | 2 +- slsk-batchdl/Utils.cs | 28 ++++++++++++++++++++++++++++ slsk-batchdl/slsk-batchdl.csproj | 2 +- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/slsk-batchdl/Download.cs b/slsk-batchdl/Download.cs index 1b3906e..d167f81 100644 --- a/slsk-batchdl/Download.cs +++ b/slsk-batchdl/Download.cs @@ -28,6 +28,8 @@ static class Download string origPath = filePath; filePath += ".incomplete"; + Printing.WriteLine($"Downloading: {track} to '{filePath}'", debugOnly: true); + var transferOptions = new TransferOptions( stateChanged: (state) => { diff --git a/slsk-batchdl/FileManager.cs b/slsk-batchdl/FileManager.cs index c3d12e8..00f8d04 100644 --- a/slsk-batchdl/FileManager.cs +++ b/slsk-batchdl/FileManager.cs @@ -33,7 +33,7 @@ public class FileManager if (tle.defaultFolderName != null) { - parent = Path.Join(parent, tle.defaultFolderName.ReplaceInvalidChars(Config.I.invalidReplaceStr, removeSlash: false)); + parent = Path.Join(parent, tle.defaultFolderName); } if (tle.source.Type == TrackType.Album && !string.IsNullOrEmpty(remoteCommonDir)) @@ -44,7 +44,7 @@ public class FileManager parent = Path.Join(parent, dirname, Path.GetDirectoryName(relpath) ?? ""); } - return Path.Join(parent, name); + return Path.Join(parent, name).CleanPath(Config.I.invalidReplaceStr); } public void SetRemoteCommonDir(string? remoteCommonDir) diff --git a/slsk-batchdl/Program.cs b/slsk-batchdl/Program.cs index 2007c4e..02d9951 100644 --- a/slsk-batchdl/Program.cs +++ b/slsk-batchdl/Program.cs @@ -493,7 +493,7 @@ static partial class Program PrintAlbum(tracks); } - var semaphore = new SemaphoreSlim(999); // Needs to be uncapped due to a bug that causes album downloads to fail after some time + var semaphore = new SemaphoreSlim(Config.I.concurrentProcesses == -2 ? 1 : 999); // Needs to be uncapped due to a bug that causes album downloads to fail after some time using var cts = new CancellationTokenSource(); try diff --git a/slsk-batchdl/Utils.cs b/slsk-batchdl/Utils.cs index 9c349b6..b61a1b9 100644 --- a/slsk-batchdl/Utils.cs +++ b/slsk-batchdl/Utils.cs @@ -272,6 +272,34 @@ public static class Utils return str; } + public static string CleanPath(this string fullPath, string replaceWith) + { + fullPath = Utils.NormalizedPath(fullPath); + + string[] pathParts = fullPath.Split('/'); + + foreach (char badChar in Path.GetInvalidPathChars()) + { + if (badChar != ':') + { + pathParts[0] = pathParts[0].Replace(badChar.ToString(), replaceWith); + } + } + + var chars = Path.GetInvalidFileNameChars(); + + for (int i = 1; i < pathParts.Length; i++) + { + foreach (char badChar in chars) + { + pathParts[i] = pathParts[i].Replace(badChar.ToString(), replaceWith); + } + } + + return string.Join('/', pathParts); + } + + public static string ReplaceSpecialChars(this string str, string replaceStr) { if (str.Length == 0) diff --git a/slsk-batchdl/slsk-batchdl.csproj b/slsk-batchdl/slsk-batchdl.csproj index 7efdf46..10c6f13 100644 --- a/slsk-batchdl/slsk-batchdl.csproj +++ b/slsk-batchdl/slsk-batchdl.csproj @@ -27,7 +27,7 @@ - +