| | | 1 | | using Microsoft.JSInterop; |
| | | 2 | | |
| | | 3 | | namespace Chronicis.Client.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Service for export operations - triggers file downloads via the API |
| | | 7 | | /// </summary> |
| | | 8 | | public class ExportApiService : IExportApiService |
| | | 9 | | { |
| | | 10 | | private readonly HttpClient _httpClient; |
| | | 11 | | private readonly IJSRuntime _jsRuntime; |
| | | 12 | | private readonly ILogger<ExportApiService> _logger; |
| | | 13 | | |
| | 0 | 14 | | public ExportApiService( |
| | 0 | 15 | | HttpClient httpClient, |
| | 0 | 16 | | IJSRuntime jsRuntime, |
| | 0 | 17 | | ILogger<ExportApiService> logger) |
| | | 18 | | { |
| | 0 | 19 | | _httpClient = httpClient; |
| | 0 | 20 | | _jsRuntime = jsRuntime; |
| | 0 | 21 | | _logger = logger; |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<bool> ExportWorldToMarkdownAsync(Guid worldId, string worldName) |
| | | 25 | | { |
| | | 26 | | try |
| | | 27 | | { |
| | 0 | 28 | | _logger.LogDebug("Starting export for world {WorldId} ({WorldName})", worldId, worldName); |
| | | 29 | | |
| | 0 | 30 | | var response = await _httpClient.GetAsync($"worlds/{worldId}/export"); |
| | | 31 | | |
| | 0 | 32 | | if (!response.IsSuccessStatusCode) |
| | | 33 | | { |
| | 0 | 34 | | _logger.LogWarning("Export failed for world {WorldId}. Status: {StatusCode}", |
| | 0 | 35 | | worldId, response.StatusCode); |
| | 0 | 36 | | return false; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | // Get the zip file content |
| | 0 | 40 | | var content = await response.Content.ReadAsByteArrayAsync(); |
| | | 41 | | |
| | | 42 | | // Build filename |
| | 0 | 43 | | var safeWorldName = string.Join("_", worldName.Split(Path.GetInvalidFileNameChars())); |
| | 0 | 44 | | if (safeWorldName.Length > 50) |
| | 0 | 45 | | safeWorldName = safeWorldName.Substring(0, 50); |
| | 0 | 46 | | var fileName = $"{safeWorldName}_export_{DateTime.UtcNow:yyyyMMdd_HHmmss}.zip"; |
| | | 47 | | |
| | | 48 | | // Trigger browser download via JavaScript |
| | 0 | 49 | | await _jsRuntime.InvokeVoidAsync("chronicisDownloadFile", fileName, "application/zip", content); |
| | | 50 | | |
| | 0 | 51 | | _logger.LogDebug("Export download triggered for world {WorldId}. File: {FileName}, Size: {Size} bytes", |
| | 0 | 52 | | worldId, fileName, content.Length); |
| | | 53 | | |
| | 0 | 54 | | return true; |
| | | 55 | | } |
| | 0 | 56 | | catch (Exception ex) |
| | | 57 | | { |
| | 0 | 58 | | _logger.LogError(ex, "Error exporting world {WorldId}", worldId); |
| | 0 | 59 | | return false; |
| | | 60 | | } |
| | 0 | 61 | | } |
| | | 62 | | } |