mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-06-21 22:31:14 +00:00
6f23120c49
Extracts RouteRegionMatcher, RouteCsvWriter, RouteSummaryWriter, RouteImageRenderer, TilesZipBuilder, RegionFileCleaner from the ~750-LOC RouteProcessingService god-class. Moves TileInfo to its own file as a sealed record. Replaces IServiceProvider scope- locator with a direct IRegionService injection (folds AZ-360 / C08). Updates DI registration and tests. Tests: 133 / 133 unit + 5 / 5 smoke green; integration suite exit 0. Pixel-equivalent stitched route image and byte-equivalent CSV / summary / ZIP outputs verified through the smoke run. Co-authored-by: Cursor <cursoragent@cursor.com>
67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using SatelliteProvider.Common.Configs;
|
|
using SatelliteProvider.DataAccess.Models;
|
|
|
|
namespace SatelliteProvider.Services.RouteManagement;
|
|
|
|
// AZ-364 / C11: extracted from RouteProcessingService.CleanupRegionFilesAsync.
|
|
// Deletes per-region files (CSV, summary, stitched image) once a route has
|
|
// successfully consumed them. Each delete is best-effort: a failed delete is
|
|
// logged at warning level and does not abort the cleanup of the remaining
|
|
// files. Stitched image path is reconstructed from regionId + ReadyDirectory
|
|
// because regions historically did not always persist that path on the entity.
|
|
public class RegionFileCleaner
|
|
{
|
|
private readonly StorageConfig _storageConfig;
|
|
private readonly ILogger<RegionFileCleaner> _logger;
|
|
|
|
public RegionFileCleaner(IOptions<StorageConfig> storageConfig, ILogger<RegionFileCleaner> logger)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(storageConfig);
|
|
_storageConfig = storageConfig.Value;
|
|
_logger = logger;
|
|
}
|
|
|
|
public Task CleanupAsync(IEnumerable<RegionEntity> regions, CancellationToken cancellationToken = default)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(regions);
|
|
|
|
foreach (var region in regions)
|
|
{
|
|
if (cancellationToken.IsCancellationRequested)
|
|
break;
|
|
|
|
if (region == null) continue;
|
|
|
|
DeleteIfPresent(region.CsvFilePath, "region CSV file");
|
|
DeleteIfPresent(region.SummaryFilePath, "region summary file");
|
|
|
|
var stitchedImagePath = Path.Combine(_storageConfig.ReadyDirectory, $"region_{region.Id}_stitched.jpg");
|
|
if (File.Exists(stitchedImagePath))
|
|
{
|
|
DeleteIfPresent(stitchedImagePath, "region stitched image");
|
|
}
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private void DeleteIfPresent(string? path, string label)
|
|
{
|
|
if (string.IsNullOrEmpty(path) || !File.Exists(path))
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
File.Delete(path);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning(ex, "Failed to delete {Label}: {FilePath}", label, path);
|
|
}
|
|
}
|
|
}
|