From fcab97089be93b5d3aabd6ee39946be0c5221780 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Mon, 17 Mar 2014 15:23:39 -0700 Subject: [PATCH] Fixing profile folders logic Keep filename unchanged for SEO purpose. Use hashcode hexadecimal version of folder names --- .../Services/ImageProfileManager.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs index 76cac5be8..480959317 100644 --- a/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs +++ b/src/Orchard.Web/Modules/Orchard.MediaProcessing/Services/ImageProfileManager.cs @@ -109,7 +109,7 @@ namespace Orchard.MediaProcessing.Services { using (var image = GetImage(path)) { - var filterContext = new FilterContext { Media = image, FilePath = _storageProvider.Combine("_Profiles", _storageProvider.Combine(profileName, CreateDefaultFileName(path))) }; + var filterContext = new FilterContext { Media = image, FilePath = _storageProvider.Combine("_Profiles", FormatProfilePath(profileName, path)) }; if (image == null) { return filterContext.FilePath; @@ -134,7 +134,6 @@ namespace Orchard.MediaProcessing.Services { _fileNameProvider.UpdateFileName(profileName, path, filterContext.FilePath); if (!filterContext.Saved) { - _storageProvider.TryCreateFolder(_storageProvider.Combine("_Profiles", profilePart.Name)); var newFile = _storageProvider.OpenOrCreate(filterContext.FilePath); using (var imageStream = newFile.OpenWrite()) { using (var sw = new BinaryWriter(imageStream)) { @@ -206,14 +205,14 @@ namespace Orchard.MediaProcessing.Services { return false; } - private static string CreateDefaultFileName(string path) { - var extention = Path.GetExtension(path); - 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; - } + private string FormatProfilePath(string profileName, string path) { + + var filenameWithExtension = Path.GetFileName(path) ?? ""; + var fileLocation = path.Substring(0, path.Length - filenameWithExtension.Length); + + return _storageProvider.Combine( + _storageProvider.Combine(profileName.GetHashCode().ToString("x").ToLowerInvariant(), fileLocation.GetHashCode().ToString("x").ToLowerInvariant()), + filenameWithExtension); } } }