Skip to content

Commit

Permalink
fixing code following merge
Browse files Browse the repository at this point in the history
  • Loading branch information
JackLewis-digirati committed Oct 23, 2024
1 parent 25262b2 commit 53db9d4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
17 changes: 13 additions & 4 deletions src/IIIFPresentation/API/Converters/CollectionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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);
Expand Down Expand Up @@ -247,6 +245,17 @@ public static List<ICollectionItem> GenerateItems(UrlRoots urlRoots, List<Models
}).ToList()
: [];
}

/// <summary>
/// Generates items in a hierarchy into the correct format
/// </summary>
/// <param name="urlRoots">The URL to use</param>
/// <param name="items">The items to convert</param>
/// <returns>A list of ICollectionItems</returns>
public static List<ICollectionItem> GenerateItems(UrlRoots urlRoots, IEnumerable<Hierarchy> items)
{
return items.Select(i => GenerateCollectionItem(i, urlRoots, true)).ToList();
}

/// <summary>
/// Generates the view component of a presentation collection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,7 +23,7 @@ public static class PresentationCollectionX
/// <returns>An enriched presentation collection</returns>
public static PresentationCollection EnrichPresentationCollection(this PresentationCollection presentationCollection,
Collection collection, UrlRoots urlRoots, int pageSize, int currentPage,
int totalItems, List<Collection>? items, string? orderQueryParam = null)
int totalItems, List<Hierarchy>? items, string? orderQueryParam = null)
{
var totalPages = CollectionConverter.GenerateTotalPages(pageSize, totalItems);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.EntityFrameworkCore;
using Models.API.General;
using Models.Database.Collections;
using Models.Database.General;
using Repository;
using Repository.Helpers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -117,7 +119,7 @@ await dbContext.TrySaveCollection<PresentationCollection>(request.CustomerId, lo
return saveErrors;
}

await UploadToS3IfRequiredAsync(request, collection, convertedIIIFCollection!, isStorageCollection,
await UploadToS3IfRequiredAsync(collection, convertedIIIF.ConvertedCollection, isStorageCollection,
cancellationToken);

if (hierarchy.Parent != null)
Expand All @@ -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<IIIF.Presentation.V3.Collection>();
var convertedIIIFCollection = collectionAsIIIF.AsJson();
var thumbnails = collectionAsIIIF.Thumbnail?.OfType<Image>().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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
28 changes: 16 additions & 12 deletions src/IIIFPresentation/API/Features/Storage/StorageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ public async Task<IActionResult> 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]
Expand All @@ -125,34 +125,38 @@ public async Task<IActionResult> 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<DeserializedCollection> TryDeserializePresentationCollection(string rawRequestBody)
{
PresentationCollection? collection;
try
{
collection = await rawRequestBody.ToPresentation<PresentationCollection>();
}
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}")]
Expand Down

0 comments on commit 53db9d4

Please sign in to comment.