Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S2C proxy #254

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions ArmaRealMapWebSite/Controllers/S2CController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp;

namespace ArmaRealMapWebSite.Controllers
{
public class S2CController : Controller
{
private readonly string cacheLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "GameRealisticMap", "S2CloudlessCache");
private readonly SemaphoreSlim downloadSemaphore = new SemaphoreSlim(1, 1);

private readonly HttpClient httpClient;
private readonly ILogger<S2CController> logger;

public S2CController(IHttpClientFactory httpClientFactory, ILogger<S2CController> logger)
{
this.httpClient = httpClientFactory.CreateClient("S2C");
this.logger = logger;
}

[Route("sat/{dataset}/{zoom}/{y}/{x}.jpg")]
public async Task<ActionResult> GetTile(string dataset, int zoom, int y, int x)
{
if (dataset != "s2cloudless-2020" || zoom > 16 || zoom < 0 || x < 0 || y < 0)
{
return NotFound();
}
var endPoint = "https://tiles.maps.eox.at/wmts/1.0.0/s2cloudless-2020_3857/default/GoogleMapsCompatible/";
var filePath = FormattableString.Invariant($"{zoom}/{y}/{x}.jpg");
var cacheFile = System.IO.Path.Combine(cacheLocation, filePath);
if (!System.IO.File.Exists(cacheFile))
{
await downloadSemaphore.WaitAsync().ConfigureAwait(false);
try
{
if (!System.IO.File.Exists(cacheFile))
{
Directory.CreateDirectory(Path.GetDirectoryName(cacheFile));
var bytes = await Load(new Uri(endPoint + filePath, UriKind.Absolute)).ConfigureAwait(false);
var id = Image.DetectFormat(bytes);
if (id.DefaultMimeType == "image/jpeg")
{
await System.IO.File.WriteAllBytesAsync(cacheFile, bytes);
}
}
}
finally
{
downloadSemaphore.Release();
}
}
if (!System.IO.File.Exists(cacheFile))
{
return NotFound();
}
return File(await System.IO.File.ReadAllBytesAsync(cacheFile), "image/jpeg");
}

private async Task<byte[]> Load(Uri uri)
{
int sleep = 10;
while (sleep < 5000)
{
await Task.Delay(sleep).ConfigureAwait(false);
try
{
return await httpClient.GetByteArrayAsync(uri).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.Log(LogLevel.Warning, "{0}: {1}", uri.OriginalString, ex.Message);
}
sleep += 500;
}
throw new ApplicationException($"Failed to load '{uri}'");
}
}
}
6 changes: 6 additions & 0 deletions ArmaRealMapWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public void ConfigureServices(IServiceCollection services)
);
});

services.AddHttpClient("S2C", c => {

c.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/111.0");

});

if (Environment.OSVersion.Platform == PlatformID.Unix)
{
services.AddDataProtection()
Expand Down
Loading