Fixes U4-1444 so HttpPostedFiles are now properly accepted through the SetValue method.

Corrects an issue with the RootUrl in the PhysicalFileSystem.
This commit is contained in:
Morten Christensen
2013-01-14 11:02:12 -01:00
parent 98a171266e
commit 749a872fec
7 changed files with 145 additions and 19 deletions

View File

@@ -19,7 +19,7 @@ namespace Umbraco.Core.IO
public PhysicalFileSystem(string virtualRoot)
{
RootPath = IOHelper.MapPath(virtualRoot);
_rootUrl = VirtualPathUtility.ToAbsolute(virtualRoot);
_rootUrl = IOHelper.ResolveUrl(virtualRoot);
}
public PhysicalFileSystem(string rootPath, string rootUrl)

View File

@@ -5,6 +5,7 @@ using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Web;
using Umbraco.Core.Models.EntityBase;
namespace Umbraco.Core.Models
@@ -256,11 +257,93 @@ namespace Umbraco.Core.Models
}
/// <summary>
/// Sets the value of a Property
/// Sets the <see cref="System.Object"/> value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="value">Value to set for the Property</param>
public virtual void SetValue(string propertyTypeAlias, object value)
{
// .NET magic to call one of the 'SetPropertyValue' handlers with matching signature
((dynamic)this).SetPropertyValue(propertyTypeAlias, (dynamic)value);
}
/// <summary>
/// Sets the <see cref="System.String"/> value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="value">Value to set for the Property</param>
public virtual void SetPropertyValue(string propertyTypeAlias, string value)
{
SetValueOnProperty(propertyTypeAlias, value);
}
/// <summary>
/// Sets the <see cref="System.Int32"/> value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="value">Value to set for the Property</param>
public virtual void SetPropertyValue(string propertyTypeAlias, int value)
{
SetValueOnProperty(propertyTypeAlias, value);
}
/// <summary>
/// Sets the <see cref="System.Boolean"/> value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="value">Value to set for the Property</param>
public virtual void SetPropertyValue(string propertyTypeAlias, bool value)
{
int val = Convert.ToInt32(value);
SetValueOnProperty(propertyTypeAlias, val);
}
/// <summary>
/// Sets the <see cref="System.DateTime"/> value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="value">Value to set for the Property</param>
public virtual void SetPropertyValue(string propertyTypeAlias, DateTime value)
{
SetValueOnProperty(propertyTypeAlias, value);
}
/// <summary>
/// Sets the <see cref="System.Web.HttpPostedFile"/> value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="value">Value to set for the Property</param>
public virtual void SetPropertyValue(string propertyTypeAlias, HttpPostedFile value)
{
ContentExtensions.SetValue(this, propertyTypeAlias, value);
}
/// <summary>
/// Sets the <see cref="System.Web.HttpPostedFileBase"/> value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="value">Value to set for the Property</param>
public virtual void SetPropertyValue(string propertyTypeAlias, HttpPostedFileBase value)
{
ContentExtensions.SetValue(this, propertyTypeAlias, value);
}
/// <summary>
/// Sets the <see cref="System.Web.HttpPostedFileWrapper"/> value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="value">Value to set for the Property</param>
public virtual void SetPropertyValue(string propertyTypeAlias, HttpPostedFileWrapper value)
{
ContentExtensions.SetValue(this, propertyTypeAlias, value);
}
/// <summary>
/// Private method to set the value of a property
/// </summary>
/// <param name="propertyTypeAlias"></param>
/// <param name="value"></param>
private void SetValueOnProperty(string propertyTypeAlias, object value)
{
if (Properties.Contains(propertyTypeAlias))
{

View File

@@ -2,6 +2,7 @@
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
@@ -53,13 +54,14 @@ namespace Umbraco.Core.Models
}
}
/*
/// <summary>
/// Sets and uploads the file from a HttpPostedFileBase object as the property value
/// </summary>
/// <param name="media"><see cref="IMedia"/> to add property value to</param>
/// <param name="propertyTypeAlias">Alias of the property to save the value on</param>
/// <param name="value">The <see cref="HttpPostedFileBase"/> containing the file that will be uploaded</param>
public static void SetValue(this IMedia media, string propertyTypeAlias, HttpPostedFileBase value)
public static void SetPropertyValue(this IMedia media, string propertyTypeAlias, HttpPostedFileBase value)
{
var name =
IOHelper.SafeFileName(
@@ -77,7 +79,7 @@ namespace Umbraco.Core.Models
/// <param name="media"><see cref="IMedia"/> to add property value to</param>
/// <param name="propertyTypeAlias">Alias of the property to save the value on</param>
/// <param name="value">The <see cref="HttpPostedFile"/> containing the file that will be uploaded</param>
public static void SetValue(this IMedia media, string propertyTypeAlias, HttpPostedFile value)
public static void SetPropertyValue(this IMedia media, string propertyTypeAlias, HttpPostedFile value)
{
var name =
IOHelper.SafeFileName(
@@ -95,19 +97,19 @@ namespace Umbraco.Core.Models
/// <param name="media"><see cref="IMedia"/> to add property value to</param>
/// <param name="propertyTypeAlias">Alias of the property to save the value on</param>
/// <param name="value">The <see cref="HttpPostedFileWrapper"/> containing the file that will be uploaded</param>
public static void SetValue(this IMedia media, string propertyTypeAlias, HttpPostedFileWrapper value)
public static void SetPropertyValue(this IMedia media, string propertyTypeAlias, HttpPostedFileWrapper value)
{
if (string.IsNullOrEmpty(value.FileName) == false)
SetFileOnContent(media, propertyTypeAlias, value.FileName, value.InputStream);
}
*/
/// <summary>
/// Sets and uploads the file from a HttpPostedFileBase object as the property value
/// </summary>
/// <param name="content"><see cref="IContent"/> to add property value to</param>
/// <param name="propertyTypeAlias">Alias of the property to save the value on</param>
/// <param name="value">The <see cref="HttpPostedFileBase"/> containing the file that will be uploaded</param>
public static void SetValue(this IContent content, string propertyTypeAlias, HttpPostedFileBase value)
public static void SetValue(this IContentBase content, string propertyTypeAlias, HttpPostedFileBase value)
{
var name =
IOHelper.SafeFileName(
@@ -125,7 +127,7 @@ namespace Umbraco.Core.Models
/// <param name="content"><see cref="IContent"/> to add property value to</param>
/// <param name="propertyTypeAlias">Alias of the property to save the value on</param>
/// <param name="value">The <see cref="HttpPostedFile"/> containing the file that will be uploaded</param>
public static void SetValue(this IContent content, string propertyTypeAlias, HttpPostedFile value)
public static void SetValue(this IContentBase content, string propertyTypeAlias, HttpPostedFile value)
{
var name =
IOHelper.SafeFileName(
@@ -143,7 +145,7 @@ namespace Umbraco.Core.Models
/// <param name="content"><see cref="IContent"/> to add property value to</param>
/// <param name="propertyTypeAlias">Alias of the property to save the value on</param>
/// <param name="value">The <see cref="HttpPostedFileWrapper"/> containing the file that will be uploaded</param>
public static void SetValue(this IContent content, string propertyTypeAlias, HttpPostedFileWrapper value)
public static void SetValue(this IContentBase content, string propertyTypeAlias, HttpPostedFileWrapper value)
{
if (string.IsNullOrEmpty(value.FileName) == false)
SetFileOnContent(content, propertyTypeAlias, value.FileName, value.InputStream);
@@ -208,8 +210,8 @@ namespace Umbraco.Core.Models
//Only add dimensions to web images
if (supportsResizing)
{
SetPropertyValue(content, uploadFieldConfigNode, "widthFieldAlias", GetDimensions(fs, fileName).Item1);
SetPropertyValue(content, uploadFieldConfigNode, "heightFieldAlias", GetDimensions(fs, fileName).Item2);
SetPropertyValue(content, uploadFieldConfigNode, "widthFieldAlias", GetDimensions(fs, fileName).Item1.ToString(CultureInfo.InvariantCulture));
SetPropertyValue(content, uploadFieldConfigNode, "heightFieldAlias", GetDimensions(fs, fileName).Item2.ToString(CultureInfo.InvariantCulture));
}
else
{
@@ -217,7 +219,7 @@ namespace Umbraco.Core.Models
SetPropertyValue(content, uploadFieldConfigNode, "heightFieldAlias", string.Empty);
}
SetPropertyValue(content, uploadFieldConfigNode, "lengthFieldAlias", fs.GetSize(fileName));
SetPropertyValue(content, uploadFieldConfigNode, "lengthFieldAlias", fs.GetSize(fileName).ToString(CultureInfo.InvariantCulture));
SetPropertyValue(content, uploadFieldConfigNode, "extensionFieldAlias", extension);
}
}
@@ -226,7 +228,7 @@ namespace Umbraco.Core.Models
property.Value = fs.GetUrl(fileName);
}
private static void SetPropertyValue(IContentBase content, XmlNode uploadFieldConfigNode, string propertyAlias, object propertyValue)
private static void SetPropertyValue(IContentBase content, XmlNode uploadFieldConfigNode, string propertyAlias, string propertyValue)
{
XmlNode propertyNode = uploadFieldConfigNode.SelectSingleNode(propertyAlias);
if (propertyNode != null && string.IsNullOrEmpty(propertyNode.FirstChild.Value) == false)

View File

@@ -66,7 +66,7 @@ namespace Umbraco.Core.Models
TPassType GetValue<TPassType>(string propertyTypeAlias);
/// <summary>
/// Sets the value of a Property
/// Sets the <see cref="System.Object"/> value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="value">Value to set for the Property</param>

View File

@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using NUnit.Framework;
using Rhino.Mocks;
using Umbraco.Core.Models;
using Umbraco.Tests.TestHelpers.Entities;
@@ -39,6 +42,46 @@ namespace Umbraco.Tests.Models
Assert.That(content.Properties["title"].Value, Is.EqualTo("This is the new title"));
}
[Test]
public void Can_Set_Property_Value_As_String()
{
// Arrange
var contentType = MockedContentTypes.CreateTextpageContentType();
var content = MockedContent.CreateTextpageContent(contentType, "Textpage", -1);
// Act
content.SetValue("title", "This is the new title");
// Assert
Assert.That(content.Properties.Any(), Is.True);
Assert.That(content.Properties["title"], Is.Not.Null);
Assert.That(content.Properties["title"].Value, Is.EqualTo("This is the new title"));
}
[Test]
public void Can_Set_Property_Value_As_HttpPostedFileBase()
{
// Arrange
var contentType = MockedContentTypes.CreateTextpageContentType();
var content = MockedContent.CreateTextpageContent(contentType, "Textpage", -1);
var stream = new MemoryStream(System.Text.Encoding.Default.GetBytes("TestContent"));
var httpPostedFileBase = MockRepository.GenerateMock<HttpPostedFileBase>();
httpPostedFileBase.Stub(x => x.ContentLength).Return(Convert.ToInt32(stream.Length));
httpPostedFileBase.Stub(x => x.ContentType).Return("text/plain");
httpPostedFileBase.Stub(x => x.FileName).Return("sample.txt");
httpPostedFileBase.Stub(x => x.InputStream).Return(stream);
// Assert
content.SetValue("title", httpPostedFileBase);
// Assert
Assert.That(content.Properties.Any(), Is.True);
Assert.That(content.Properties["title"], Is.Not.Null);
Assert.That(content.Properties["title"].Value, Is.StringContaining("sample.txt"));
}
[Test]
public void Can_Clone_Content()
{

View File

@@ -5,8 +5,6 @@ using System.Xml;
using Umbraco.Core.IO;
using umbraco.cms.businesslogic.Files;
using umbraco.cms.businesslogic.property;
using IContent = Umbraco.Core.Models.IContent;
using IMedia = Umbraco.Core.Models.IMedia;
namespace umbraco.cms.businesslogic.datatype
{

View File

@@ -966,9 +966,6 @@ namespace umbraco.cms.businesslogic.web
/// <returns></returns>
public bool SaveAndPublish(User u)
{
var e = new SaveEventArgs();
FireBeforeSave(e);
foreach (var property in GenericProperties)
{
if(property.Value == null)
@@ -977,6 +974,9 @@ namespace umbraco.cms.businesslogic.web
Content.SetValue(property.PropertyType.Alias, property.Value);
}
var e = new SaveEventArgs();
FireBeforeSave(e);
if (!e.Cancel)
{
var result = ApplicationContext.Current.Services.ContentService.SaveAndPublish(Content, u.Id, true);