|
| 1 | +using System.IO; |
| 2 | +using System.Linq; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | + |
| 6 | +namespace BExIS.Utils.Files |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Provides utility methods for handling and sanitizing file names based on operating system rules. |
| 10 | + /// </summary> |
| 11 | + public static class FileNameUtility |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Replaces all invalid file name characters with a specified replacement character. |
| 15 | + /// It ensures the resulting string is safe to use as a file name segment on Windows/Unix-like systems. |
| 16 | + /// </summary> |
| 17 | + /// <param name="fileName">The original file name string to sanitize.</param> |
| 18 | + /// <param name="replacementChar">The character to use for replacing invalid characters (default is '-').</param> |
| 19 | + /// <returns>A sanitized string suitable for use as a file name.</returns> |
| 20 | + public static string SanitizeFileName(string fileName, char replacementChar = '-') |
| 21 | + { |
| 22 | + // 1. Check for null or empty input |
| 23 | + if (string.IsNullOrEmpty(fileName)) |
| 24 | + { |
| 25 | + return string.Empty; |
| 26 | + } |
| 27 | + |
| 28 | + // 2. Get the array of characters forbidden in file names by the operating system. |
| 29 | + // This is the most reliable way to handle OS-specific invalid characters. |
| 30 | + char[] invalidChars = Path.GetInvalidFileNameChars(); |
| 31 | + |
| 32 | + // 3. Use a StringBuilder for efficient string manipulation |
| 33 | + StringBuilder sb = new StringBuilder(fileName.Length); |
| 34 | + |
| 35 | + // 4. Iterate through the input string and replace invalid characters |
| 36 | + foreach (char c in fileName) |
| 37 | + { |
| 38 | + if (invalidChars.Contains(c)) |
| 39 | + { |
| 40 | + // Replace the invalid character with the specified replacement char |
| 41 | + sb.Append(replacementChar); |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + // Keep valid characters as they are |
| 46 | + sb.Append(c); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + string sanitizedName = sb.ToString(); |
| 51 | + |
| 52 | + // 5. Post-sanitization cleanup for specific Windows constraints (ends with '.' or ' ') |
| 53 | + // While replacement should handle most issues, it's good practice to ensure |
| 54 | + // the name doesn't end in characters that Windows implicitly trims or handles poorly. |
| 55 | + |
| 56 | + // Trim spaces from the end |
| 57 | + sanitizedName = sanitizedName.TrimEnd(' '); |
| 58 | + |
| 59 | + // If the name ends with the replacement char (e.g., if the original ended with a '.'), |
| 60 | + // ensure it doesn't leave an ending period (Windows specific) unless it's part of an extension. |
| 61 | + // We'll use a simple check to ensure it doesn't end with a period. |
| 62 | + if (sanitizedName.EndsWith(".")) |
| 63 | + { |
| 64 | + // Remove trailing period if it exists |
| 65 | + sanitizedName = sanitizedName.TrimEnd('.'); |
| 66 | + } |
| 67 | + |
| 68 | + // Optional: Replace multiple consecutive replacement characters with a single one. |
| 69 | + // This prevents "file---name--.txt" from becoming "file-name.txt". |
| 70 | + string doubleReplacement = new string(new[] { replacementChar, replacementChar }); |
| 71 | + while (sanitizedName.Contains(doubleReplacement)) |
| 72 | + { |
| 73 | + sanitizedName = sanitizedName.Replace(doubleReplacement, replacementChar.ToString()); |
| 74 | + } |
| 75 | + |
| 76 | + return sanitizedName; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments