From 4546c0627c46643b5a150b6237f66e56f47e9380 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Wed, 26 Feb 2014 18:28:53 -0800 Subject: [PATCH] Using MD5 hashes for profile filenames Prevent the long urls which can get generated when deep hierarchies are used. --- .../Services/ImageProfileManager.cs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs index a187c81df..76cac5be8 100644 --- a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs +++ b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.IO; using System.Linq; using System.Net; +using System.Security.Cryptography; using System.Web; using Orchard.ContentManagement; using Orchard.FileSystems.Media; @@ -205,20 +206,14 @@ namespace Orchard.MediaProcessing.Services { return false; } - private static readonly char[] _disallowed = @"/:?#[]@!$&'()*+,.;=s""<>\|%".ToCharArray(); - private static string CreateDefaultFileName(string path) { var extention = Path.GetExtension(path); - var newPath = Path.ChangeExtension(path, ""); - newPath = newPath.TrimEnd('.').RemoveDiacritics(); - var normalized = newPath.ToCharArray(); - for (var i = 0; i < normalized.Length; i++) { - if (Array.IndexOf(_disallowed, normalized[i]) >= 0) { - normalized[i] = '_'; - } + var utf8Bytes = System.Text.Encoding.UTF8.GetBytes(path); + using (var provider = new MD5CryptoServiceProvider()) { + var hash = provider.ComputeHash(utf8Bytes); + var encodedHash = Convert.ToBase64String(hash); + return encodedHash + extention; } - - return new string(normalized) + extention; } } }