-
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
Showing
15 changed files
with
1,000 additions
and
9 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
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
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,52 @@ | ||
using GeoCop.Api.Models; | ||
using Microsoft.AspNetCore.StaticFiles; | ||
using System.Net.Mime; | ||
|
||
namespace GeoCop.Api | ||
{ | ||
/// <summary> | ||
/// Provides access to file content types. | ||
/// </summary> | ||
public static class IContentTypeProviderExtensions | ||
{ | ||
private const string DefaultContentType = "application/octet-stream"; | ||
|
||
/// <summary> | ||
/// Returns the <see cref="ContentType"/> for the specified <see cref="Asset"/>. | ||
/// </summary> | ||
/// <param name="contentTypeProvider">The IContentTypeProvider to extend.</param> | ||
/// <param name="asset">The asset from which the content type should be read.</param> | ||
/// <returns>The <see cref="ContentType"/>.</returns> | ||
public static ContentType GetContentType(this IContentTypeProvider contentTypeProvider, Asset asset) | ||
{ | ||
return contentTypeProvider.GetContentType(asset.OriginalFilename); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the <see cref="ContentType"/> for the specified file extension. | ||
/// </summary> | ||
/// <param name="contentTypeProvider">The IContentTypeProvider to extend.</param> | ||
/// <param name="fileName">The file from which the content type should be read.</param> | ||
/// <returns>The <see cref="ContentType"/>.</returns> | ||
public static ContentType GetContentType(this IContentTypeProvider contentTypeProvider, string fileName) | ||
{ | ||
return new ContentType(contentTypeProvider.GetContentTypeAsString(fileName)); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the <see cref="ContentType"/> for the specified file extension. | ||
/// </summary> | ||
/// <param name="contentTypeProvider">The IContentTypeProvider to extend.</param> | ||
/// <param name="fileName">The file from which the content type should be read.</param> | ||
/// <returns>The content type as string.</returns> | ||
public static string GetContentTypeAsString(this IContentTypeProvider contentTypeProvider, string fileName) | ||
{ | ||
if (!contentTypeProvider.TryGetContentType(fileName, out var contentType)) | ||
{ | ||
contentType = DefaultContentType; | ||
} | ||
|
||
return contentType; | ||
} | ||
} | ||
} |
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,56 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Stac; | ||
using Stac.Api.Interfaces; | ||
using Stac.Api.WebApi.Implementations.Default; | ||
|
||
namespace GeoCop.Api.StacServices | ||
{ | ||
/// <summary> | ||
/// Provides access to STAC collections. | ||
/// </summary> | ||
public class StacCollectionsProvider : ICollectionsProvider | ||
{ | ||
private readonly ILogger<StacCollectionsProvider> logger; | ||
private readonly IDbContextFactory<Context> contextFactory; | ||
private readonly StacConverter stacConverter; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="StacCollectionsProvider"/> class. | ||
/// </summary> | ||
public StacCollectionsProvider(ILogger<StacCollectionsProvider> logger, IDbContextFactory<Context> contextFactory, StacConverter stacConverter) | ||
{ | ||
this.logger = logger; | ||
this.contextFactory = contextFactory; | ||
this.stacConverter = stacConverter; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public Task<StacCollection> GetCollectionByIdAsync(string collectionId, IStacApiContext stacApiContext, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
using var db = contextFactory.CreateDbContext(); | ||
var deliveryMandate = db.DeliveryMandatesWithIncludes.FirstOrDefault(dm => stacConverter.GetCollectionId(dm) == collectionId) | ||
?? throw new InvalidOperationException($"Collection with id {collectionId} does not exist."); | ||
var collection = stacConverter.ToStacCollection(deliveryMandate); | ||
return Task.FromResult(collection); | ||
} | ||
catch (Exception ex) | ||
{ | ||
var message = $"Error while getting collection with id {collectionId}"; | ||
logger.LogError(ex, message); | ||
throw new InvalidOperationException(message, ex); | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public Task<IEnumerable<StacCollection>> GetCollectionsAsync(IStacApiContext stacApiContext, CancellationToken cancellationToken = default) | ||
{ | ||
using var db = contextFactory.CreateDbContext(); | ||
var collections = db.DeliveryMandatesWithIncludes.Select(stacConverter.ToStacCollection); | ||
stacApiContext.Properties.SetProperty(DefaultConventions.MatchedCountPropertiesKey, collections.Count()); | ||
|
||
return Task.FromResult<IEnumerable<StacCollection>>(collections); | ||
} | ||
} | ||
} |
Oops, something went wrong.