From 17ff952b52f50d38d7d482a1e13bd9d9cfc29c6f Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 11 Feb 2016 15:54:00 +0100 Subject: [PATCH] Fixes the URL creating for media in the back office to use an IMedia instead of published media (since it's not published yet at the time) --- src/Umbraco.Core/Models/MediaExtensions.cs | 81 +++++++++++++++++++ src/Umbraco.Core/Services/IMediaService.cs | 1 + src/Umbraco.Core/Umbraco.Core.csproj | 1 + .../Models/Mapping/MediaModelMapper.cs | 42 +++------- 4 files changed, 96 insertions(+), 29 deletions(-) create mode 100644 src/Umbraco.Core/Models/MediaExtensions.cs diff --git a/src/Umbraco.Core/Models/MediaExtensions.cs b/src/Umbraco.Core/Models/MediaExtensions.cs new file mode 100644 index 0000000000..1f2e1b62b2 --- /dev/null +++ b/src/Umbraco.Core/Models/MediaExtensions.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Umbraco.Core.Configuration.UmbracoSettings; +using Umbraco.Core.Logging; +using Umbraco.Core.PropertyEditors.ValueConverters; + +namespace Umbraco.Core.Models +{ + internal static class MediaExtensions + { + /// + /// Hack: we need to put this in a real place, this is currently just used to render the urls for a media item in the back office + /// + /// + public static string GetUrl(this IMedia media, string propertyAlias, ILogger logger) + { + var propertyType = media.PropertyTypes.FirstOrDefault(x => x.Alias.InvariantEquals(propertyAlias)); + if (propertyType != null) + { + var val = media.Properties[propertyType]; + if (val != null) + { + var jsonString = val.Value as string; + if (jsonString != null) + { + if (propertyType.PropertyEditorAlias == Constants.PropertyEditors.ImageCropperAlias) + { + if (jsonString.DetectIsJson()) + { + try + { + var json = JsonConvert.DeserializeObject(jsonString); + if (json["src"] != null) + { + return json["src"].Value(); + } + } + catch (Exception ex) + { + logger.Error("Could not parse the string " + jsonString + " to a json object", ex); + return string.Empty; + } + } + else + { + return jsonString; + } + } + else if (propertyType.PropertyEditorAlias == Constants.PropertyEditors.UploadFieldAlias) + { + return jsonString; + } + //hrm, without knowing what it is, just adding a string here might not be very nice + } + } + } + return string.Empty; + } + + /// + /// Hack: we need to put this in a real place, this is currently just used to render the urls for a media item in the back office + /// + /// + public static string[] GetUrls(this IMedia media, IContentSection contentSection, ILogger logger) + { + var links = new List(); + var autoFillProperties = contentSection.ImageAutoFillProperties.ToArray(); + if (autoFillProperties.Any()) + { + links.AddRange( + autoFillProperties + .Select(field => media.GetUrl(field.Alias, logger)) + .Where(link => link.IsNullOrWhiteSpace() == false)); + } + return links.ToArray(); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Services/IMediaService.cs b/src/Umbraco.Core/Services/IMediaService.cs index d32acdd6b1..d104b95ddc 100644 --- a/src/Umbraco.Core/Services/IMediaService.cs +++ b/src/Umbraco.Core/Services/IMediaService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using Umbraco.Core.Configuration; using Umbraco.Core.Models; using Umbraco.Core.Persistence.DatabaseModelDefinitions; diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index ef45bb08ab..92afe4105c 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -513,6 +513,7 @@ + diff --git a/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs b/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs index 94961d9793..799a93a220 100644 --- a/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs +++ b/src/Umbraco.Web/Models/Mapping/MediaModelMapper.cs @@ -10,6 +10,7 @@ using AutoMapper; using umbraco; using Umbraco.Core; using Umbraco.Core.Configuration; +using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.Mapping; using Umbraco.Core.PropertyEditors; @@ -53,7 +54,7 @@ namespace Umbraco.Web.Models.Mapping .ForMember(display => display.Alias, expression => expression.Ignore()) .ForMember(display => display.IsContainer, expression => expression.Ignore()) .ForMember(display => display.Tabs, expression => expression.ResolveUsing(new TabsAndPropertiesResolver(applicationContext.Services.TextService))) - .AfterMap((media, display) => AfterMap(media, display, applicationContext.Services.DataTypeService, applicationContext.Services.TextService)); + .AfterMap((media, display) => AfterMap(media, display, applicationContext.Services.DataTypeService, applicationContext.Services.TextService, applicationContext.ProfilingLogger.Logger)); //FROM IMedia TO ContentItemBasic config.CreateMap>() @@ -84,7 +85,7 @@ namespace Umbraco.Web.Models.Mapping .ForMember(x => x.Alias, expression => expression.Ignore()); } - private static void AfterMap(IMedia media, MediaItemDisplay display, IDataTypeService dataTypeService, ILocalizedTextService localizedText) + private static void AfterMap(IMedia media, MediaItemDisplay display, IDataTypeService dataTypeService, ILocalizedTextService localizedText, ILogger logger) { // Adapted from ContentModelMapper //map the IsChildOfListView (this is actually if it is a descendant of a list view!) @@ -137,35 +138,18 @@ namespace Umbraco.Web.Models.Mapping } }; - var helper = new UmbracoHelper(UmbracoContext.Current); - var mediaItem = helper.TypedMedia(media.Id); - if (mediaItem != null) + var links = media.GetUrls(UmbracoConfig.For.UmbracoSettings().Content, logger); + + if (links.Any()) { - var crops = new List(); - var autoFillProperties = UmbracoConfig.For.UmbracoSettings().Content.ImageAutoFillProperties.ToArray(); - if (autoFillProperties.Any()) + var link = new ContentPropertyDisplay { - foreach (var field in autoFillProperties) - { - var crop = mediaItem.GetCropUrl(field.Alias, string.Empty); - if (string.IsNullOrWhiteSpace(crop) == false) - crops.Add(crop.Split('?')[0]); - } - - if (crops.Any()) - { - var link = new ContentPropertyDisplay - { - Alias = string.Format("{0}urls", Constants.PropertyEditors.InternalGenericPropertiesPrefix), - Label = localizedText.Localize("media/urls"), - // don't add the querystring, split on the "?" will also work if there is no "?" - Value = string.Join(",", crops), - View = "urllist" - }; - - genericProperties.Add(link); - } - } + Alias = string.Format("{0}urls", Constants.PropertyEditors.InternalGenericPropertiesPrefix), + Label = localizedText.Localize("media/urls"), + Value = string.Join(",", links), + View = "urllist" + }; + genericProperties.Add(link); } TabsAndPropertiesResolver.MapGenericProperties(media, display, localizedText, genericProperties);