Skip to content

Commit

Permalink
Refactor StacStoreService to improve asset URI handling in collection
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelmathot committed Jun 4, 2024
1 parent 1b03baa commit 78358a5
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/Stars.Services/Store/StacStoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public async Task<StacItemNode> StoreItemNodeAtDestinationAsync(StacItemNode sta

public async Task<StacCollectionNode> StoreCollectionNodeAtDestinationAsync(StacCollectionNode stacCollectionNode, IDestination destination, CancellationToken ct)
{
PrepareStacCatalogueForDestination(stacCollectionNode, destination);
PrepareStacCollectionForDestination(stacCollectionNode, destination);
return await _stacRouter.RouteAsync(await StoreResourceAtDestinationAsync(stacCollectionNode, destination, ct), ct) as StacCollectionNode;
}

Expand Down Expand Up @@ -305,6 +305,16 @@ private void PrepareStacCatalogueForDestination(StacCatalogNode stacCatalogNode,
PrepareStacNodeForDestination(stacCatalogNode, destination);
}

private void PrepareStacCollectionForDestination(StacCollectionNode stacCollectionNode, IDestination destination)
{
PrepareStacNodeForDestination(stacCollectionNode, destination);
if (storeOptions.AbsoluteAssetsUrl)
MakeAssetUriAbsolute(stacCollectionNode, destination);
else
MakeAssetUriRelative(stacCollectionNode, destination);
}


public void PrepareStacItemForDestination(StacItemNode stacItemNode, IDestination destination)
{
PrepareStacNodeForDestination(stacItemNode, destination);
Expand All @@ -327,6 +337,16 @@ private void MakeAssetUriAbsolute(StacItemNode stacItemNode, IDestination destin
}
}

private void MakeAssetUriAbsolute(StacCollectionNode stacCollectionNode, IDestination destination)
{
MakeAssetUriRelative(stacCollectionNode, destination);
foreach (var asset in stacCollectionNode.StacCollection.Assets)
{
if (asset.Value.Uri.IsAbsoluteUri) continue;
asset.Value.Uri = new Uri(MapToFrontUri(destination.Uri), asset.Value.Uri);
}
}

private void MakeAssetUriRelative(StacItemNode stacItemNode, IDestination destination)
{
foreach (var asset in stacItemNode.StacItem.Assets)
Expand Down Expand Up @@ -356,6 +376,35 @@ private void MakeAssetUriRelative(StacItemNode stacItemNode, IDestination destin
}
}

private void MakeAssetUriRelative(StacCollectionNode stacCollectionNode, IDestination destination)
{
foreach (var asset in stacCollectionNode.StacCollection.Assets)
{
if (!asset.Value.Uri.IsAbsoluteUri) continue;
// 0. make sure the uri is not outside of the root catalog
var relativeUri = RootCatalogDestination.Uri.MakeRelativeUri(asset.Value.Uri);
if (relativeUri.IsAbsoluteUri || relativeUri.ToString().StartsWith("../"))
continue;
// 1. Check the asset uri can be relative to destination itself
relativeUri = destination.Uri.MakeRelativeUri(asset.Value.Uri);
if (!relativeUri.IsAbsoluteUri)
{
asset.Value.Uri = relativeUri;
continue;
}
// 1. Check the asset uri can be relative to root catalog
relativeUri = RootCatalogDestination.Uri.MakeRelativeUri(asset.Value.Uri);
if (relativeUri.IsAbsoluteUri) continue;
Uri absoluteUri = new Uri(RootCatalogNode.Uri, relativeUri);
relativeUri = stacCollectionNode.Uri.MakeRelativeUri(asset.Value.Uri);
if (!relativeUri.IsAbsoluteUri)
{
asset.Value.Uri = relativeUri;
continue;
}
}
}

public async Task<IAssetsContainer> GetAssetsInFolderAsync(string relPath, CancellationToken ct)
{
return await resourceServiceProvider.GetAssetsInFolderAsync(new GenericResource(new Uri(RootCatalogDestination.Uri, relPath)), ct);
Expand Down

0 comments on commit 78358a5

Please sign in to comment.