From 749a872fec963d6bb27ceb9ba97fd96c439fdee2 Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Mon, 14 Jan 2013 11:02:12 -0100 Subject: [PATCH] Fixes U4-1444 so HttpPostedFiles are now properly accepted through the SetValue method. Corrects an issue with the RootUrl in the PhysicalFileSystem. --- src/Umbraco.Core/IO/PhysicalFileSystem.cs | 2 +- src/Umbraco.Core/Models/ContentBase.cs | 85 ++++++++++++++++++- src/Umbraco.Core/Models/ContentExtensions.cs | 24 +++--- src/Umbraco.Core/Models/IContentBase.cs | 2 +- src/Umbraco.Tests/Models/ContentTests.cs | 43 ++++++++++ .../businesslogic/datatype/FileHandlerData.cs | 2 - src/umbraco.cms/businesslogic/web/Document.cs | 6 +- 7 files changed, 145 insertions(+), 19 deletions(-) diff --git a/src/Umbraco.Core/IO/PhysicalFileSystem.cs b/src/Umbraco.Core/IO/PhysicalFileSystem.cs index c7f9f734b8..fa02f39c4f 100644 --- a/src/Umbraco.Core/IO/PhysicalFileSystem.cs +++ b/src/Umbraco.Core/IO/PhysicalFileSystem.cs @@ -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) diff --git a/src/Umbraco.Core/Models/ContentBase.cs b/src/Umbraco.Core/Models/ContentBase.cs index a8b8aa080b..4e785c3a2d 100644 --- a/src/Umbraco.Core/Models/ContentBase.cs +++ b/src/Umbraco.Core/Models/ContentBase.cs @@ -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 } /// - /// Sets the value of a Property + /// Sets the value of a Property /// /// Alias of the PropertyType /// Value to set for the Property 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); + } + + /// + /// Sets the value of a Property + /// + /// Alias of the PropertyType + /// Value to set for the Property + public virtual void SetPropertyValue(string propertyTypeAlias, string value) + { + SetValueOnProperty(propertyTypeAlias, value); + } + + /// + /// Sets the value of a Property + /// + /// Alias of the PropertyType + /// Value to set for the Property + public virtual void SetPropertyValue(string propertyTypeAlias, int value) + { + SetValueOnProperty(propertyTypeAlias, value); + } + + /// + /// Sets the value of a Property + /// + /// Alias of the PropertyType + /// Value to set for the Property + public virtual void SetPropertyValue(string propertyTypeAlias, bool value) + { + int val = Convert.ToInt32(value); + SetValueOnProperty(propertyTypeAlias, val); + } + + /// + /// Sets the value of a Property + /// + /// Alias of the PropertyType + /// Value to set for the Property + public virtual void SetPropertyValue(string propertyTypeAlias, DateTime value) + { + SetValueOnProperty(propertyTypeAlias, value); + } + + /// + /// Sets the value of a Property + /// + /// Alias of the PropertyType + /// Value to set for the Property + public virtual void SetPropertyValue(string propertyTypeAlias, HttpPostedFile value) + { + ContentExtensions.SetValue(this, propertyTypeAlias, value); + } + + /// + /// Sets the value of a Property + /// + /// Alias of the PropertyType + /// Value to set for the Property + public virtual void SetPropertyValue(string propertyTypeAlias, HttpPostedFileBase value) + { + ContentExtensions.SetValue(this, propertyTypeAlias, value); + } + + /// + /// Sets the value of a Property + /// + /// Alias of the PropertyType + /// Value to set for the Property + public virtual void SetPropertyValue(string propertyTypeAlias, HttpPostedFileWrapper value) + { + ContentExtensions.SetValue(this, propertyTypeAlias, value); + } + + /// + /// Private method to set the value of a property + /// + /// + /// + private void SetValueOnProperty(string propertyTypeAlias, object value) { if (Properties.Contains(propertyTypeAlias)) { diff --git a/src/Umbraco.Core/Models/ContentExtensions.cs b/src/Umbraco.Core/Models/ContentExtensions.cs index d14c023ae6..1ed91eb59a 100644 --- a/src/Umbraco.Core/Models/ContentExtensions.cs +++ b/src/Umbraco.Core/Models/ContentExtensions.cs @@ -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 } } + /* /// /// Sets and uploads the file from a HttpPostedFileBase object as the property value /// /// to add property value to /// Alias of the property to save the value on /// The containing the file that will be uploaded - 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 /// to add property value to /// Alias of the property to save the value on /// The containing the file that will be uploaded - 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 /// to add property value to /// Alias of the property to save the value on /// The containing the file that will be uploaded - 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); } - + */ /// /// Sets and uploads the file from a HttpPostedFileBase object as the property value /// /// to add property value to /// Alias of the property to save the value on /// The containing the file that will be uploaded - 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 /// to add property value to /// Alias of the property to save the value on /// The containing the file that will be uploaded - 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 /// to add property value to /// Alias of the property to save the value on /// The containing the file that will be uploaded - 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) diff --git a/src/Umbraco.Core/Models/IContentBase.cs b/src/Umbraco.Core/Models/IContentBase.cs index 3303a4dafc..42174ee757 100644 --- a/src/Umbraco.Core/Models/IContentBase.cs +++ b/src/Umbraco.Core/Models/IContentBase.cs @@ -66,7 +66,7 @@ namespace Umbraco.Core.Models TPassType GetValue(string propertyTypeAlias); /// - /// Sets the value of a Property + /// Sets the value of a Property /// /// Alias of the PropertyType /// Value to set for the Property diff --git a/src/Umbraco.Tests/Models/ContentTests.cs b/src/Umbraco.Tests/Models/ContentTests.cs index 4159380860..96a6b3acc8 100644 --- a/src/Umbraco.Tests/Models/ContentTests.cs +++ b/src/Umbraco.Tests/Models/ContentTests.cs @@ -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.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() { diff --git a/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs b/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs index eeb934cc9f..067bef6626 100644 --- a/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs +++ b/src/umbraco.cms/businesslogic/datatype/FileHandlerData.cs @@ -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 { diff --git a/src/umbraco.cms/businesslogic/web/Document.cs b/src/umbraco.cms/businesslogic/web/Document.cs index 52f40d639c..d95f330055 100644 --- a/src/umbraco.cms/businesslogic/web/Document.cs +++ b/src/umbraco.cms/businesslogic/web/Document.cs @@ -966,9 +966,6 @@ namespace umbraco.cms.businesslogic.web /// 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);