Fixes U4-1368 so the FileHandlerData doesn't handle setting properties for Document and Media.

Properties using the UploadField will have its value as well as related values updated in a BeforeSave event.
This commit is contained in:
Morten Christensen
2013-01-02 14:28:15 -01:00
parent 0312caa166
commit 169db0c15f
9 changed files with 163 additions and 64 deletions

View File

@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbraco.Core.CodeAnnotations;
using Umbraco.Core.CodeAnnotations;
namespace Umbraco.Core.IO
{
@@ -10,7 +6,7 @@ namespace Umbraco.Core.IO
public static class FileSystemExtensions
{
[UmbracoExperimentalFeature("", "Will be declared public after 4.10")]
internal static long GetSize(this IFileSystem fs, string path)
internal static long GetSize(this IFileSystem fs, string path)
{
var s = fs.OpenFile(path);
var size = s.Length;
@@ -20,7 +16,7 @@ namespace Umbraco.Core.IO
}
[UmbracoExperimentalFeature("", "Will be declared public after 4.10")]
internal static void CopyFile(this IFileSystem fs, string path, string newPath)
internal static void CopyFile(this IFileSystem fs, string path, string newPath)
{
fs.AddFile(newPath, fs.OpenFile(path));
}

View File

@@ -6,7 +6,7 @@ using Umbraco.Core.CodeAnnotations;
namespace Umbraco.Core.IO
{
[UmbracoExperimentalFeature("http://issues.umbraco.org/issue/U4-1156", "Will be declared public after 4.10")]
internal interface IFileSystem
internal interface IFileSystem
{
IEnumerable<string> GetDirectories(string path);

View File

@@ -102,7 +102,7 @@ namespace Umbraco.Core.Models
//If Content is trashed the parent id should be set to that of the RecycleBin
if (isTrashed)
{
ParentId = -20;
ParentId = -21;
}
else//otherwise set the parent id to the optional parameter, -1 being the fallback
{

View File

@@ -0,0 +1,101 @@
using System;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml;
using Umbraco.Core.Configuration;
using Umbraco.Core.IO;
using Umbraco.Core.Models;
using umbraco.cms.businesslogic.Files;
using umbraco.interfaces;
namespace Umbraco.Web.Strategies.DataTypes
{
/// <summary>
/// Before Save Content/Media subscriber that checks for Upload fields and updates related fields accordingly.
/// </summary>
/// <remarks>
/// This is an intermediate fix for the legacy DataTypeUploadField and the FileHandlerData, so that properties
/// are saved correctly when using the Upload field on a (legacy) Document or Media class.
/// </remarks>
public class LegacyUploadFieldWorkaround : IApplicationStartupHandler
{
public LegacyUploadFieldWorkaround()
{
global::umbraco.cms.businesslogic.media.Media.BeforeSave += Media_BeforeSave;
global::umbraco.cms.businesslogic.web.Document.BeforeSave += Document_BeforeSave;
}
void Document_BeforeSave(global::umbraco.cms.businesslogic.web.Document sender, global::umbraco.cms.businesslogic.SaveEventArgs e)
{
if (UmbracoSettings.ImageAutoFillImageProperties != null)
{
var property =
sender.GenericProperties.FirstOrDefault(x => x.PropertyType.DataTypeDefinition.DataType.Id ==
new Guid("5032a6e6-69e3-491d-bb28-cd31cd11086c"));
if (property == null)
return;
FillProperties(sender.Content, property);
}
}
void Media_BeforeSave(global::umbraco.cms.businesslogic.media.Media sender, global::umbraco.cms.businesslogic.SaveEventArgs e)
{
if (UmbracoSettings.ImageAutoFillImageProperties != null)
{
var property =
sender.GenericProperties.FirstOrDefault(x => x.PropertyType.DataTypeDefinition.DataType.Id ==
new Guid("5032a6e6-69e3-491d-bb28-cd31cd11086c"));
if(property == null)
return;
FillProperties(sender.MediaItem, property);
}
}
private void FillProperties(IContentBase content, global::umbraco.cms.businesslogic.property.Property property)
{
XmlNode uploadFieldConfigNode =
global::umbraco.UmbracoSettings.ImageAutoFillImageProperties.SelectSingleNode(
string.Format("uploadField [@alias = \"{0}\"]", property.PropertyType.Alias));
if (uploadFieldConfigNode != null)
{
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
string path = string.IsNullOrEmpty(property.Value.ToString())
? string.Empty
: VirtualPathUtility.ToAbsolute(property.Value.ToString(), SystemDirectories.Root)
.Replace(fs.GetUrl(""), "");
var file = string.IsNullOrEmpty(path)
? new UmbracoFile()
: new UmbracoFile(path);
// only add dimensions to web images
UpdateProperty(uploadFieldConfigNode, content, "widthFieldAlias",
file.SupportsResizing ? file.GetDimensions().Item1.ToString() : string.Empty);
UpdateProperty(uploadFieldConfigNode, content, "heightFieldAlias",
file.SupportsResizing ? file.GetDimensions().Item2.ToString() : string.Empty);
UpdateProperty(uploadFieldConfigNode, content, "lengthFieldAlias", file.Length == default(long) ? string.Empty : file.Length.ToString());
UpdateProperty(uploadFieldConfigNode, content, "extensionFieldAlias", string.IsNullOrEmpty(file.Extension) ? string.Empty : file.Extension);
}
}
private void UpdateProperty(XmlNode uploadFieldConfigNode, IContentBase content, string propertyAlias,
object propertyValue)
{
XmlNode propertyNode = uploadFieldConfigNode.SelectSingleNode(propertyAlias);
if (propertyNode != null && !String.IsNullOrEmpty(propertyNode.FirstChild.Value))
{
if (content.Properties[propertyNode.FirstChild.Value] != null)
{
content.SetValue(propertyNode.FirstChild.Value, propertyValue);
}
}
}
}
}

View File

@@ -328,6 +328,7 @@
<Compile Include="RouteCollectionExtensions.cs" />
<Compile Include="Routing\LookupByPageIdQuery.cs" />
<Compile Include="Mvc\SurfaceControllerResolver.cs" />
<Compile Include="Strategies\DataTypes\LegacyUploadFieldWorkaround.cs" />
<Compile Include="Strategies\Publishing\UpdateCacheAfterPublish.cs" />
<Compile Include="Strategies\Publishing\UpdateCacheAfterUnPublish.cs" />
<Compile Include="Strategies\Migrations\EnsureAppsTreesUpdatedOnUpgrade.cs" />

View File

@@ -38,7 +38,7 @@ namespace umbraco.cms.businesslogic
private string _contentTypeIcon;
private ContentType _contentType;
private Properties m_LoadedProperties = null;
private IContentBase _contentBase;
protected internal IContentBase ContentBase;
#endregion
@@ -55,9 +55,9 @@ namespace umbraco.cms.businesslogic
protected internal Content(IContentBase contentBase)
: base(contentBase)
{
_contentBase = contentBase;
_version = _contentBase.Version;
_versionDate = _contentBase.UpdateDate;
ContentBase = contentBase;
_version = ContentBase.Version;
_versionDate = ContentBase.UpdateDate;
_versionDateInitialized = true;
}
@@ -646,9 +646,9 @@ namespace umbraco.cms.businesslogic
{
m_LoadedProperties = new Properties();
if (_contentBase != null)
if (ContentBase != null)
{
m_LoadedProperties.AddRange(_contentBase.Properties.Select(x => new Property(x)));
m_LoadedProperties.AddRange(ContentBase.Properties.Select(x => new Property(x)));
return;
}

View File

@@ -1,11 +1,12 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
using umbraco.cms.businesslogic.Files;
using umbraco.cms.businesslogic.property;
using umbraco.IO;
using IContent = Umbraco.Core.Models.IContent;
using IMedia = Umbraco.Core.Models.IMedia;
namespace umbraco.cms.businesslogic.datatype
{
@@ -83,31 +84,21 @@ namespace umbraco.cms.businesslogic.datatype
}
// check for auto fill of other properties (width, height, extension and filesize)
string propertyTypeAlias = new Property(PropertyId).PropertyType.Alias;
if (UmbracoSettings.ImageAutoFillImageProperties != null)
{
string propertyTypeAlias = new Property(PropertyId).PropertyType.Alias;
XmlNode uploadFieldConfigNode =
UmbracoSettings.ImageAutoFillImageProperties.SelectSingleNode(
string.Format("uploadField [@alias = \"{0}\"]", propertyTypeAlias));
if (uploadFieldConfigNode != null)
{
// get the current document
Content content = Content.GetContentFromVersion(Version);
// only add dimensions to web images
if (um.SupportsResizing)
var legacy = Content.GetContentFromVersion(Version);
if (legacy.ContentBase is IContent == false && legacy.ContentBase is IMedia == false)
{
updateContentProperty(uploadFieldConfigNode, content, "widthFieldAlias",
um.GetDimensions().Item1);
updateContentProperty(uploadFieldConfigNode, content, "heightFieldAlias",
um.GetDimensions().Item2);
FillProperties(uploadFieldConfigNode, legacy, um);
}
else
{
updateContentProperty(uploadFieldConfigNode, content, "widthFieldAlias", String.Empty);
updateContentProperty(uploadFieldConfigNode, content, "heightFieldAlias", String.Empty);
}
updateContentProperty(uploadFieldConfigNode, content, "lengthFieldAlias", um.Length);
updateContentProperty(uploadFieldConfigNode, content, "extensionFieldAlias", um.Extension);
}
}
@@ -129,7 +120,7 @@ namespace umbraco.cms.businesslogic.datatype
}
}
}
private void clearRelatedValues()
{
string propertyTypeAlias = new Property(PropertyId).PropertyType.Alias;
@@ -141,16 +132,26 @@ namespace umbraco.cms.businesslogic.datatype
if (uploadFieldConfigNode != null)
{
// get the current document
Content content = Content.GetContentFromVersion(Version);
Content legacy = Content.GetContentFromVersion(Version);
// only add dimensions to web images
updateContentProperty(uploadFieldConfigNode, content, "widthFieldAlias", String.Empty);
updateContentProperty(uploadFieldConfigNode, content, "heightFieldAlias", String.Empty);
updateContentProperty(uploadFieldConfigNode, content, "lengthFieldAlias", String.Empty);
updateContentProperty(uploadFieldConfigNode, content, "extensionFieldAlias", String.Empty);
updateContentProperty(uploadFieldConfigNode, legacy, "widthFieldAlias", String.Empty);
updateContentProperty(uploadFieldConfigNode, legacy, "heightFieldAlias", String.Empty);
updateContentProperty(uploadFieldConfigNode, legacy, "lengthFieldAlias", String.Empty);
updateContentProperty(uploadFieldConfigNode, legacy, "extensionFieldAlias", String.Empty);
}
}
}
private void FillProperties(XmlNode uploadFieldConfigNode, Content content, UmbracoFile um)
{
// only add dimensions to web images
updateContentProperty(uploadFieldConfigNode, content, "widthFieldAlias", um.SupportsResizing ? um.GetDimensions().Item1.ToString() : string.Empty);
updateContentProperty(uploadFieldConfigNode, content, "heightFieldAlias", um.SupportsResizing ? um.GetDimensions().Item2.ToString() : string.Empty);
updateContentProperty(uploadFieldConfigNode, content, "lengthFieldAlias", um.Length);
updateContentProperty(uploadFieldConfigNode, content, "extensionFieldAlias", um.Extension);
}
private void updateContentProperty(XmlNode uploadFieldConfigNode, Content content, string propertyAlias,
object propertyValue)
{

View File

@@ -1,7 +1,6 @@
using System;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.Rdbms;
using umbraco.DataLayer;
using System.Collections;
using System.Collections.Generic;
@@ -20,7 +19,7 @@ namespace umbraco.cms.businesslogic.media
{
#region Constants and static members
private IMedia _media;
protected internal IMedia MediaItem;
private const string m_SQLOptimizedMany = @"
select
count(children.id) as children, cmsContentType.isContainer, umbracoNode.id, umbracoNode.uniqueId, umbracoNode.level, umbracoNode.parentId, umbracoNode.path, umbracoNode.sortOrder, umbracoNode.createDate, umbracoNode.nodeUser, umbracoNode.text,
@@ -207,17 +206,17 @@ namespace umbraco.cms.businesslogic.media
{
get
{
return _media == null ? base.sortOrder : _media.SortOrder;
return MediaItem == null ? base.sortOrder : MediaItem.SortOrder;
}
set
{
if (_media == null)
if (MediaItem == null)
{
base.sortOrder = value;
}
else
{
_media.SortOrder = value;
MediaItem.SortOrder = value;
}
}
}
@@ -226,17 +225,17 @@ namespace umbraco.cms.businesslogic.media
{
get
{
return _media == null ? base.Level : _media.Level;
return MediaItem == null ? base.Level : MediaItem.Level;
}
set
{
if (_media == null)
if (MediaItem == null)
{
base.Level = value;
}
else
{
_media.Level = value;
MediaItem.Level = value;
}
}
}
@@ -245,7 +244,7 @@ namespace umbraco.cms.businesslogic.media
{
get
{
return _media == null ? base.ParentId : _media.ParentId;
return MediaItem == null ? base.ParentId : MediaItem.ParentId;
}
}
@@ -253,17 +252,17 @@ namespace umbraco.cms.businesslogic.media
{
get
{
return _media == null ? base.Path : _media.Path;
return MediaItem == null ? base.Path : MediaItem.Path;
}
set
{
if (_media == null)
if (MediaItem == null)
{
base.Path = value;
}
else
{
_media.Path = value;
MediaItem.Path = value;
}
}
}
@@ -297,14 +296,14 @@ namespace umbraco.cms.businesslogic.media
foreach (var property in GenericProperties)
{
_media.SetValue(property.PropertyType.Alias, property.Value);
MediaItem.SetValue(property.PropertyType.Alias, property.Value);
}
FireBeforeSave(e);
if (!e.Cancel)
{
ApplicationContext.Current.Services.MediaService.Save(_media);
ApplicationContext.Current.Services.MediaService.Save(MediaItem);
base.Save();
@@ -417,14 +416,14 @@ namespace umbraco.cms.businesslogic.media
#region Private methods
private void SetupNode(IMedia media)
{
_media = media;
MediaItem = media;
//Setting private properties from IContentBase replacing CMSNode.setupNode() / CMSNode.PopulateCMSNodeFromReader()
base.PopulateCMSNodeFromContentBase(_media, _objectType);
base.PopulateCMSNodeFromContentBase(MediaItem, _objectType);
//If the version is empty we update with the latest version from the current IContent.
if (Version == Guid.Empty)
Version = _media.Version;
Version = MediaItem.Version;
}
[Obsolete("Deprecated, This method is no longer needed", false)]
@@ -458,9 +457,9 @@ namespace umbraco.cms.businesslogic.media
// Remove all files
//DeleteAssociatedMediaFiles();
if (_media != null)
if (MediaItem != null)
{
ApplicationContext.Current.Services.MediaService.Delete(_media);
ApplicationContext.Current.Services.MediaService.Delete(MediaItem);
}
else
{
@@ -487,9 +486,9 @@ namespace umbraco.cms.businesslogic.media
if (!e.Cancel)
{
if (_media != null)
if (MediaItem != null)
{
ApplicationContext.Current.Services.MediaService.MoveToRecycleBin(_media);
ApplicationContext.Current.Services.MediaService.MoveToRecycleBin(MediaItem);
}
else
{

View File

@@ -931,15 +931,16 @@ namespace umbraco.cms.businesslogic.web
public override void Save()
{
var e = new SaveEventArgs();
foreach (var property in GenericProperties)
{
Content.SetValue(property.PropertyType.Alias, property.Value);
}
FireBeforeSave(e);
if (!e.Cancel)
{
foreach (var property in GenericProperties)
{
Content.SetValue(property.PropertyType.Alias, property.Value);
}
ApplicationContext.Current.Services.ContentService.Save(Content);
base.Save();