From a05c71ec859342fe2b45ce474bad97c3ad21d69a Mon Sep 17 00:00:00 2001 From: Stephan Date: Fri, 11 Dec 2015 17:22:47 +0100 Subject: [PATCH] Fix files and medias upload --- .../Media/UploadAutoFillProperties.cs | 20 ++++++++++++++----- src/Umbraco.Tests/Models/MediaXmlTest.cs | 11 ++++++++++ .../Querying/ContentTypeSqlMappingTests.cs | 8 ++++---- .../FileUploadPropertyEditor.cs | 2 +- .../ImageCropperPropertyEditor.cs | 2 +- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/Umbraco.Core/Media/UploadAutoFillProperties.cs b/src/Umbraco.Core/Media/UploadAutoFillProperties.cs index c7157c4eb6..553c994599 100644 --- a/src/Umbraco.Core/Media/UploadAutoFillProperties.cs +++ b/src/Umbraco.Core/Media/UploadAutoFillProperties.cs @@ -5,6 +5,7 @@ using System.Linq; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.IO; +using Umbraco.Core.Logging; using Umbraco.Core.Models; namespace Umbraco.Core.Media @@ -121,12 +122,21 @@ namespace Umbraco.Core.Media } else { - var fs = FileSystemProviderManager.Current.GetFileSystemProvider(); - using (var filestream = fs.OpenFile(filepath)) + // if anything goes wrong, just reset the properties + try { - var extension = (Path.GetExtension(filepath) ?? "").TrimStart('.'); - var size = ImageHelper.IsImageFile(extension) ? (Size?) ImageHelper.GetDimensions(filestream) : null; - SetProperties(content, autoFillConfig, size, filestream.Length, extension); + using (var filestream = MediaHelper.FileSystem.OpenFile(filepath)) + { + var extension = (Path.GetExtension(filepath) ?? "").TrimStart('.'); + var size = ImageHelper.IsImageFile(extension) ? (Size?)ImageHelper.GetDimensions(filestream) : null; + SetProperties(content, autoFillConfig, size, filestream.Length, extension); + } + } + catch (Exception ex) + { + LogHelper.Error(typeof(UploadAutoFillProperties), "Could not populate upload auto-fill properties for file \"" + + filepath + "\".", ex); + ResetProperties(content, autoFillConfig); } } } diff --git a/src/Umbraco.Tests/Models/MediaXmlTest.cs b/src/Umbraco.Tests/Models/MediaXmlTest.cs index a5db651115..bd711d2191 100644 --- a/src/Umbraco.Tests/Models/MediaXmlTest.cs +++ b/src/Umbraco.Tests/Models/MediaXmlTest.cs @@ -9,6 +9,7 @@ using Umbraco.Tests.TestHelpers; using Umbraco.Tests.TestHelpers.Entities; using umbraco.editorControls.tinyMCE3; using umbraco.interfaces; +using Umbraco.Web.PropertyEditors; namespace Umbraco.Tests.Models { @@ -44,9 +45,19 @@ namespace Umbraco.Tests.Models var mediaType = MockedContentTypes.CreateImageMediaType("image2"); ServiceContext.ContentTypeService.Save(mediaType); + // reference, so static ctor runs, so event handlers register + // and then, this will reset the width, height... because the file does not exist, of course ;-( + var ignored = new FileUploadPropertyEditor(); + var media = MockedMedia.CreateMediaImage(mediaType, -1); ServiceContext.MediaService.Save(media, 0); + // so we have to force-reset these values because the property editor has cleared them + media.SetValue(Constants.Conventions.Media.Width, "200"); + media.SetValue(Constants.Conventions.Media.Height, "200"); + media.SetValue(Constants.Conventions.Media.Bytes, "100"); + media.SetValue(Constants.Conventions.Media.Extension, "png"); + var nodeName = media.ContentType.Alias.ToSafeAliasWithForcingCheck(); var urlName = media.GetUrlSegment(); diff --git a/src/Umbraco.Tests/Persistence/Querying/ContentTypeSqlMappingTests.cs b/src/Umbraco.Tests/Persistence/Querying/ContentTypeSqlMappingTests.cs index 916b6d3333..caf872a995 100644 --- a/src/Umbraco.Tests/Persistence/Querying/ContentTypeSqlMappingTests.cs +++ b/src/Umbraco.Tests/Persistence/Querying/ContentTypeSqlMappingTests.cs @@ -151,10 +151,10 @@ namespace Umbraco.Tests.Persistence.Querying DatabaseContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF ", SqlSyntaxContext.SqlSyntaxProvider.GetQuotedTableName("cmsContentType")))); DatabaseContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", SqlSyntaxContext.SqlSyntaxProvider.GetQuotedTableName("cmsPropertyTypeGroup")))); - DatabaseContext.Database.Insert("cmsPropertyTypeGroup", "id", false, new PropertyTypeGroupDto { Id = 77776, ContentTypeNodeId = 99999, Text = "Group1", SortOrder = 1 }); - DatabaseContext.Database.Insert("cmsPropertyTypeGroup", "id", false, new PropertyTypeGroupDto { Id = 77777, ContentTypeNodeId = 99999, Text = "Group2", SortOrder = 2 }); - DatabaseContext.Database.Insert("cmsPropertyTypeGroup", "id", false, new PropertyTypeGroupDto { Id = 77778, ContentTypeNodeId = 99999, Text = "Group3", SortOrder = 3 }); - DatabaseContext.Database.Insert("cmsPropertyTypeGroup", "id", false, new PropertyTypeGroupDto { Id = 77779, ContentTypeNodeId = 99999, Text = "Group4", SortOrder = 4 }); + DatabaseContext.Database.Insert("cmsPropertyTypeGroup", "id", false, new PropertyTypeGroupDto { Id = 77776, UniqueId = 77776.ToGuid(), ContentTypeNodeId = 99999, Text = "Group1", SortOrder = 1 }); + DatabaseContext.Database.Insert("cmsPropertyTypeGroup", "id", false, new PropertyTypeGroupDto { Id = 77777, UniqueId = 77777.ToGuid(), ContentTypeNodeId = 99999, Text = "Group2", SortOrder = 2 }); + DatabaseContext.Database.Insert("cmsPropertyTypeGroup", "id", false, new PropertyTypeGroupDto { Id = 77778, UniqueId = 77778.ToGuid(), ContentTypeNodeId = 99999, Text = "Group3", SortOrder = 3 }); + DatabaseContext.Database.Insert("cmsPropertyTypeGroup", "id", false, new PropertyTypeGroupDto { Id = 77779, UniqueId = 77779.ToGuid(), ContentTypeNodeId = 99999, Text = "Group4", SortOrder = 4 }); DatabaseContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} OFF ", SqlSyntaxContext.SqlSyntaxProvider.GetQuotedTableName("cmsPropertyTypeGroup")))); DatabaseContext.Database.Execute(new Sql(string.Format("SET IDENTITY_INSERT {0} ON ", SqlSyntaxContext.SqlSyntaxProvider.GetQuotedTableName("cmsPropertyType")))); diff --git a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs index bf1086513a..74882fd0e4 100644 --- a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs @@ -89,7 +89,7 @@ namespace Umbraco.Web.PropertyEditors /// A value indicating whether a property is an upload field, and (optionaly) has a non-empty value. private static bool IsUploadField(Property property, bool ensureValue) { - if (property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.UploadFieldAlias) + if (property.PropertyType.PropertyEditorAlias != Constants.PropertyEditors.UploadFieldAlias) return false; if (ensureValue == false) return true; diff --git a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs index a913d2cb44..bfbe578875 100644 --- a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs @@ -108,7 +108,7 @@ namespace Umbraco.Web.PropertyEditors /// A value indicating whether a property is an image cropper field, and (optionaly) has a non-empty value. private static bool IsCropperField(Property property, bool ensureValue) { - if (property.PropertyType.PropertyEditorAlias == Constants.PropertyEditors.ImageCropperAlias) + if (property.PropertyType.PropertyEditorAlias != Constants.PropertyEditors.ImageCropperAlias) return false; if (ensureValue == false) return true;