From ce0923cfc645e014a48949eac4a52d6bf614f661 Mon Sep 17 00:00:00 2001 From: sebastiaan Date: Wed, 19 Sep 2012 12:45:11 -0200 Subject: [PATCH] Uploading media with hyphens get a space added before the hyphen New media uploader: illegal characters in the filename are not filtered New media uploader: IE in intranet mode stores the whole path Fixed U4-882 U4-883 U4-874 --- src/Umbraco.Core/IO/IOHelper.cs | 9 +++ .../umbraco/webservices/MediaUploader.ashx.cs | 7 +- .../businesslogic/datatype/FileHandlerData.cs | 18 +---- .../media/UmbracoFileMediaFactory.cs | 6 +- .../media/UmbracoImageMediaFactory.cs | 8 +-- .../media/UmbracoMediaFactory.cs | 69 +++++++++++-------- 6 files changed, 64 insertions(+), 53 deletions(-) diff --git a/src/Umbraco.Core/IO/IOHelper.cs b/src/Umbraco.Core/IO/IOHelper.cs index c0734898d6..ca05e0c217 100644 --- a/src/Umbraco.Core/IO/IOHelper.cs +++ b/src/Umbraco.Core/IO/IOHelper.cs @@ -202,5 +202,14 @@ namespace Umbraco.Core.IO } + /// + /// Check to see if filename passed has any special chars in it and strips them to create a safe filename. Used to overcome an issue when Umbraco is used in IE in an intranet environment. + /// + /// The filename passed to the file handler from the upload field. + /// 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; + } } } diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/MediaUploader.ashx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/MediaUploader.ashx.cs index fff54392d9..e98d306552 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/MediaUploader.ashx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/MediaUploader.ashx.cs @@ -160,9 +160,14 @@ namespace umbraco.presentation.umbraco.webservices // if there was a file uploded if (uploadFile.ContentLength > 0) { + // Ensure we get the filename without the path in IE in intranet mode + // http://stackoverflow.com/questions/382464/httppostedfile-filename-different-from-ie + var fileName = new FileInfo(uploadFile.FileName).Name; + fileName = Umbraco.Core.IO.IOHelper.SafeFileName(fileName); + var postedMediaFile = new PostedMediaFile { - FileName = uploadFile.FileName, + FileName = fileName, DisplayName = context.Request["name"], ContentType = uploadFile.ContentType, ContentLength = uploadFile.ContentLength, diff --git a/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs b/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs index f399f9600b..feb7ddde3e 100644 --- a/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs +++ b/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs @@ -33,13 +33,13 @@ namespace umbraco.cms.businesslogic.datatype if (value is HttpPostedFile) { var file = value as HttpPostedFile; - name = SafeFileName(file.FileName.Substring(file.FileName.LastIndexOf(IOHelper.DirSepChar) + 1, file.FileName.Length - file.FileName.LastIndexOf(IOHelper.DirSepChar) - 1).ToLower()); + name = Umbraco.Core.IO.IOHelper.SafeFileName(file.FileName.Substring(file.FileName.LastIndexOf(IOHelper.DirSepChar) + 1, file.FileName.Length - file.FileName.LastIndexOf(IOHelper.DirSepChar) - 1).ToLower()); fileStream = file.InputStream; } else if (value is HttpPostedFileBase) { var file = value as HttpPostedFileBase; - name = SafeFileName(file.FileName.Substring(file.FileName.LastIndexOf(IOHelper.DirSepChar) + 1, file.FileName.Length - file.FileName.LastIndexOf(IOHelper.DirSepChar) - 1).ToLower()); + name = Umbraco.Core.IO.IOHelper.SafeFileName(file.FileName.Substring(file.FileName.LastIndexOf(IOHelper.DirSepChar) + 1, file.FileName.Length - file.FileName.LastIndexOf(IOHelper.DirSepChar) - 1).ToLower()); fileStream = file.InputStream; } else if (value is HttpPostedFileWrapper) @@ -129,19 +129,7 @@ namespace umbraco.cms.businesslogic.datatype } } } - - /// - /// Check to see if filename passed has any special chars in it and strips them to create a safe filename. Used to overcome an issue when Umbraco is used in IE in an intranet environment. - /// - /// The filename passed to the file handler from the upload field. - /// A safe filename without any path specific chars. - private string SafeFileName(string filePath) - { - if (!String.IsNullOrEmpty(filePath)) - return Regex.Replace(filePath, @"[^a-zA-Z0-9\-\.\/\:]{1}", "_"); - return String.Empty; - } - + private void clearRelatedValues() { string propertyTypeAlias = new Property(PropertyId).PropertyType.Alias; diff --git a/src/umbraco.cms/businesslogic/media/UmbracoFileMediaFactory.cs b/src/umbraco.cms/businesslogic/media/UmbracoFileMediaFactory.cs index 2f2c25b886..0be8d72a28 100644 --- a/src/umbraco.cms/businesslogic/media/UmbracoFileMediaFactory.cs +++ b/src/umbraco.cms/businesslogic/media/UmbracoFileMediaFactory.cs @@ -27,14 +27,14 @@ namespace umbraco.cms.businesslogic.media var propertyId = media.getProperty("umbracoFile").Id; // Get paths - var destFilePath = _fileSystem.GetRelativePath(propertyId, uploadedFile.FileName); + var destFilePath = FileSystem.GetRelativePath(propertyId, uploadedFile.FileName); var ext = Path.GetExtension(destFilePath).Substring(1); //var absoluteDestPath = HttpContext.Current.Server.MapPath(destPath); //var absoluteDestFilePath = HttpContext.Current.Server.MapPath(destFilePath); // Set media properties - media.getProperty("umbracoFile").Value = _fileSystem.GetUrl(destFilePath); + media.getProperty("umbracoFile").Value = FileSystem.GetUrl(destFilePath); media.getProperty("umbracoBytes").Value = uploadedFile.ContentLength; if (media.getProperty("umbracoExtension") != null) @@ -43,7 +43,7 @@ namespace umbraco.cms.businesslogic.media if (media.getProperty("umbracoExtensio") != null) media.getProperty("umbracoExtensio").Value = ext; - _fileSystem.AddFile(destFilePath, uploadedFile.InputStream, uploadedFile.ReplaceExisting); + FileSystem.AddFile(destFilePath, uploadedFile.InputStream, uploadedFile.ReplaceExisting); // Save media media.Save(); diff --git a/src/umbraco.cms/businesslogic/media/UmbracoImageMediaFactory.cs b/src/umbraco.cms/businesslogic/media/UmbracoImageMediaFactory.cs index eec6f81c53..4c5c25d6ff 100644 --- a/src/umbraco.cms/businesslogic/media/UmbracoImageMediaFactory.cs +++ b/src/umbraco.cms/businesslogic/media/UmbracoImageMediaFactory.cs @@ -37,11 +37,11 @@ namespace umbraco.cms.businesslogic.media var propertyId = media.getProperty("umbracoFile").Id; // Get paths - var destFilePath = _fileSystem.GetRelativePath(propertyId, postedFile.FileName); + var destFilePath = FileSystem.GetRelativePath(propertyId, postedFile.FileName); var ext = Path.GetExtension(destFilePath).Substring(1); // Set media properties - media.getProperty("umbracoFile").Value = _fileSystem.GetUrl(destFilePath); + media.getProperty("umbracoFile").Value = FileSystem.GetUrl(destFilePath); media.getProperty("umbracoWidth").Value = fileWidth; media.getProperty("umbracoHeight").Value = fileHeight; media.getProperty("umbracoBytes").Value = postedFile.ContentLength; @@ -61,7 +61,7 @@ namespace umbraco.cms.businesslogic.media image.Dispose(); - _fileSystem.AddFile(destFilePath, postedFile.InputStream, postedFile.ReplaceExisting); + FileSystem.AddFile(destFilePath, postedFile.InputStream, postedFile.ReplaceExisting); // Save media media.Save(); @@ -148,7 +148,7 @@ namespace umbraco.cms.businesslogic.media bp.Save(ms, codec, ep); ms.Seek(0, 0); - _fileSystem.AddFile(thumbnailFileName, ms); + FileSystem.AddFile(thumbnailFileName, ms); ms.Close(); } diff --git a/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs b/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs index 8b745653a8..14a4d982a8 100644 --- a/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs +++ b/src/umbraco.cms/businesslogic/media/UmbracoMediaFactory.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; -using System.IO; +using System.Globalization; using System.Linq; -using System.Text; -using System.Web; +using System.Text.RegularExpressions; +using System.Threading; using System.Xml; using Umbraco.Core.IO; using umbraco.BusinessLogic; @@ -16,11 +16,11 @@ namespace umbraco.cms.businesslogic.media public virtual int Priority { get { return 1000; } } public abstract string MediaTypeAlias { get; } - internal readonly IMediaFileSystem _fileSystem; + internal readonly IMediaFileSystem FileSystem; protected UmbracoMediaFactory() { - _fileSystem = FileSystemProviderManager.Current.GetFileSystemProvider(); + FileSystem = FileSystemProviderManager.Current.GetFileSystemProvider(); } public virtual bool CanHandleMedia(int parentNodeId, PostedMediaFile postedFile, User user) @@ -29,9 +29,7 @@ namespace umbraco.cms.businesslogic.media { var parentNode = new Media(parentNodeId); - return parentNodeId > -1 ? - user.Applications.Any(app => app.alias.ToLower() == "media") && (user.StartMediaId <= 0 || ("," + parentNode.Path + ",").Contains("," + user.StartMediaId + ",")) && parentNode.ContentType.AllowedChildContentTypeIDs.Contains(MediaType.GetByAlias(MediaTypeAlias).Id) : - true; + return parentNodeId <= -1 || user.Applications.Any(app => app.alias.ToLower() == "media") && (user.StartMediaId <= 0 || ("," + parentNode.Path + ",").Contains("," + user.StartMediaId + ",")) && parentNode.ContentType.AllowedChildContentTypeIDs.Contains(MediaType.GetByAlias(MediaTypeAlias).Id); } catch { @@ -45,7 +43,7 @@ namespace umbraco.cms.businesslogic.media Media media; string mediaName = !string.IsNullOrEmpty(postedFile.DisplayName) ? postedFile.DisplayName - : extractTitleFromFileName(postedFile.FileName); + : ExtractTitleFromFileName(postedFile.FileName); if (postedFile.ReplaceExisting && TryFindExistingMedia(parentNodeId, postedFile.FileName, out media)) { @@ -89,8 +87,8 @@ namespace umbraco.cms.businesslogic.media var prop = childMedia.getProperty("umbracoFile"); if (prop != null) { - var destFilePath = _fileSystem.GetRelativePath(prop.Id, fileName); - var destFileUrl = _fileSystem.GetUrl(destFilePath); + var destFilePath = FileSystem.GetRelativePath(prop.Id, fileName); + var destFileUrl = FileSystem.GetUrl(destFilePath); if (prop.Value.ToString() == destFileUrl) { @@ -105,29 +103,40 @@ namespace umbraco.cms.businesslogic.media return false; } - private string extractTitleFromFileName(string fileName) + private string ExtractTitleFromFileName(string fileName) { // change the name - string currentChar = String.Empty; - string curName = fileName.Substring(0, fileName.LastIndexOf(".")); - int curNameLength = curName.Length; - string friendlyName = String.Empty; - for (int i = 0; i < curNameLength; i++) + var curName = fileName.Substring(0, fileName.LastIndexOf(".", StringComparison.Ordinal)).ToCharArray(); + var curNameLength = curName.Length; + var friendlyName = String.Empty; + + for (var i = 0; i < curNameLength; i++) { - currentChar = curName.Substring(i, 1); - if (friendlyName.Length == 0) - currentChar = currentChar.ToUpper(); - - if (i < curNameLength - 1 && friendlyName != "" && curName.Substring(i - 1, 1) == " ") - currentChar = currentChar.ToUpper(); - else if (currentChar != " " && i < curNameLength - 1 && friendlyName != "" - && curName.Substring(i - 1, 1).ToUpper() != curName.Substring(i - 1, 1) - && currentChar.ToUpper() == currentChar) - friendlyName += " "; - - friendlyName += currentChar; - + var currentChar = curName[i]; + var currentString = String.Empty; + + if (Char.IsSeparator(currentChar) || Char.IsWhiteSpace(currentChar) || (Char.IsPunctuation(currentChar) + && (currentChar == '_' || currentChar == '-' || currentChar == '.' || currentChar == '%'))) + { + currentString = " "; + } + else if (Char.IsPunctuation(currentChar) || Char.IsLetterOrDigit(currentChar)) + { + currentString = currentChar.ToString(CultureInfo.InvariantCulture); + } + + friendlyName += currentString; } + + //Capitalize each first letter of a word + var cultureInfo = Thread.CurrentThread.CurrentCulture; + var textInfo = cultureInfo.TextInfo; + + friendlyName = textInfo.ToTitleCase(friendlyName); + + //Remove multiple consecutive spaces + var regex = new Regex(@"[ ]{2,}", RegexOptions.None); + friendlyName = regex.Replace(friendlyName, @" "); return friendlyName; }