feat(sources): add multiple sources
This commit is contained in:
@@ -6,6 +6,7 @@ using TileServer.Api.Http;
|
||||
using TileServer.Application.Accounts;
|
||||
using TileServer.Application.Configuration;
|
||||
using TileServer.Application.Extracts;
|
||||
using TileServer.Application.Styles;
|
||||
using TileServer.Application.Tiles;
|
||||
using TileServer.Domain.Exceptions;
|
||||
using TileServer.Domain.Tiles;
|
||||
@@ -72,17 +73,19 @@ public sealed class PersonalDeliveryController(
|
||||
{
|
||||
var access = await ResolveAsync(slug, ct).ConfigureAwait(false);
|
||||
var token = AccessToken.Read(Request) ?? string.Empty;
|
||||
var extract = string.IsNullOrWhiteSpace(source)
|
||||
? extracts.GetAll().FirstOrDefault(e => e.Enabled)
|
||||
: extracts.Find(source);
|
||||
if (extract is null)
|
||||
var selected = ExtractSourceQuery.Resolve(source, extracts);
|
||||
if (selected.Count == 0)
|
||||
{
|
||||
throw new ResourceNotFoundException("Extract", source ?? "(none configured)");
|
||||
}
|
||||
|
||||
var tilesUrl =
|
||||
$"{PublicUrl.GetBase(Request, options)}/u/{access.User.Slug}/tiles/{extract.Id}/{{z}}/{{x}}/{{y}}.pbf?v=3&token={Uri.EscapeDataString(token)}";
|
||||
var style = await styles.GetForDeliveryAsync(access.User.Slug, name, extract.Id, tilesUrl, ct)
|
||||
var root = PublicUrl.GetBase(Request, options);
|
||||
var bound = selected
|
||||
.Select(extract => new BoundExtract(
|
||||
extract.Id,
|
||||
$"{root}/u/{access.User.Slug}/tiles/{extract.Id}/{{z}}/{{x}}/{{y}}.pbf?v=3&token={Uri.EscapeDataString(token)}"))
|
||||
.ToArray();
|
||||
var style = await styles.GetForDeliveryAsync(access.User.Slug, name, bound, ct)
|
||||
.ConfigureAwait(false);
|
||||
var json = style.ToJsonString();
|
||||
usage.RecordStyle(access.User.Id, access.Token.Id, Encoding.UTF8.GetByteCount(json));
|
||||
|
||||
@@ -17,5 +17,9 @@ public interface IUserStyleService
|
||||
|
||||
Task<IReadOnlyList<StyleListItem>> ListForDeliveryAsync(string slug, string baseUrl, string token, CancellationToken ct);
|
||||
|
||||
Task<JsonObject> GetForDeliveryAsync(string slug, string name, string extractId, string tilesUrl, CancellationToken ct);
|
||||
Task<JsonObject> GetForDeliveryAsync(
|
||||
string slug,
|
||||
string name,
|
||||
IReadOnlyList<BoundExtract> extracts,
|
||||
CancellationToken ct);
|
||||
}
|
||||
|
||||
@@ -130,8 +130,7 @@ public sealed class UserStyleService(
|
||||
public async Task<JsonObject> GetForDeliveryAsync(
|
||||
string slug,
|
||||
string name,
|
||||
string extractId,
|
||||
string tilesUrl,
|
||||
IReadOnlyList<BoundExtract> extracts,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var styleName = ResourceName.Require(name, nameof(name));
|
||||
@@ -139,7 +138,7 @@ public sealed class UserStyleService(
|
||||
?? throw new ResourceNotFoundException("User", slug);
|
||||
var own = await styles.FindAsync(user.Id, styleName, ct).ConfigureAwait(false);
|
||||
var raw = own is null ? catalog.GetRequired(styleName) : Parse(own.Json, styleName);
|
||||
return styleService.Bind(raw, extractId, tilesUrl);
|
||||
return styleService.Bind(raw, extracts);
|
||||
}
|
||||
|
||||
private static JsonObject Parse(string json, string name)
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
using TileServer.Domain;
|
||||
using TileServer.Domain.Exceptions;
|
||||
using TileServer.Domain.Extracts;
|
||||
|
||||
namespace TileServer.Application.Extracts;
|
||||
|
||||
public static class ExtractSourceQuery
|
||||
{
|
||||
public const int MaxCount = 16;
|
||||
|
||||
public static IReadOnlyList<Extract> Resolve(string? source, IExtractCatalog catalog)
|
||||
{
|
||||
var ids = ParseIds(source, catalog);
|
||||
var result = new List<Extract>(ids.Count);
|
||||
foreach (var id in ids)
|
||||
{
|
||||
result.Add(catalog.Find(id) ?? throw new ResourceNotFoundException("Extract", id));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<string> ParseIds(string? source, IExtractCatalog catalog)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(source))
|
||||
{
|
||||
var first = catalog.GetAll().FirstOrDefault(item => item.Enabled);
|
||||
return first is null ? [] : [first.Id];
|
||||
}
|
||||
|
||||
var seen = new HashSet<string>(StringComparer.Ordinal);
|
||||
var ids = new List<string>();
|
||||
foreach (var part in source.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
|
||||
{
|
||||
var id = ResourceName.Require(part, nameof(source));
|
||||
if (!seen.Add(id))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ids.Add(id);
|
||||
if (ids.Count >= MaxCount)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ids.Count == 0)
|
||||
{
|
||||
throw new DomainValidationException("At least one source is required.");
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ namespace TileServer.Application.Styles;
|
||||
|
||||
public sealed record StyleListItem(string Name, string Url);
|
||||
|
||||
public sealed record BoundExtract(string ExtractId, string TilesUrl);
|
||||
|
||||
public interface IStyleService
|
||||
{
|
||||
IReadOnlyList<StyleListItem> List(string baseUrl);
|
||||
@@ -12,5 +14,7 @@ public interface IStyleService
|
||||
|
||||
JsonObject Bind(JsonObject style, string extractId, string tilesUrl);
|
||||
|
||||
JsonObject Bind(JsonObject style, IReadOnlyList<BoundExtract> extracts);
|
||||
|
||||
Task SaveAsync(string name, JsonObject style, CancellationToken ct);
|
||||
}
|
||||
|
||||
@@ -31,11 +31,26 @@ public sealed class StyleService(
|
||||
}
|
||||
|
||||
public JsonObject Bind(JsonObject style, string extractId, string tilesUrl)
|
||||
=> Bind(style, [new BoundExtract(extractId, tilesUrl)]);
|
||||
|
||||
public JsonObject Bind(JsonObject style, IReadOnlyList<BoundExtract> extracts)
|
||||
{
|
||||
var extract = ResolveExtract(extractId);
|
||||
if (extracts.Count == 0)
|
||||
{
|
||||
throw new DomainValidationException("At least one extract is required.");
|
||||
}
|
||||
|
||||
var clone = JsonNode.Parse(style.ToJsonString())?.AsObject()
|
||||
?? throw new DomainValidationException("Style must be a JSON object.");
|
||||
BindRuntimeUrls(clone, extract, tilesUrl);
|
||||
var first = extracts[0];
|
||||
BindRuntimeUrls(clone, ResolveExtract(first.ExtractId), first.TilesUrl);
|
||||
var primaryKeys = VectorSourceKeys(clone);
|
||||
for (var i = 1; i < extracts.Count; i++)
|
||||
{
|
||||
var extra = extracts[i];
|
||||
AppendExtract(clone, ResolveExtract(extra.ExtractId), extra.TilesUrl, primaryKeys);
|
||||
}
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
@@ -87,6 +102,71 @@ public sealed class StyleService(
|
||||
}
|
||||
}
|
||||
|
||||
private void AppendExtract(
|
||||
JsonObject style,
|
||||
Extract extract,
|
||||
string tilesUrl,
|
||||
IReadOnlyList<string> primarySourceKeys)
|
||||
{
|
||||
if (style["sources"] is not JsonObject sources || style["layers"] is not JsonArray layers)
|
||||
{
|
||||
throw new DomainValidationException("Style must have sources and layers.");
|
||||
}
|
||||
|
||||
var metadata = tileStore.GetMetadata(extract.Id);
|
||||
var minZoom = metadata?.MinZoom ?? extract.MinZoom;
|
||||
var maxZoom = metadata?.MaxZoom ?? extract.MaxZoom;
|
||||
var suffix = "__" + extract.Id;
|
||||
var primary = primarySourceKeys.ToHashSet(StringComparer.Ordinal);
|
||||
|
||||
foreach (var key in primarySourceKeys)
|
||||
{
|
||||
sources[key + suffix] = CreateVectorSource(tilesUrl, minZoom, maxZoom);
|
||||
}
|
||||
|
||||
var extras = new List<JsonNode>();
|
||||
foreach (var node in layers)
|
||||
{
|
||||
if (node is not JsonObject layer)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var sourceName = layer["source"]?.GetValue<string>();
|
||||
if (sourceName is null || !primary.Contains(sourceName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var copy = JsonNode.Parse(layer.ToJsonString())?.AsObject()
|
||||
?? throw new DomainValidationException("Layer clone failed.");
|
||||
var id = layer["id"]?.GetValue<string>() ?? "layer";
|
||||
copy["id"] = id + suffix;
|
||||
copy["source"] = sourceName + suffix;
|
||||
extras.Add(copy);
|
||||
}
|
||||
|
||||
foreach (var extra in extras)
|
||||
{
|
||||
layers.Add(extra);
|
||||
}
|
||||
}
|
||||
|
||||
private static IReadOnlyList<string> VectorSourceKeys(JsonObject style)
|
||||
{
|
||||
if (style["sources"] is not JsonObject sources)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
return sources
|
||||
.Where(property =>
|
||||
property.Value is JsonObject source &&
|
||||
string.Equals(source["type"]?.GetValue<string>(), "vector", StringComparison.OrdinalIgnoreCase))
|
||||
.Select(property => property.Key)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
private static JsonObject CreateVectorSource(string tilesUrl, int minZoom, int maxZoom)
|
||||
{
|
||||
var source = new JsonObject
|
||||
|
||||
Reference in New Issue
Block a user