From c5535ddbc2a55ad5cace836d7c2c7df22520ef9f Mon Sep 17 00:00:00 2001 From: sebastiaan Date: Thu, 20 Sep 2012 16:46:55 -0200 Subject: [PATCH] Improved SafeFileName method to include all valid characters as listed by RFC 3986 section 2.2 Reserved Characters (January 2005) and character data chars that could be ambiguous Now characters like Chinese characters are allowed in the filenames, which addresses the issue below #U4-885 Fixed --- src/Umbraco.Core/IO/IOHelper.cs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/IO/IOHelper.cs b/src/Umbraco.Core/IO/IOHelper.cs index ca05e0c217..8a3a0688d5 100644 --- a/src/Umbraco.Core/IO/IOHelper.cs +++ b/src/Umbraco.Core/IO/IOHelper.cs @@ -209,7 +209,37 @@ namespace Umbraco.Core.IO /// A safe filename without any path specific chars. internal static string SafeFileName(string filePath) { - return !String.IsNullOrEmpty(filePath) ? Regex.Replace(filePath, @"[^a-zA-Z0-9\-\.\/\:]{1}", "_") : String.Empty; + if (String.IsNullOrEmpty(filePath)) + return String.Empty; + + if (!String.IsNullOrWhiteSpace(filePath)) + { + foreach (var character in Path.GetInvalidFileNameChars()) + { + filePath = filePath.Replace(character, '_'); + } + } + else + { + filePath = String.Empty; + } + + // Adapted from: http://stackoverflow.com/a/4827510/5018 + // Combined both Reserved Characters and Character Data + // from http://en.wikipedia.org/wiki/Percent-encoding + var stringBuilder = new StringBuilder(); + + const string reservedCharacters = "!*'();:@&=+$,/?%#[]-~{}\"<>\\^`| "; + + foreach (var character in filePath) + { + if (reservedCharacters.IndexOf(character) == -1) + stringBuilder.Append(character); + else + stringBuilder.Append("_"); + } + + return stringBuilder.ToString(); } } }