zip file for tiles

This commit is contained in:
Anton Martynenko
2025-11-20 12:17:57 +01:00
parent 9048a7b3ec
commit 661396554f
10 changed files with 303 additions and 7 deletions
@@ -1,3 +1,4 @@
using System.IO.Compression;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
@@ -307,13 +308,21 @@ public class RouteProcessingService : BackgroundService
await StitchRouteTilesAsync(allTiles.Values.ToList(), stitchedImagePath, route.ZoomLevel, geofencePolygonBounds, routePoints, cancellationToken);
}
string? tilesZipPath = null;
if (route.CreateTilesZip)
{
tilesZipPath = Path.Combine(readyDir, $"route_{routeId}_tiles.zip");
await CreateTilesZipAsync(tilesZipPath, allTiles.Values, cancellationToken);
}
var summaryPath = Path.Combine(readyDir, $"route_{routeId}_summary.txt");
await GenerateRouteSummaryAsync(summaryPath, route, allTiles.Count, totalTilesFromRegions, duplicateTiles, cancellationToken);
await GenerateRouteSummaryAsync(summaryPath, route, allTiles.Count, totalTilesFromRegions, duplicateTiles, tilesZipPath, cancellationToken);
route.MapsReady = true;
route.CsvFilePath = csvPath;
route.SummaryFilePath = summaryPath;
route.StitchedImagePath = stitchedImagePath;
route.TilesZipPath = tilesZipPath;
route.UpdatedAt = DateTime.UtcNow;
await _routeRepository.UpdateRouteAsync(route);
@@ -400,6 +409,7 @@ public class RouteProcessingService : BackgroundService
int uniqueTiles,
int totalTilesFromRegions,
int duplicateTiles,
string? tilesZipPath,
CancellationToken cancellationToken)
{
var summary = new System.Text.StringBuilder();
@@ -428,6 +438,10 @@ public class RouteProcessingService : BackgroundService
{
summary.AppendLine($"- Stitched Map: route_{route.Id}_stitched.jpg");
}
if (tilesZipPath != null)
{
summary.AppendLine($"- Tiles ZIP: route_{route.Id}_tiles.zip");
}
summary.AppendLine();
summary.AppendLine($"Completed: {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC");
@@ -670,6 +684,52 @@ public class RouteProcessingService : BackgroundService
}
}
}
private Task CreateTilesZipAsync(
string zipFilePath,
IEnumerable<TileInfo> tiles,
CancellationToken cancellationToken)
{
return Task.Run(() =>
{
if (File.Exists(zipFilePath))
{
File.Delete(zipFilePath);
}
using var zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create);
int addedFiles = 0;
int missingFiles = 0;
foreach (var tile in tiles)
{
if (cancellationToken.IsCancellationRequested)
break;
if (File.Exists(tile.FilePath))
{
try
{
var fileName = Path.GetFileName(tile.FilePath);
zipArchive.CreateEntryFromFile(tile.FilePath, fileName, CompressionLevel.Optimal);
addedFiles++;
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to add tile to zip: {FilePath}", tile.FilePath);
}
}
else
{
_logger.LogWarning("Tile file not found for zip: {FilePath}", tile.FilePath);
missingFiles++;
}
}
_logger.LogInformation("Tiles zip created: {ZipPath} with {AddedFiles} tiles ({MissingFiles} missing)",
zipFilePath, addedFiles, missingFiles);
}, cancellationToken);
}
}
public class TileInfo