mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-02-14 07:43:31 -07:00
Fix filename sanitization for NTFS filesystems on non-Windows systems (#1455)
Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
parent
a74ba4da0c
commit
7c79e0639b
|
|
@ -1,11 +1,32 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Utils.Extensions;
|
namespace DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
|
||||||
public static class PathExtensions
|
public static class PathExtensions
|
||||||
{
|
{
|
||||||
|
// Characters that are invalid on common filesystems.
|
||||||
|
// This is a union of invalid characters from Windows (NTFS/FAT32), Linux (ext4/XFS), and macOS (HFS+/APFS).
|
||||||
|
// We use this instead of Path.GetInvalidFileNameChars() because that only returns OS-specific characters,
|
||||||
|
// not filesystem-specific characters. This means that it's possible to use, for example, an NTFS drive on
|
||||||
|
// Linux, which would allow the OS to create filenames with '?' but result in errors when writing to the filesystem.
|
||||||
|
// https://github.com/Tyrrrz/DiscordChatExporter/issues/1452
|
||||||
|
private static readonly char[] InvalidFileNameChars =
|
||||||
|
[
|
||||||
|
'\0', // Null character - invalid on all filesystems
|
||||||
|
'/', // Path separator on Unix
|
||||||
|
'\\', // Path separator on Windows
|
||||||
|
':', // Reserved on Windows (drive letters, NTFS streams)
|
||||||
|
'*', // Wildcard on Windows
|
||||||
|
'?', // Wildcard on Windows
|
||||||
|
'"', // Reserved on Windows
|
||||||
|
'<', // Redirection on Windows
|
||||||
|
'>', // Redirection on Windows
|
||||||
|
'|', // Pipe on Windows
|
||||||
|
];
|
||||||
|
|
||||||
extension(Path)
|
extension(Path)
|
||||||
{
|
{
|
||||||
public static string EscapeFileName(string path)
|
public static string EscapeFileName(string path)
|
||||||
|
|
@ -13,7 +34,7 @@ public static class PathExtensions
|
||||||
var buffer = new StringBuilder(path.Length);
|
var buffer = new StringBuilder(path.Length);
|
||||||
|
|
||||||
foreach (var c in path)
|
foreach (var c in path)
|
||||||
buffer.Append(!Path.GetInvalidFileNameChars().Contains(c) ? c : '_');
|
buffer.Append(!InvalidFileNameChars.Contains(c) ? c : '_');
|
||||||
|
|
||||||
// File names cannot end with a dot on Windows
|
// File names cannot end with a dot on Windows
|
||||||
// https://github.com/Tyrrrz/DiscordChatExporter/issues/977
|
// https://github.com/Tyrrrz/DiscordChatExporter/issues/977
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue