Fixing profile folders logic

Keep filename unchanged for SEO purpose.
Use hashcode hexadecimal version of folder names
This commit is contained in:
Sebastien Ros
2014-03-17 15:23:39 -07:00
parent 7cc07a0a53
commit fcab97089b

View File

@@ -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);
}
}
}