You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.0 KiB
33 lines
1.0 KiB
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
namespace DiscordChatExporter.Core.Utils;
|
|
|
|
public static class PathEx
|
|
{
|
|
private static readonly HashSet<char> InvalidFileNameChars = new(Path.GetInvalidFileNameChars());
|
|
|
|
public static string EscapeFileName(string path)
|
|
{
|
|
var buffer = new StringBuilder(path.Length);
|
|
|
|
foreach (var c in path)
|
|
buffer.Append(!InvalidFileNameChars.Contains(c) ? c : '_');
|
|
|
|
// File names cannot end with a dot on Windows
|
|
// https://github.com/Tyrrrz/DiscordChatExporter/issues/977
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
{
|
|
while (buffer.Length > 0 && buffer[^1] == '.')
|
|
buffer.Remove(buffer.Length - 1, 1);
|
|
}
|
|
|
|
return buffer.ToString();
|
|
}
|
|
|
|
public static bool IsDirectoryPath(string path) =>
|
|
path.EndsWith(Path.DirectorySeparatorChar) ||
|
|
path.EndsWith(Path.AltDirectorySeparatorChar);
|
|
} |