Add media url provider support

This commit is contained in:
Rasmus John Pedersen
2019-04-15 15:57:35 +02:00
parent 9f9d24e6b3
commit d0701ae34e
28 changed files with 322 additions and 58 deletions

View File

@@ -81,54 +81,23 @@ namespace Umbraco.Web.Models
/// <inheritdoc />
/// <remarks>
/// The url of documents are computed by the document url providers. The url of medias are, at the moment,
/// computed here from the 'umbracoFile' property -- but we should move to media url providers at some point.
/// The url of documents are computed by the document url providers. The url of medias are computed by the media url providers
/// </remarks>
public virtual string GetUrl(string culture = null) // TODO: consider .GetCulture("fr-FR").Url
{
var umbracoContext = UmbracoContextAccessor.UmbracoContext;
if (umbracoContext == null)
throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext is null.");
if (umbracoContext.UrlProvider == null)
throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext.UrlProvider is null.");
switch (ItemType)
{
case PublishedItemType.Content:
var umbracoContext = UmbracoContextAccessor.UmbracoContext;
if (umbracoContext == null)
throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext is null.");
if (umbracoContext.UrlProvider == null)
throw new InvalidOperationException("Cannot compute Url for a content item when UmbracoContext.UrlProvider is null.");
return umbracoContext.UrlProvider.GetUrl(this, culture);
case PublishedItemType.Media:
var prop = GetProperty(Constants.Conventions.Media.File);
if (prop?.GetValue() == null)
{
return string.Empty;
}
var propType = ContentType.GetPropertyType(Constants.Conventions.Media.File);
// TODO: consider implementing media url providers
// note: that one does not support variations
//This is a hack - since we now have 2 properties that support a URL: upload and cropper, we need to detect this since we always
// want to return the normal URL and the cropper stores data as json
switch (propType.EditorAlias)
{
case Constants.PropertyEditors.Aliases.UploadField:
return prop.GetValue().ToString();
case Constants.PropertyEditors.Aliases.ImageCropper:
//get the url from the json format
var stronglyTyped = prop.GetValue() as ImageCropperValue;
if (stronglyTyped != null)
{
return stronglyTyped.Src;
}
return prop.GetValue()?.ToString();
}
return string.Empty;
return umbracoContext.UrlProvider.GetMediaUrl(this, Constants.Conventions.Media.File, culture);
default:
throw new NotSupportedException();
}