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
This commit is contained in:
sebastiaan
2012-09-19 12:45:11 -02:00
parent 982ba0f4b0
commit ce0923cfc6
6 changed files with 64 additions and 53 deletions

View File

@@ -202,5 +202,14 @@ namespace Umbraco.Core.IO
}
/// <summary>
/// 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.
/// </summary>
/// <param name="filePath">The filename passed to the file handler from the upload field.</param>
/// <returns>A safe filename without any path specific chars.</returns>
internal static string SafeFileName(string filePath)
{
return !String.IsNullOrEmpty(filePath) ? Regex.Replace(filePath, @"[^a-zA-Z0-9\-\.\/\:]{1}", "_") : String.Empty;
}
}
}

View File

@@ -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,

View File

@@ -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
}
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="filePath">The filename passed to the file handler from the upload field.</param>
/// <returns>A safe filename without any path specific chars.</returns>
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;

View File

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

View File

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

View File

@@ -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<IMediaFileSystem>();
FileSystem = FileSystemProviderManager.Current.GetFileSystemProvider<IMediaFileSystem>();
}
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;
}