Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content & media url endpoints #15832

Merged
merged 6 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Api.Management.ViewModels.Document;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;

namespace Umbraco.Cms.Api.Management.Controllers.Document;

[ApiVersion("1.0")]
public class DocumentUrlController : DocumentControllerBase
{
private readonly IContentService _contentService;
private readonly IDocumentUrlFactory _documentUrlFactory;

public DocumentUrlController(
IContentService contentService,
IDocumentUrlFactory documentUrlFactory)
{
_contentService = contentService;
_documentUrlFactory = documentUrlFactory;
}

[MapToApiVersion("1.0")]
[HttpGet("urls")]
[ProducesResponseType(typeof(IEnumerable<DocumentUrlInfoResponseModel>), StatusCodes.Status200OK)]
public async Task<ActionResult> GetUrls([FromQuery(Name = "id")] HashSet<Guid> ids)
{
IEnumerable<IContent> items = _contentService.GetByIds(ids);

return Ok(await _documentUrlFactory.CreateUrlSetsAsync(items));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task<IActionResult> ByKey(CancellationToken cancellationToken, Guid
return MediaNotFound();
}

MediaResponseModel model = await _mediaPresentationFactory.CreateResponseModelAsync(media);
MediaResponseModel model = _mediaPresentationFactory.CreateResponseModel(media);
return Ok(model);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Api.Management.ViewModels.Media;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Controllers.Media;

[ApiVersion("1.0")]
public class MediaUrlController : MediaControllerBase
{
private readonly IMediaService _mediaService;
private readonly IMediaUrlFactory _mediaUrlFactory;

public MediaUrlController(
IMediaService mediaService,
IMediaUrlFactory mediaUrlFactory)
{
_mediaService = mediaService;
_mediaUrlFactory = mediaUrlFactory;
}

[MapToApiVersion("1.0")]
[HttpGet("urls")]
[ProducesResponseType(typeof(IEnumerable<MediaUrlInfoResponseModel>), StatusCodes.Status200OK)]
public ActionResult GetUrls([FromQuery(Name = "id")] HashSet<Guid> ids)
{
IEnumerable<IMedia> items = _mediaService.GetByIds(ids);

return Ok(_mediaUrlFactory.CreateUrlSets(items));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal static IUmbracoBuilder AddMedia(this IUmbracoBuilder builder)
builder.Services.AddTransient<IMediaPresentationFactory, MediaPresentationFactory>();
builder.Services.AddTransient<IMediaEditingPresentationFactory, MediaEditingPresentationFactory>();
builder.Services.AddTransient<IUrlAssembler, DefaultUrlAssembler>();
builder.Services.AddTransient<IMediaUrlFactory, MediaUrlFactory>();
builder.Services.AddScoped<IAbsoluteUrlBuilder, DefaultAbsoluteUrlBuilder>();

builder.WithCollectionBuilder<MapDefinitionCollectionBuilder>().Add<MediaMapDefinition>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<DocumentResponseModel> CreateResponseModelAsync(IContent conte
{
DocumentResponseModel responseModel = _umbracoMapper.Map<DocumentResponseModel>(content)!;

responseModel.Urls = await _documentUrlFactory.GetUrlsAsync(content);
responseModel.Urls = await _documentUrlFactory.CreateUrlsAsync(content);

Guid? templateKey = content.TemplateId.HasValue
? _templateService.GetAsync(content.TemplateId.Value).Result?.Key
Expand Down
19 changes: 16 additions & 3 deletions src/Umbraco.Cms.Api.Management/Factories/DocumentUrlFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public DocumentUrlFactory(
_publishedUrlProvider = publishedUrlProvider;
}

public async Task<IEnumerable<DocumentUrlInfo>> GetUrlsAsync(IContent content)
public async Task<IEnumerable<DocumentUrlInfo>> CreateUrlsAsync(IContent content)
{
IUmbracoContext umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();

UrlInfo[] urlInfos = (await content.GetContentUrlsAsync(
IEnumerable<UrlInfo> urlInfos = await content.GetContentUrlsAsync(
_publishedRouter,
umbracoContext,
_languageService,
Expand All @@ -57,11 +57,24 @@ public async Task<IEnumerable<DocumentUrlInfo>> GetUrlsAsync(IContent content)
_variationContextAccessor,
_loggerFactory.CreateLogger<IContent>(),
_uriUtility,
_publishedUrlProvider)).ToArray();
_publishedUrlProvider);

return urlInfos
.Where(urlInfo => urlInfo.IsUrl)
.Select(urlInfo => new DocumentUrlInfo { Culture = urlInfo.Culture, Url = urlInfo.Text })
.ToArray();
}

public async Task<IEnumerable<DocumentUrlInfoResponseModel>> CreateUrlSetsAsync(IEnumerable<IContent> contentItems)
{
var documentUrlInfoResourceSets = new List<DocumentUrlInfoResponseModel>();

foreach (IContent content in contentItems)
{
IEnumerable<DocumentUrlInfo> urls = await CreateUrlsAsync(content);
documentUrlInfoResourceSets.Add(new DocumentUrlInfoResponseModel(content.Key, urls));
}

return documentUrlInfoResourceSets;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace Umbraco.Cms.Api.Management.Factories;

public interface IDocumentUrlFactory
{
Task<IEnumerable<DocumentUrlInfo>> GetUrlsAsync(IContent content);
Task<IEnumerable<DocumentUrlInfo>> CreateUrlsAsync(IContent content);
Task<IEnumerable<DocumentUrlInfoResponseModel>> CreateUrlSetsAsync(IEnumerable<IContent> contentItems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Umbraco.Cms.Api.Management.Factories;

public interface IMediaPresentationFactory
{
Task<MediaResponseModel> CreateResponseModelAsync(IMedia media);
MediaResponseModel CreateResponseModel(IMedia media);

MediaItemResponseModel CreateItemResponseModel(IMediaEntitySlim entity);

Expand Down
10 changes: 10 additions & 0 deletions src/Umbraco.Cms.Api.Management/Factories/IMediaUrlFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Umbraco.Cms.Api.Management.ViewModels.Media;
using Umbraco.Cms.Core.Models;

namespace Umbraco.Cms.Api.Management.Factories;

public interface IMediaUrlFactory
{
IEnumerable<MediaUrlInfo> CreateUrls(IMedia media);
IEnumerable<MediaUrlInfoResponseModel> CreateUrlSets(IEnumerable<IMedia> mediaItems);
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,33 @@
using Microsoft.Extensions.Options;
using Umbraco.Cms.Api.Management.Routing;
using Umbraco.Cms.Api.Management.ViewModels.Content;
using Umbraco.Cms.Api.Management.ViewModels.Content;
using Umbraco.Cms.Api.Management.ViewModels.Media;
using Umbraco.Cms.Api.Management.ViewModels.Media.Item;
using Umbraco.Cms.Api.Management.ViewModels.MediaType;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;

namespace Umbraco.Cms.Api.Management.Factories;

internal sealed class MediaPresentationFactory : IMediaPresentationFactory
{
private readonly IUmbracoMapper _umbracoMapper;
private readonly ContentSettings _contentSettings;
private readonly MediaUrlGeneratorCollection _mediaUrlGenerators;
private readonly IAbsoluteUrlBuilder _absoluteUrlBuilder;
private readonly IMediaUrlFactory _mediaUrlFactory;

public MediaPresentationFactory(
IUmbracoMapper umbracoMapper,
IOptions<ContentSettings> contentSettings,
MediaUrlGeneratorCollection mediaUrlGenerators,
IAbsoluteUrlBuilder absoluteUrlBuilder)
IMediaUrlFactory mediaUrlFactory)
{
_umbracoMapper = umbracoMapper;
_contentSettings = contentSettings.Value;
_mediaUrlGenerators = mediaUrlGenerators;
_absoluteUrlBuilder = absoluteUrlBuilder;
_mediaUrlFactory = mediaUrlFactory;
}

public Task<MediaResponseModel> CreateResponseModelAsync(IMedia media)
public MediaResponseModel CreateResponseModel(IMedia media)
{
MediaResponseModel responseModel = _umbracoMapper.Map<MediaResponseModel>(media)!;

responseModel.Urls = media
.GetUrls(_contentSettings, _mediaUrlGenerators)
.WhereNotNull()
.Select(mediaUrl => new MediaUrlInfo
{
Culture = null,
Url = _absoluteUrlBuilder.ToAbsoluteUrl(mediaUrl).ToString(),
})
.ToArray();
responseModel.Urls = _mediaUrlFactory.CreateUrls(media);

return Task.FromResult(responseModel);
return responseModel;
}

public MediaItemResponseModel CreateItemResponseModel(IMediaEntitySlim entity)
Expand Down
48 changes: 48 additions & 0 deletions src/Umbraco.Cms.Api.Management/Factories/MediaUrlFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Api.Management.Routing;
using Umbraco.Cms.Api.Management.ViewModels.Content;
using Umbraco.Cms.Api.Management.ViewModels.Document;
using Umbraco.Cms.Api.Management.ViewModels.Media;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Extensions;

namespace Umbraco.Cms.Api.Management.Factories;

public class MediaUrlFactory : IMediaUrlFactory
{
private readonly ContentSettings _contentSettings;
private readonly MediaUrlGeneratorCollection _mediaUrlGenerators;
private readonly IAbsoluteUrlBuilder _absoluteUrlBuilder;


public MediaUrlFactory(
IOptions<ContentSettings> contentSettings,
MediaUrlGeneratorCollection mediaUrlGenerators,
IAbsoluteUrlBuilder absoluteUrlBuilder)
{
_contentSettings = contentSettings.Value;
_mediaUrlGenerators = mediaUrlGenerators;
_absoluteUrlBuilder = absoluteUrlBuilder;
}

public IEnumerable<MediaUrlInfo> CreateUrls(IMedia media) =>
media
.GetUrls(_contentSettings, _mediaUrlGenerators)
.WhereNotNull()
.Select(mediaUrl => new MediaUrlInfo
{
Culture = null,
Url = _absoluteUrlBuilder.ToAbsoluteUrl(mediaUrl).ToString(),
})
.ToArray();

public IEnumerable<MediaUrlInfoResponseModel> CreateUrlSets(IEnumerable<IMedia> mediaItems) =>
mediaItems.Select(media => new MediaUrlInfoResponseModel(media.Key, CreateUrls(media))).ToArray();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Umbraco.Cms.Api.Management.ViewModels.Document;

public sealed class DocumentUrlInfoResponseModel
{
public DocumentUrlInfoResponseModel(Guid id, IEnumerable<DocumentUrlInfo> urlInfos)
{
Id = id;
UrlInfos = urlInfos;
}

public Guid Id { get; }

public IEnumerable<DocumentUrlInfo> UrlInfos { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Umbraco.Cms.Api.Management.ViewModels.Media;

public sealed class MediaUrlInfoResponseModel
{
public MediaUrlInfoResponseModel(Guid id, IEnumerable<MediaUrlInfo> urlInfos)
{
Id = id;
UrlInfos = urlInfos;
}

public Guid Id { get; }

public IEnumerable<MediaUrlInfo> UrlInfos { get; }
}
Loading