From 1491f60791733a70878c9716476ed22667f186e8 Mon Sep 17 00:00:00 2001 From: Elias Fierke Date: Mon, 8 Jun 2026 15:31:41 +0200 Subject: [PATCH] [fix:] added unversioned file... --- PathUtilities.cs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 PathUtilities.cs diff --git a/PathUtilities.cs b/PathUtilities.cs new file mode 100644 index 0000000..b008b19 --- /dev/null +++ b/PathUtilities.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; + +namespace Logof_Client; + +public static class PathUtilities +{ + private static readonly char[] WindowsInvalidFileNameChars = + { + '<', '>', ':', '"', '/', '\\', '|', '?', '*' + }; + + private static readonly string[] WindowsReservedFileNames = + { + "CON", "PRN", "AUX", "NUL", + "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", + "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" + }; + + public static string NormalizeFileSystemPath(string path) + { + if (string.IsNullOrWhiteSpace(path)) return path; + + if (Uri.TryCreate(path, UriKind.Absolute, out var uri) && uri.IsFile) + return uri.LocalPath; + + return path; + } + + public static string NormalizeFileSystemPath(Uri path) + { + return path.IsFile ? path.LocalPath : path.ToString(); + } + + public static bool HasInvalidFileNameChars(string fileName) + { + if (string.IsNullOrWhiteSpace(fileName)) return true; + if (fileName.EndsWith(' ') || fileName.EndsWith('.')) return true; + if (fileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) return true; + if (fileName.IndexOfAny(WindowsInvalidFileNameChars) >= 0) return true; + + var nameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); + foreach (var reservedName in WindowsReservedFileNames) + { + if (string.Equals(fileName, reservedName, StringComparison.OrdinalIgnoreCase) || + string.Equals(nameWithoutExtension, reservedName, StringComparison.OrdinalIgnoreCase)) + return true; + } + + foreach (var c in fileName) + { + if (char.IsControl(c)) return true; + } + + return false; + } +}