-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1de8b49
commit cbec168
Showing
8 changed files
with
154 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 15 additions & 9 deletions
24
LeedsExperiment/Preservation.API/Controllers/RepositoryController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
using Microsoft.AspNetCore.Http.Extensions; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Preservation.API.Models; | ||
|
||
namespace Preservation.API.Controllers; | ||
|
||
[Route("[controller]/{*path}")] | ||
[ApiController] | ||
public class RepositoryController(IPreservation preservation) : Controller | ||
public class RepositoryController(IPreservation preservation, ModelConverter modelConverter) : Controller | ||
{ | ||
/// <summary> | ||
/// Browse underlying repository for Container, DigitalObject or Binary. | ||
/// </summary> | ||
/// <returns></returns> | ||
[HttpGet] | ||
/// <param name="path">Path to item in repository to fetch (e.g. path/to/item</param> | ||
/// <param name="version"> | ||
/// Optional version parameter, in format "v1","v2" etc. Current version returned if omitted | ||
/// </param> | ||
/// <returns><see cref="Container"/>, <see cref="DigitalObject"/> or <see cref="Binary"/></returns> | ||
[HttpGet(Name = "Browse")] | ||
[Produces("application/json")] | ||
public async Task<IActionResult> Index([FromRoute] string path, [FromQuery] string? version = null) | ||
{ | ||
// How do we know if this is an archive group or not? | ||
var unEscapedPath = Uri.UnescapeDataString(path); | ||
var storageResource = string.IsNullOrEmpty(version) | ||
? await preservation.GetResource(path) | ||
: await preservation.GetArchivalGroup(path, version); | ||
? await preservation.GetResource(unEscapedPath) | ||
: await preservation.GetArchivalGroup(unEscapedPath, version); | ||
|
||
if (storageResource == null) return NotFound(); | ||
// Convert to appropriate type | ||
var preservationResource = storageResource.ToPreservationResource(new Uri(HttpContext.Request.GetDisplayUrl())); | ||
|
||
var preservationResource = | ||
modelConverter.ToPreservationResource(storageResource, new Uri(HttpContext.Request.GetDisplayUrl())); | ||
return Ok(preservationResource); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Fedora.Abstractions; | ||
using Fedora.Storage; | ||
|
||
namespace Preservation.API.Models; | ||
|
||
public class ModelConverter(UriGenerator uriGenerator) | ||
{ | ||
public PreservationResource ToPreservationResource(Fedora.Abstractions.Resource storageResource, Uri requestPath) | ||
{ | ||
switch (storageResource) | ||
{ | ||
case Fedora.Abstractions.ArchivalGroup ag: | ||
{ | ||
var digitalObject = new DigitalObject | ||
{ | ||
Id = requestPath, | ||
Name = ag.Name, | ||
Version = ToDigitalObjectVersion(ag.Version, requestPath), | ||
Versions = (ag.Versions ?? Array.Empty<ObjectVersion>()) | ||
.Select(v => ToDigitalObjectVersion(v, requestPath)!).ToArray(), | ||
Binaries = ag.Binaries.Select(b => ToPresentationBinary(b)).ToArray(), | ||
Containers = ag.Containers.Select(c => ToPresentationContainer(c)).ToArray(), | ||
}; | ||
MapBasicsFromStorageResource(digitalObject, storageResource); | ||
return digitalObject; | ||
} | ||
case Fedora.Abstractions.Container c: | ||
return ToPresentationContainer(c); | ||
case Fedora.Abstractions.Binary b: | ||
return ToPresentationBinary(b); | ||
} | ||
|
||
throw new InvalidOperationException($"Unable to handle {storageResource.GetType()} resource"); | ||
} | ||
|
||
private DigitalObjectVersion? ToDigitalObjectVersion(ObjectVersion? objectVersion, Uri repositoryUri) | ||
{ | ||
if (objectVersion == null) return null; | ||
|
||
var digitalObjectVersion = new DigitalObjectVersion | ||
{ | ||
Id = uriGenerator.GetRepositoryPath(repositoryUri, objectVersion.OcflVersion), | ||
Name = objectVersion.OcflVersion, | ||
Date = objectVersion.MementoDateTime, | ||
}; | ||
return digitalObjectVersion; | ||
} | ||
|
||
private Binary ToPresentationBinary(Fedora.Abstractions.Binary fedoraBinary) | ||
{ | ||
var binary = new Binary | ||
{ | ||
Id = uriGenerator.GetRepositoryPath(fedoraBinary.StorageApiUri), | ||
Content = new Uri("https://todo"), | ||
Name = fedoraBinary.FileName, | ||
Digest = fedoraBinary.Digest, | ||
Location = new Uri("s3://todo/path"), | ||
PartOf = uriGenerator.GetRepositoryPath(fedoraBinary.PartOf), | ||
}; | ||
|
||
MapBasicsFromStorageResource(binary, fedoraBinary); | ||
return binary; | ||
} | ||
|
||
private Container ToPresentationContainer(Fedora.Abstractions.Container fedoraContainer) | ||
{ | ||
var container = new Container | ||
{ | ||
Id = uriGenerator.GetRepositoryPath(fedoraContainer.StorageApiUri), | ||
Name = fedoraContainer.Name, | ||
Containers = fedoraContainer.Containers.Select(c => ToPresentationContainer(c)).ToArray(), | ||
Binaries = fedoraContainer.Binaries.Select(b => ToPresentationBinary(b)).ToArray(), | ||
PartOf = uriGenerator.GetRepositoryPath(fedoraContainer.PartOf), | ||
}; | ||
|
||
MapBasicsFromStorageResource(container, fedoraContainer); | ||
return container; | ||
} | ||
|
||
private void MapBasicsFromStorageResource<T>(T target, Resource resource) | ||
where T : PreservationResource, new() | ||
{ | ||
target.Created = resource.Created ?? DateTime.MinValue; | ||
target.CreatedBy = new Uri($"http://example.id/{resource.CreatedBy}"); | ||
target.LastModified = resource.LastModified; | ||
target.LastModifiedBy = new Uri($"http://example.id/{resource.LastModifiedBy}"); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
LeedsExperiment/Preservation.API/Models.cs → ...eriment/Preservation.API/Models/Models.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Preservation.API.Models; | ||
|
||
public class UriGenerator(IHttpContextAccessor httpContextAccessor) | ||
{ | ||
[return: NotNullIfNotNull(nameof(storageUri))] | ||
public Uri? GetRepositoryPath(Uri? storageUri, string? version = null) | ||
{ | ||
if (storageUri == null) return null; | ||
|
||
// Only append ?version if provided as arg, so use AbsolutePath only be default | ||
var request = httpContextAccessor.HttpContext.Request; | ||
var uriBuilder = new UriBuilder(request.Scheme, request.Host.Host) | ||
{ | ||
Port = request.Host.Port ?? 80, | ||
Path = GetPreservationPath(storageUri) | ||
}; | ||
|
||
if (!string.IsNullOrEmpty(version)) | ||
{ | ||
uriBuilder.Query = $"version={version}"; | ||
} | ||
|
||
return uriBuilder.Uri; | ||
} | ||
|
||
private static string GetPreservationPath(Uri storageUri) | ||
{ | ||
var storageUriAbsolutePath = storageUri.AbsolutePath; | ||
const string storageApiPrefix = "/api"; | ||
return Uri.UnescapeDataString(storageUriAbsolutePath[..4] == storageApiPrefix | ||
? storageUriAbsolutePath[4..] | ||
: storageUriAbsolutePath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters