< Summary

Information
Class: Chronicis.Client.Services.ExportApiService
Assembly: Chronicis.Client
File(s): /home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/ExportApiService.cs
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 62
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

/home/runner/work/chronicis/chronicis/src/Chronicis.Client/Services/ExportApiService.cs

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