< 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
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 62
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
ExportWorldToMarkdownAsync()0%2040%

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
 014    public ExportApiService(
 015        HttpClient httpClient,
 016        IJSRuntime jsRuntime,
 017        ILogger<ExportApiService> logger)
 18    {
 019        _httpClient = httpClient;
 020        _jsRuntime = jsRuntime;
 021        _logger = logger;
 022    }
 23
 24    public async Task<bool> ExportWorldToMarkdownAsync(Guid worldId, string worldName)
 25    {
 26        try
 27        {
 028            _logger.LogDebug("Starting export for world {WorldId} ({WorldName})", worldId, worldName);
 29
 030            var response = await _httpClient.GetAsync($"worlds/{worldId}/export");
 31
 032            if (!response.IsSuccessStatusCode)
 33            {
 034                _logger.LogWarning("Export failed for world {WorldId}. Status: {StatusCode}",
 035                    worldId, response.StatusCode);
 036                return false;
 37            }
 38
 39            // Get the zip file content
 040            var content = await response.Content.ReadAsByteArrayAsync();
 41
 42            // Build filename
 043            var safeWorldName = string.Join("_", worldName.Split(Path.GetInvalidFileNameChars()));
 044            if (safeWorldName.Length > 50)
 045                safeWorldName = safeWorldName.Substring(0, 50);
 046            var fileName = $"{safeWorldName}_export_{DateTime.UtcNow:yyyyMMdd_HHmmss}.zip";
 47
 48            // Trigger browser download via JavaScript
 049            await _jsRuntime.InvokeVoidAsync("chronicisDownloadFile", fileName, "application/zip", content);
 50
 051            _logger.LogDebug("Export download triggered for world {WorldId}. File: {FileName}, Size: {Size} bytes",
 052                worldId, fileName, content.Length);
 53
 054            return true;
 55        }
 056        catch (Exception ex)
 57        {
 058            _logger.LogError(ex, "Error exporting world {WorldId}", worldId);
 059            return false;
 60        }
 061    }
 62}