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)

This commit is contained in:
Shannon
2016-02-11 15:54:00 +01:00
parent dccee93700
commit 17ff952b52
4 changed files with 96 additions and 29 deletions

View File

@@ -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
{
/// <summary>
/// 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
/// </summary>
/// <returns></returns>
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<JObject>(jsonString);
if (json["src"] != null)
{
return json["src"].Value<string>();
}
}
catch (Exception ex)
{
logger.Error<ImageCropperValueConverter>("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;
}
/// <summary>
/// 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
/// </summary>
/// <returns></returns>
public static string[] GetUrls(this IMedia media, IContentSection contentSection, ILogger logger)
{
var links = new List<string>();
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();
}
}
}

View File

@@ -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;

View File

@@ -513,6 +513,7 @@
<Compile Include="Services\IServerRegistrationService.cs" />
<Compile Include="Services\ITaskService.cs" />
<Compile Include="Services\LocalizedTextServiceSupplementaryFileSource.cs" />
<Compile Include="Models\MediaExtensions.cs" />
<Compile Include="Services\MigrationEntryService.cs" />
<Compile Include="Services\MoveOperationStatusType.cs" />
<Compile Include="Services\PublicAccessService.cs" />