Only use 0-9A-Za-z-_()[] characters in filenames

This commit is contained in:
nullium21 2022-10-26 13:31:32 +03:00
parent 72b594d72e
commit dffebdf7ed

View File

@ -2,7 +2,6 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.IO; using System.IO;
using System.Linq;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.IO; using osu.Game.IO;
@ -137,20 +136,34 @@ namespace osu.Game.Extensions
return instance.OnlineID.Equals(other.OnlineID); return instance.OnlineID.Equals(other.OnlineID);
} }
private static readonly char[] invalid_filename_characters = Path.GetInvalidFileNameChars() private static bool isValidFilenameChar(this char ch)
// Backslash is added to avoid issues when exporting to zip. {
// See SharpCompress filename normalisation https://github.com/adamhathcock/sharpcompress/blob/a1e7c0068db814c9aa78d86a94ccd1c761af74bd/src/SharpCompress/Writers/Zip/ZipWriter.cs#L143. if (ch >= '0' && ch <= '9')
.Append('\\') return true;
.ToArray();
if (ch >= 'A' && ch <= 'Z')
return true;
if (ch >= 'a' && ch <= 'z')
return true;
return "-_()[]".Contains(ch);
}
/// <summary> /// <summary>
/// Get a valid filename for use inside a zip file. Avoids backslashes being incorrectly converted to directories. /// Get a valid filename for use inside a zip file. Avoids backslashes being incorrectly converted to directories.
/// </summary> /// </summary>
public static string GetValidArchiveContentFilename(this string filename) public static string GetValidArchiveContentFilename(this string filename)
{ {
foreach (char c in invalid_filename_characters) char[] resultData = filename.ToCharArray();
filename = filename.Replace(c, '_');
return filename; for (int i = 0; i < resultData.Length; i++)
{
if (!isValidFilenameChar(resultData[i]))
resultData[i] = '_';
}
return new string(resultData);
} }
} }
} }