using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.RateLimiting; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Options; using EARSystem.Infrastructure; namespace EARSystem.Web.Controllers; [Authorize, EnableRateLimiting("maps"), Route("_AMapService")] public class MapProxyController(IHttpClientFactory factory, IOptions config) : ControllerBase { [HttpGet("{**path}")] public async Task Proxy(string path, CancellationToken ct) { string[] allowed = ["v4/map/styles", "v3/assistant/coordinate/convert", "v3/geocode/geo", "v3/geocode/regeo", "v3/place/around", "v3/place/text", "v3/place/detail", "v5/place/around", "v5/place/text"]; if (!allowed.Contains(path, StringComparer.Ordinal) || string.IsNullOrWhiteSpace(config.Value.JsSecurityCode)) return NotFound(); var query = Request.Query.ToDictionary(x => x.Key, x => (string?)x.Value.ToString()); query["jscode"] = config.Value.JsSecurityCode; query["key"] = config.Value.BrowserKey; var host = path == "v4/map/styles" ? "https://webapi.amap.com/" : "https://restapi.amap.com/"; using var response = await factory.CreateClient("MapProxy").GetAsync(QueryHelpers.AddQueryString(host + path, query), ct); if (!response.IsSuccessStatusCode) return StatusCode(503); Response.Headers.CacheControl = "no-store"; return File(await response.Content.ReadAsByteArrayAsync(ct), response.Content.Headers.ContentType?.ToString() ?? "application/json"); } }