94 lines
3.4 KiB
C#
94 lines
3.4 KiB
C#
using Microsoft.Extensions.Options;
|
|
using TileServer.Application.Configuration;
|
|
using TileServer.Application.Tiles;
|
|
using TileServer.Domain;
|
|
using TileServer.Domain.Exceptions;
|
|
using TileServer.Domain.Extracts;
|
|
using TileServer.Domain.Tiles;
|
|
|
|
namespace TileServer.Application.Extracts;
|
|
|
|
public sealed class ExtractService(
|
|
IExtractCatalog catalog,
|
|
IExtractStateStore stateStore,
|
|
ITileStore tileStore,
|
|
IOptions<TileServerOptions> options) : IExtractService
|
|
{
|
|
public async Task<IReadOnlyList<ExtractResponse>> ListAsync(string baseUrl, CancellationToken ct)
|
|
{
|
|
var result = new List<ExtractResponse>();
|
|
foreach (var extract in catalog.GetAll())
|
|
{
|
|
result.Add(await MapAsync(extract, baseUrl, ct).ConfigureAwait(false));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<ExtractResponse> GetAsync(string id, string baseUrl, CancellationToken ct)
|
|
=> await MapAsync(catalog.GetRequired(id), baseUrl, ct).ConfigureAwait(false);
|
|
|
|
public async Task<ExtractResponse> AddOrUpdateAsync(CreateExtractRequest request, string baseUrl, CancellationToken ct)
|
|
{
|
|
var id = ResourceName.Require(request.Id, nameof(request.Id));
|
|
if (string.IsNullOrWhiteSpace(request.Name))
|
|
{
|
|
throw new DomainValidationException("Extract name is required.");
|
|
}
|
|
|
|
if (!Uri.TryCreate(request.Url, UriKind.Absolute, out var url) ||
|
|
(url.Scheme != Uri.UriSchemeHttp && url.Scheme != Uri.UriSchemeHttps))
|
|
{
|
|
throw new DomainValidationException("Extract URL must be an absolute http(s) URI.");
|
|
}
|
|
|
|
var defaults = options.Value;
|
|
var center = request.Center is { Length: >= 2 } ? request.Center : [37.6173, 55.7558];
|
|
var extract = new Extract
|
|
{
|
|
Id = id,
|
|
Name = request.Name.Trim(),
|
|
Url = url,
|
|
CenterLon = center[0],
|
|
CenterLat = center[1],
|
|
MinZoom = request.MinZoom ?? defaults.DefaultMinZoom,
|
|
MaxZoom = PlanetilerLimits.ClampMaxZoom(request.MaxZoom ?? defaults.DefaultMaxZoom),
|
|
Enabled = request.Enabled
|
|
};
|
|
|
|
await catalog.AddOrUpdateAsync(extract, ct).ConfigureAwait(false);
|
|
return await MapAsync(extract, baseUrl, ct).ConfigureAwait(false);
|
|
}
|
|
|
|
public Task RemoveAsync(string id, CancellationToken ct)
|
|
=> catalog.RemoveAsync(ResourceName.Require(id, nameof(id)), ct);
|
|
|
|
private async Task<ExtractResponse> MapAsync(Extract extract, string baseUrl, CancellationToken ct)
|
|
{
|
|
var state = await stateStore.GetAsync(extract.Id, ct).ConfigureAwait(false);
|
|
var metadata = tileStore.GetMetadata(extract.Id);
|
|
var bounds = metadata is null
|
|
? null
|
|
: new TilesetBounds(metadata.MinLon, metadata.MinLat, metadata.MaxLon, metadata.MaxLat);
|
|
|
|
return new ExtractResponse(
|
|
extract.Id,
|
|
extract.Name,
|
|
extract.Url.ToString(),
|
|
extract.Enabled,
|
|
[extract.CenterLon, extract.CenterLat],
|
|
extract.MinZoom,
|
|
extract.MaxZoom,
|
|
string.Empty,
|
|
string.Empty,
|
|
tileStore.Exists(extract.Id) && state.Status == ExtractSyncState.Pending
|
|
? ExtractSyncState.Ready
|
|
: state.Status,
|
|
state.DownloadedAt,
|
|
state.BuiltAt,
|
|
state.PbfBytes,
|
|
state.LastError,
|
|
bounds);
|
|
}
|
|
}
|