diff --git a/src/IIIFPresentation/API/Converters/CollectionConverter.cs b/src/IIIFPresentation/API/Converters/CollectionConverter.cs index 27f7443..89dfe26 100644 --- a/src/IIIFPresentation/API/Converters/CollectionConverter.cs +++ b/src/IIIFPresentation/API/Converters/CollectionConverter.cs @@ -106,11 +106,9 @@ public static PresentationCollection ToFlatCollection(this Models.Database.Colle Slug = hierarchy.Slug, Parent = GeneratePresentationCollectionParent(urlRoots, hierarchy), ItemsOrder = hierarchy.ItemsOrder, - Items = items.Select(i => GenerateCollectionItem(i, urlRoots, true)).ToList(), + Items = GenerateItems(urlRoots, items), PartOf = GeneratePartOf(hierarchy, dbAsset, urlRoots), - TotalItems = totalItems, - View = GenerateView(dbAsset, urlRoots, pageSize, currentPage, totalPages, orderQueryParamConverted), SeeAlso = GenerateSeeAlso(dbAsset, urlRoots), Created = dbAsset.Created.Floor(DateTimeX.Precision.Second), @@ -119,7 +117,7 @@ public static PresentationCollection ToFlatCollection(this Models.Database.Colle ModifiedBy = dbAsset.ModifiedBy }; } - + private static ICollectionItem GenerateCollectionItem(Hierarchy hierarchy, UrlRoots urlRoots, bool flatId) { var id = flatId ? hierarchy.GenerateFlatId(urlRoots) : hierarchy.GenerateHierarchicalId(urlRoots); @@ -247,6 +245,17 @@ public static List GenerateItems(UrlRoots urlRoots, List + /// Generates items in a hierarchy into the correct format + /// + /// The URL to use + /// The items to convert + /// A list of ICollectionItems + public static List GenerateItems(UrlRoots urlRoots, IEnumerable items) + { + return items.Select(i => GenerateCollectionItem(i, urlRoots, true)).ToList(); + } /// /// Generates the view component of a presentation collection diff --git a/src/IIIFPresentation/API/Features/Storage/Helpers/PresentationCollectionX.cs b/src/IIIFPresentation/API/Features/Storage/Helpers/PresentationCollectionX.cs index f324122..c9d0ab6 100644 --- a/src/IIIFPresentation/API/Features/Storage/Helpers/PresentationCollectionX.cs +++ b/src/IIIFPresentation/API/Features/Storage/Helpers/PresentationCollectionX.cs @@ -2,6 +2,7 @@ using API.Helpers; using Core.Helpers; using Models.API.Collection; +using Models.Database.General; using Collection = Models.Database.Collections.Collection; namespace API.Features.Storage.Helpers; @@ -22,7 +23,7 @@ public static class PresentationCollectionX /// An enriched presentation collection public static PresentationCollection EnrichPresentationCollection(this PresentationCollection presentationCollection, Collection collection, UrlRoots urlRoots, int pageSize, int currentPage, - int totalItems, List? items, string? orderQueryParam = null) + int totalItems, List? items, string? orderQueryParam = null) { var totalPages = CollectionConverter.GenerateTotalPages(pageSize, totalItems); diff --git a/src/IIIFPresentation/API/Features/Storage/Helpers/PresentationContextX.cs b/src/IIIFPresentation/API/Features/Storage/Helpers/PresentationContextX.cs index 16d8efd..c84e216 100644 --- a/src/IIIFPresentation/API/Features/Storage/Helpers/PresentationContextX.cs +++ b/src/IIIFPresentation/API/Features/Storage/Helpers/PresentationContextX.cs @@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore; using Models.API.General; using Models.Database.Collections; +using Models.Database.General; using Repository; using Repository.Helpers; diff --git a/src/IIIFPresentation/API/Features/Storage/Requests/CreateCollection.cs b/src/IIIFPresentation/API/Features/Storage/Requests/CreateCollection.cs index 0562804..f923650 100644 --- a/src/IIIFPresentation/API/Features/Storage/Requests/CreateCollection.cs +++ b/src/IIIFPresentation/API/Features/Storage/Requests/CreateCollection.cs @@ -9,6 +9,8 @@ using AWS.S3.Models; using Core; using Core.Helpers; +using IIIF.Presentation.V3.Content; +using IIIF.Serialisation; using MediatR; using Microsoft.Extensions.Options; using Models.API.Collection; @@ -117,7 +119,7 @@ await dbContext.TrySaveCollection(request.CustomerId, lo return saveErrors; } - await UploadToS3IfRequiredAsync(request, collection, convertedIIIFCollection!, isStorageCollection, + await UploadToS3IfRequiredAsync(collection, convertedIIIF.ConvertedCollection, isStorageCollection, cancellationToken); if (hierarchy.Parent != null) @@ -131,19 +133,7 @@ await UploadToS3IfRequiredAsync(request, collection, convertedIIIFCollection!, i WriteResult.Created); } - private static string ConvertToIIIFCollection(CreateCollection request, Collection collection) - { - var collectionAsIIIF = request.RawRequestBody.FromJson(); - var convertedIIIFCollection = collectionAsIIIF.AsJson(); - var thumbnails = collectionAsIIIF.Thumbnail?.OfType().ToList(); - if (thumbnails != null) - { - collection.Thumbnail = thumbnails.GetThumbnailPath(); - } - return convertedIIIFCollection; - } - - private async Task UploadToS3IfRequiredAsync(CreateCollection request, + private async Task UploadToS3IfRequiredAsync( Collection collection, string convertedIIIFCollection, bool isStorageCollection, CancellationToken cancellationToken = default) { if (!isStorageCollection) diff --git a/src/IIIFPresentation/API/Features/Storage/Requests/UpsertCollection.cs b/src/IIIFPresentation/API/Features/Storage/Requests/UpsertCollection.cs index c9d14fa..36dafe8 100644 --- a/src/IIIFPresentation/API/Features/Storage/Requests/UpsertCollection.cs +++ b/src/IIIFPresentation/API/Features/Storage/Requests/UpsertCollection.cs @@ -209,7 +209,7 @@ await CollectionRetrieval.RetrieveFullPathForCollection(databaseCollection, dbCo { await bucketWriter.WriteToBucket( new ObjectInBucket(settings.AWS.S3.StorageBucket, - databaseCollection.GetCollectionBucketKey()), + databaseCollection.GetResourceBucketKey()), convertedIIIF.ConvertedCollection, "application/json", cancellationToken); } diff --git a/src/IIIFPresentation/API/Features/Storage/StorageController.cs b/src/IIIFPresentation/API/Features/Storage/StorageController.cs index f75c3f2..6893302 100644 --- a/src/IIIFPresentation/API/Features/Storage/StorageController.cs +++ b/src/IIIFPresentation/API/Features/Storage/StorageController.cs @@ -102,17 +102,17 @@ public async Task Post(int customerId, [FromServices] Presentatio var rawRequestBody = await Request.GetRawRequestBodyAsync(); - var (collection, error) = await TryDeserializePresentationCollection(rawRequestBody); - if (error) return PresentationUnableToSerialize(); + var deserializedCollection = await TryDeserializePresentationCollection(rawRequestBody); + if (deserializedCollection.Error) return PresentationUnableToSerialize(); - var validation = await validator.ValidateAsync(collection); + var validation = await validator.ValidateAsync(deserializedCollection.ConvertedCollection); if (!validation.IsValid) { return this.ValidationFailed(validation); } - return await HandleUpsert(new CreateCollection(customerId, collection, rawRequestBody, GetUrlRoots())); + return await HandleUpsert(new CreateCollection(customerId, deserializedCollection.ConvertedCollection, rawRequestBody, GetUrlRoots())); } [Authorize] @@ -125,34 +125,38 @@ public async Task Put(int customerId, string id, var rawRequestBody = await Request.GetRawRequestBodyAsync(); - var (collection, error) = await TryDeserializePresentationCollection(rawRequestBody); - if (error) return PresentationUnableToSerialize(); + var deserializedCollection = await TryDeserializePresentationCollection(rawRequestBody); + if (deserializedCollection.Error) return PresentationUnableToSerialize(); - var validation = await validator.ValidateAsync(collection); + var validation = await validator.ValidateAsync(deserializedCollection.ConvertedCollection); if (!validation.IsValid) { return this.ValidationFailed(validation); } - return await HandleUpsert(new UpsertCollection(customerId, id, collection, GetUrlRoots(), + return await HandleUpsert(new UpsertCollection(customerId, id, deserializedCollection.ConvertedCollection, GetUrlRoots(), Request.Headers.IfMatch, rawRequestBody)); } - private async Task<(PresentationCollection collection, bool error)> TryDeserializePresentationCollection(string rawRequestBody) + private async Task TryDeserializePresentationCollection(string rawRequestBody) { PresentationCollection? collection; try { collection = await rawRequestBody.ToPresentation(); } - catch (Exception ex) + catch (Exception) { - return (new PresentationCollection(), true); + return new DeserializedCollection(true, new PresentationCollection()); } - return collection == null ? (new PresentationCollection(), true) : (collection, false); + return collection == null + ? new DeserializedCollection(true, new PresentationCollection()) + : new DeserializedCollection(false, collection); } + + private record DeserializedCollection(bool Error, PresentationCollection ConvertedCollection); [Authorize] [HttpDelete("collections/{id}")]