From 16185aab1a7f4300cc9ba40e5fc49b151a5d9ed1 Mon Sep 17 00:00:00 2001 From: p-kaczynski Date: Thu, 24 Oct 2024 17:45:29 +0100 Subject: [PATCH] Change the way the S3 top level collection id is generated to match automated tests --- .../Streaming/S3StoredJsonProcessorTests.cs | 27 ++++++++++++++----- .../Streaming/S3StoredJsonProcessor.cs | 11 +++++--- .../Requests/GetHierarchicalCollection.cs | 11 +++++--- .../API/Features/Storage/StorageController.cs | 2 +- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/IIIFPresentation/API.Tests/Converters/Streaming/S3StoredJsonProcessorTests.cs b/src/IIIFPresentation/API.Tests/Converters/Streaming/S3StoredJsonProcessorTests.cs index ed92afb9..4420f7dd 100644 --- a/src/IIIFPresentation/API.Tests/Converters/Streaming/S3StoredJsonProcessorTests.cs +++ b/src/IIIFPresentation/API.Tests/Converters/Streaming/S3StoredJsonProcessorTests.cs @@ -1,4 +1,5 @@ using System.Text; +using API.Converters; using API.Converters.Streaming; using LateApexEarlySpeed.Xunit.Assertion.Json; @@ -9,24 +10,38 @@ public class S3StoredJsonProcessorTests [Fact] public void ProcessJson_ChangesTopLevelId() { - const string requestSlug = "fnord/quark/4534"; + const string requestSlug = + "0004_hierarchical_iiif_collection_2O71nF/0004_hierarchical_iiif_collection_child_PWZnry"; + const int customerId = 52; + var urlRoots = new UrlRoots + { + BaseUrl = "http://localhost" + }; - var result = GetProcessed(ManifestJsonWithId, new(requestSlug)); + var result = GetProcessed(ManifestJsonWithId, new(requestSlug, customerId, urlRoots)); JsonAssertion.Meet(root => root.IsJsonObject() - .HasProperty("id", p => p.IsJsonString().Equal($"managed:{requestSlug}")), + .HasProperty("id", p => p.IsJsonString().Equal( + $"http://localhost/52/{requestSlug}")), result); } [Fact] public void ProcessJson_AddsTopLevelId() { - const string requestSlug = "fnord/quark/4534"; + const string requestSlug = + "0004_hierarchical_iiif_collection_2O71nF/0004_hierarchical_iiif_collection_child_PWZnry"; + const int customerId = 52; + var urlRoots = new UrlRoots + { + BaseUrl = "http://localhost" + }; - var result = GetProcessed(ManifestJsonWithoutId, new(requestSlug)); + var result = GetProcessed(ManifestJsonWithoutId, new(requestSlug, customerId, urlRoots)); JsonAssertion.Meet(root => root.IsJsonObject() - .HasProperty("id", p => p.IsJsonString().Equal($"managed:{requestSlug}")), + .HasProperty("id", p => p.IsJsonString().Equal( + $"http://localhost/52/{requestSlug}")), result); } diff --git a/src/IIIFPresentation/API/Converters/Streaming/S3StoredJsonProcessor.cs b/src/IIIFPresentation/API/Converters/Streaming/S3StoredJsonProcessor.cs index 33269f28..4e54ee1e 100644 --- a/src/IIIFPresentation/API/Converters/Streaming/S3StoredJsonProcessor.cs +++ b/src/IIIFPresentation/API/Converters/Streaming/S3StoredJsonProcessor.cs @@ -2,11 +2,16 @@ namespace API.Converters.Streaming; -public class S3StoredJsonProcessor(string requestSlug) +public class S3StoredJsonProcessor(string requestSlug, int customerId, UrlRoots roots) : StreamingProcessorImplBase { private const string IdPropertyName = "id"; + private string GeneratedId { get; } = new UriBuilder(roots.BaseUrl!) + { + Path = $"{customerId}/{requestSlug}" + }.Uri.ToString(); + #region Overrides of StreamingProcessorImplBase public override object GetInitialState() => new S3ProcessorCustomState(); @@ -31,7 +36,7 @@ protected override void OnEndObject(ref Utf8JsonReader reader, Utf8JsonWriter wr if (reader.CurrentDepth == 0 && !currentState.IdSet) { writer.WritePropertyName(IdPropertyName); - writer.WriteStringValue($"managed:{requestSlug}"); + writer.WriteStringValue(GeneratedId); } base.OnEndObject(ref reader, writer, ref currentState); @@ -56,7 +61,7 @@ protected override void OnEndObject(ref Utf8JsonReader reader, Utf8JsonWriter wr currentState.IdSet = true; // Found id in the top-level object - return $"managed:{requestSlug}"; + return GeneratedId; } return v; diff --git a/src/IIIFPresentation/API/Features/Storage/Requests/GetHierarchicalCollection.cs b/src/IIIFPresentation/API/Features/Storage/Requests/GetHierarchicalCollection.cs index 9881f297..ddb4732d 100644 --- a/src/IIIFPresentation/API/Features/Storage/Requests/GetHierarchicalCollection.cs +++ b/src/IIIFPresentation/API/Features/Storage/Requests/GetHierarchicalCollection.cs @@ -1,4 +1,5 @@ -using API.Converters.Streaming; +using API.Converters; +using API.Converters.Streaming; using API.Features.Storage.Helpers; using API.Features.Storage.Models; using API.Helpers; @@ -9,18 +10,19 @@ using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; -using Models.Database.Collections; using Models.Database.General; using Repository; using Repository.Helpers; namespace API.Features.Storage.Requests; -public class GetHierarchicalCollection(int customerId, string slug) : IRequest +public class GetHierarchicalCollection(int customerId, string slug, UrlRoots urlRoots) : IRequest { public int CustomerId { get; } = customerId; public string Slug { get; } = slug; + + public UrlRoots UrlRoots { get; } = urlRoots; } public class GetHierarchicalCollectionHandler(PresentationContext dbContext, IBucketReader bucketReader, @@ -49,7 +51,8 @@ public async Task Handle(GetHierarchicalCollection request, using var memoryStream = new MemoryStream(); using var reader = new StreamReader(memoryStream); StreamingJsonProcessor.ProcessJson(objectFromS3.Stream, memoryStream, - objectFromS3.Headers.ContentLength, new S3StoredJsonProcessor(request.Slug)); + objectFromS3.Headers.ContentLength, + new S3StoredJsonProcessor(request.Slug, request.CustomerId, request.UrlRoots)); memoryStream.Seek(0, SeekOrigin.Begin); collectionFromS3 = await reader.ReadToEndAsync(cancellationToken); } diff --git a/src/IIIFPresentation/API/Features/Storage/StorageController.cs b/src/IIIFPresentation/API/Features/Storage/StorageController.cs index 48b452f1..2e57253c 100644 --- a/src/IIIFPresentation/API/Features/Storage/StorageController.cs +++ b/src/IIIFPresentation/API/Features/Storage/StorageController.cs @@ -36,7 +36,7 @@ public class StorageController(IAuthenticator authenticator, IOptions GetHierarchicalCollection(int customerId, string slug = "") { - var storageRoot = await Mediator.Send(new GetHierarchicalCollection(customerId, slug)); + var storageRoot = await Mediator.Send(new GetHierarchicalCollection(customerId, slug, GetUrlRoots())); if (storageRoot.Collection is not { IsPublic: true }) return this.PresentationNotFound();