@page "/tracks/new" @using System.Security.Claims @using DeepDrftManager.Services @attribute [Authorize] @inject ICmsTrackService CmsTrackService @inject AuthenticationStateProvider AuthStateProvider @inject NavigationManager Navigation @inject ISnackbar Snackbar @inject ILogger Logger Add Track — DeepDrft CMS Add Track WAV file @if (_selectedFile is not null) { Selected: @_selectedFile.Name (@FormatBytes(_selectedFile.Size)) } @if (!string.IsNullOrEmpty(_errorMessage)) { @_errorMessage } Cancel @if (_isUploading) { Uploading… } else { Upload } @code { // 1 GB ceiling matches DeepDrftAPI's per-request limit on api/track/upload; the // streaming path means the limit caps the request, not in-memory buffering. private const long MaxUploadBytes = 1_073_741_824L; private IBrowserFile? _selectedFile; private string _trackName = string.Empty; private string _artist = string.Empty; private string _album = string.Empty; private string _genre = string.Empty; private string _releaseDate = string.Empty; private string? _errorMessage; private bool _isUploading; private void OnFileSelected(InputFileChangeEventArgs e) { _selectedFile = e.File; _errorMessage = null; } private async Task SubmitAsync() { _errorMessage = null; if (_selectedFile is null) { _errorMessage = "Please select a WAV file."; return; } if (!_selectedFile.Name.EndsWith(".wav", StringComparison.OrdinalIgnoreCase)) { _errorMessage = "Selected file must be a .wav file."; return; } if (string.IsNullOrWhiteSpace(_trackName)) { _errorMessage = "Track Name is required."; return; } if (string.IsNullOrWhiteSpace(_artist)) { _errorMessage = "Artist is required."; return; } if (!string.IsNullOrWhiteSpace(_releaseDate) && !DateOnly.TryParseExact(_releaseDate, "yyyy-MM-dd", out _)) { _errorMessage = "Release Date must be in YYYY-MM-DD format."; return; } var authState = await AuthStateProvider.GetAuthenticationStateAsync(); var userIdValue = authState.User.FindFirstValue(ClaimTypes.NameIdentifier); if (!long.TryParse(userIdValue, out var createdByUserId)) { // The page is gated by [HierarchicalRoleAuthorize(Admin)], so a missing or // unparseable id here is a configuration bug, not normal client state. Logger.LogError("Authenticated user has no parseable NameIdentifier claim: {Value}", userIdValue); _errorMessage = "Your session is missing a valid identifier. Please sign in again."; return; } _isUploading = true; try { // OpenReadStream streams chunks from the browser via the SignalR circuit; the // service wraps it in StreamContent so the whole file is never materialised in // memory before DeepDrftAPI receives it. await using var fileStream = _selectedFile.OpenReadStream(MaxUploadBytes); var result = await CmsTrackService.UploadTrackAsync( fileStream, _selectedFile.Name, _selectedFile.ContentType, _trackName, _artist, string.IsNullOrWhiteSpace(_album) ? null : _album, string.IsNullOrWhiteSpace(_genre) ? null : _genre, string.IsNullOrWhiteSpace(_releaseDate) ? null : _releaseDate, createdByUserId); if (result.Success) { Snackbar.Add($"Uploaded '{_trackName}'.", Severity.Success); Navigation.NavigateTo("/tracks"); return; } var error = result.Messages.FirstOrDefault()?.Message ?? "Unknown error"; _errorMessage = $"Upload failed: {error}"; Logger.LogWarning("CMS upload rejected: {Error}", error); } catch (Exception ex) { Logger.LogError(ex, "Upload failed in TrackNew"); _errorMessage = "Upload failed. Please try again."; } finally { _isUploading = false; } } private void Cancel() { Navigation.NavigateTo("/tracks"); } private static string FormatBytes(long bytes) { const long KB = 1024; const long MB = KB * 1024; const long GB = MB * 1024; if (bytes >= GB) return $"{bytes / (double)GB:F2} GB"; if (bytes >= MB) return $"{bytes / (double)MB:F2} MB"; if (bytes >= KB) return $"{bytes / (double)KB:F2} KB"; return $"{bytes} bytes"; } }