1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-29 13:42:54 +08:00

Merge pull request #20948 from nullium21/fix-filename-char-filtering

Only use alphanumeric + some special characters in filenames
This commit is contained in:
Bartłomiej Dach 2022-10-29 13:19:27 +02:00 committed by GitHub
commit 96436b065a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 16 deletions

View File

@ -340,7 +340,7 @@ namespace osu.Game.Beatmaps
static string createBeatmapFilenameFromMetadata(BeatmapInfo beatmapInfo) static string createBeatmapFilenameFromMetadata(BeatmapInfo beatmapInfo)
{ {
var metadata = beatmapInfo.Metadata; var metadata = beatmapInfo.Metadata;
return $"{metadata.Artist} - {metadata.Title} ({metadata.Author.Username}) [{beatmapInfo.DifficultyName}].osu".GetValidArchiveContentFilename(); return $"{metadata.Artist} - {metadata.Title} ({metadata.Author.Username}) [{beatmapInfo.DifficultyName}].osu".GetValidFilename();
} }
} }

View File

@ -37,7 +37,7 @@ namespace osu.Game.Database
/// <param name="item">The item to export.</param> /// <param name="item">The item to export.</param>
public void Export(TModel item) public void Export(TModel item)
{ {
string filename = $"{item.GetDisplayString().GetValidArchiveContentFilename()}{FileExtension}"; string filename = $"{item.GetDisplayString().GetValidFilename()}{FileExtension}";
using (var stream = exportStorage.CreateFileSafely(filename)) using (var stream = exportStorage.CreateFileSafely(filename))
ExportModelTo(item, stream); ExportModelTo(item, stream);

View File

@ -2,7 +2,7 @@
// 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 System.Text.RegularExpressions;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.IO; using osu.Game.IO;
@ -15,6 +15,8 @@ namespace osu.Game.Extensions
{ {
public static class ModelExtensions public static class ModelExtensions
{ {
private static readonly Regex invalid_filename_chars = new Regex(@"(?!$)[^A-Za-z0-9_()[\]. \-]", RegexOptions.Compiled);
/// <summary> /// <summary>
/// Get the relative path in osu! storage for this file. /// Get the relative path in osu! storage for this file.
/// </summary> /// </summary>
@ -137,20 +139,14 @@ 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()
// 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.
.Append('\\')
.ToArray();
/// <summary> /// <summary>
/// Get a valid filename for use inside a zip file. Avoids backslashes being incorrectly converted to directories. /// Create a valid filename which should work across all platforms.
/// </summary> /// </summary>
public static string GetValidArchiveContentFilename(this string filename) /// <remarks>
{ /// This function replaces all characters not included in a very pessimistic list which should be compatible
foreach (char c in invalid_filename_characters) /// across all operating systems. We are using this in place of <see cref="Path.GetInvalidFileNameChars"/> as
filename = filename.Replace(c, '_'); /// that function does not have per-platform considerations (and is only made to work on windows).
return filename; /// </remarks>
} public static string GetValidFilename(this string filename) => invalid_filename_chars.Replace(filename, "_");
} }
} }