| | | 1 | | @using Chronicis.Shared.DTOs |
| | | 2 | | @using Microsoft.AspNetCore.Components.Forms |
| | | 3 | | @inject IWorldApiService WorldApi |
| | | 4 | | @inject ISnackbar Snackbar |
| | | 5 | | @inject IJSRuntime JSRuntime |
| | | 6 | | |
| | | 7 | | <MudDialog> |
| | | 8 | | <DialogContent> |
| | | 9 | | <MudStack Spacing="3"> |
| | 0 | 10 | | @if (_isUploading) |
| | | 11 | | { |
| | | 12 | | <MudProgressLinear Color="Color.Primary" Indeterminate="true" /> |
| | | 13 | | <MudText Typo="Typo.body2" Align="Align.Center"> |
| | 0 | 14 | | @_uploadStatus |
| | | 15 | | </MudText> |
| | | 16 | | } |
| | | 17 | | else |
| | | 18 | | { |
| | | 19 | | <MudPaper Outlined="true" Class="pa-4" Style="border-style: dashed;"> |
| | | 20 | | <MudStack AlignItems="AlignItems.Center" Spacing="2"> |
| | | 21 | | <MudIcon Icon="@Icons.Material.Filled.CloudUpload" Size="Size.Large" Color="Color.Primary" /> |
| | | 22 | | <InputFile id="fileInput" |
| | | 23 | | OnChange="OnFileSelected" |
| | | 24 | | accept=".pdf,.docx,.xlsx,.pptx,.txt,.md,.png,.jpg,.jpeg,.gif,.webp" |
| | | 25 | | style="display: none;" /> |
| | | 26 | | <label for="fileInput" style="cursor: pointer; width: 100%;"> |
| | | 27 | | <MudText Typo="Typo.h6" Align="Align.Center"> |
| | 0 | 28 | | @(_selectedFile == null ? "Click to select file" : _selectedFile.Name) |
| | | 29 | | </MudText> |
| | | 30 | | </label> |
| | | 31 | | <MudText Typo="Typo.body2" Color="Color.Secondary"> |
| | | 32 | | Maximum file size: 200 MB |
| | | 33 | | </MudText> |
| | | 34 | | <MudText Typo="Typo.body2" Color="Color.Secondary"> |
| | | 35 | | Supported: PDF, Word, Excel, PowerPoint, Text, Markdown, Images |
| | | 36 | | </MudText> |
| | | 37 | | </MudStack> |
| | | 38 | | </MudPaper> |
| | | 39 | | |
| | 0 | 40 | | @if (_selectedFile != null) |
| | | 41 | | { |
| | | 42 | | <MudTextField @bind-Value="_title" |
| | | 43 | | Label="Title" |
| | | 44 | | Variant="Variant.Outlined" |
| | | 45 | | HelperText="Optional: Customize the display name (defaults to filename)" /> |
| | | 46 | | |
| | | 47 | | <MudTextField @bind-Value="_description" |
| | | 48 | | Label="Description" |
| | | 49 | | Variant="Variant.Outlined" |
| | | 50 | | Lines="3" |
| | | 51 | | HelperText="Optional: Add a description for this document" /> |
| | | 52 | | |
| | 0 | 53 | | @if (!string.IsNullOrEmpty(_validationError)) |
| | | 54 | | { |
| | 0 | 55 | | <MudAlert Severity="Severity.Error">@_validationError</MudAlert> |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | </MudStack> |
| | | 60 | | </DialogContent> |
| | | 61 | | <DialogActions> |
| | | 62 | | <MudButton OnClick="Cancel" Disabled="_isUploading">Cancel</MudButton> |
| | | 63 | | <MudButton Color="Color.Primary" |
| | | 64 | | OnClick="Upload" |
| | | 65 | | Disabled="_selectedFile == null || _isUploading"> |
| | | 66 | | Upload |
| | | 67 | | </MudButton> |
| | | 68 | | </DialogActions> |
| | | 69 | | </MudDialog> |
| | | 70 | | |
| | | 71 | | @code { |
| | 0 | 72 | | [CascadingParameter] MudDialogInstance MudDialog { get; set; } = null!; |
| | 0 | 73 | | [Parameter] public Guid WorldId { get; set; } |
| | | 74 | | |
| | | 75 | | private IBrowserFile? _selectedFile; |
| | | 76 | | private byte[]? _fileData; |
| | 0 | 77 | | private string _title = string.Empty; |
| | 0 | 78 | | private string _description = string.Empty; |
| | 0 | 79 | | private string _validationError = string.Empty; |
| | | 80 | | private bool _isUploading = false; |
| | 0 | 81 | | private string _uploadStatus = string.Empty; |
| | | 82 | | |
| | | 83 | | private const long MaxFileSize = 200 * 1024 * 1024; // 200 MB |
| | 0 | 84 | | private static readonly HashSet<string> AllowedExtensions = new() |
| | 0 | 85 | | { |
| | 0 | 86 | | ".pdf", ".docx", ".xlsx", ".pptx", ".txt", ".md", |
| | 0 | 87 | | ".png", ".jpg", ".jpeg", ".gif", ".webp" |
| | 0 | 88 | | }; |
| | | 89 | | |
| | | 90 | | private async Task OnFileSelected(InputFileChangeEventArgs e) |
| | | 91 | | { |
| | 0 | 92 | | _selectedFile = e.File; |
| | 0 | 93 | | _fileData = null; |
| | 0 | 94 | | _validationError = string.Empty; |
| | | 95 | | |
| | 0 | 96 | | if (_selectedFile != null) |
| | | 97 | | { |
| | | 98 | | // Default title to filename without extension |
| | 0 | 99 | | if (string.IsNullOrWhiteSpace(_title)) |
| | | 100 | | { |
| | 0 | 101 | | _title = Path.GetFileNameWithoutExtension(_selectedFile.Name); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | // Validate file |
| | 0 | 105 | | if (_selectedFile.Size > MaxFileSize) |
| | | 106 | | { |
| | 0 | 107 | | _validationError = $"File size ({FormatFileSize(_selectedFile.Size)}) exceeds maximum of 200 MB"; |
| | 0 | 108 | | _selectedFile = null; |
| | 0 | 109 | | return; |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | var extension = Path.GetExtension(_selectedFile.Name).ToLowerInvariant(); |
| | 0 | 113 | | if (!AllowedExtensions.Contains(extension)) |
| | | 114 | | { |
| | 0 | 115 | | _validationError = $"File type {extension} is not supported"; |
| | 0 | 116 | | _selectedFile = null; |
| | 0 | 117 | | return; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | // Read file data immediately to avoid disposal issues |
| | | 121 | | try |
| | | 122 | | { |
| | 0 | 123 | | using var stream = _selectedFile.OpenReadStream(MaxFileSize); |
| | 0 | 124 | | using var memoryStream = new MemoryStream(); |
| | 0 | 125 | | await stream.CopyToAsync(memoryStream); |
| | 0 | 126 | | _fileData = memoryStream.ToArray(); |
| | 0 | 127 | | } |
| | 0 | 128 | | catch (Exception ex) |
| | | 129 | | { |
| | 0 | 130 | | _validationError = $"Failed to read file: {ex.Message}"; |
| | 0 | 131 | | _selectedFile = null; |
| | 0 | 132 | | _fileData = null; |
| | 0 | 133 | | } |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | StateHasChanged(); |
| | 0 | 137 | | } |
| | | 138 | | |
| | | 139 | | private async Task Upload() |
| | | 140 | | { |
| | 0 | 141 | | if (_selectedFile == null || _fileData == null) return; |
| | | 142 | | |
| | 0 | 143 | | _isUploading = true; |
| | 0 | 144 | | _uploadStatus = "Preparing upload..."; |
| | 0 | 145 | | StateHasChanged(); |
| | | 146 | | |
| | | 147 | | try |
| | | 148 | | { |
| | | 149 | | // Step 1: Request upload (get SAS URL) |
| | 0 | 150 | | var request = new WorldDocumentUploadRequestDto |
| | 0 | 151 | | { |
| | 0 | 152 | | FileName = _selectedFile.Name, |
| | 0 | 153 | | ContentType = _selectedFile.ContentType, |
| | 0 | 154 | | FileSizeBytes = _selectedFile.Size, |
| | 0 | 155 | | Description = _description |
| | 0 | 156 | | }; |
| | | 157 | | |
| | 0 | 158 | | var uploadResponse = await WorldApi.RequestDocumentUploadAsync(WorldId, request); |
| | | 159 | | |
| | 0 | 160 | | if (uploadResponse == null) |
| | | 161 | | { |
| | 0 | 162 | | throw new Exception("Failed to get upload URL"); |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | // Use the server-generated title (may have been renamed for uniqueness) |
| | 0 | 166 | | var finalTitle = uploadResponse.Title; |
| | | 167 | | |
| | 0 | 168 | | _uploadStatus = "Uploading file..."; |
| | 0 | 169 | | StateHasChanged(); |
| | | 170 | | |
| | | 171 | | // Step 2: Upload file directly to blob storage using SAS URL (from byte array) |
| | 0 | 172 | | using var content = new ByteArrayContent(_fileData); |
| | 0 | 173 | | content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(_selectedFile.ContentType); |
| | | 174 | | |
| | | 175 | | // Add required blob headers for Azure Storage |
| | 0 | 176 | | content.Headers.Add("x-ms-blob-type", "BlockBlob"); |
| | | 177 | | |
| | 0 | 178 | | using var httpClient = new HttpClient(); |
| | 0 | 179 | | httpClient.Timeout = TimeSpan.FromMinutes(5); // Allow time for large uploads |
| | | 180 | | |
| | 0 | 181 | | var uploadResult = await httpClient.PutAsync(uploadResponse.UploadUrl, content); |
| | | 182 | | |
| | 0 | 183 | | if (!uploadResult.IsSuccessStatusCode) |
| | | 184 | | { |
| | 0 | 185 | | var errorBody = await uploadResult.Content.ReadAsStringAsync(); |
| | 0 | 186 | | throw new Exception($"Blob upload failed ({uploadResult.StatusCode}): {errorBody}"); |
| | | 187 | | } |
| | | 188 | | |
| | 0 | 189 | | _uploadStatus = "Finalizing..."; |
| | 0 | 190 | | StateHasChanged(); |
| | | 191 | | |
| | | 192 | | // Step 3: Confirm upload |
| | 0 | 193 | | var document = await WorldApi.ConfirmDocumentUploadAsync(WorldId, uploadResponse.DocumentId); |
| | | 194 | | |
| | 0 | 195 | | if (document == null) |
| | | 196 | | { |
| | 0 | 197 | | throw new Exception("Failed to confirm upload"); |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | Snackbar.Add($"Document '{document.Title}' uploaded successfully", Severity.Success); |
| | 0 | 201 | | MudDialog.Close(DialogResult.Ok(document)); |
| | 0 | 202 | | } |
| | 0 | 203 | | catch (Exception ex) |
| | | 204 | | { |
| | 0 | 205 | | Snackbar.Add($"Upload failed: {ex.Message}", Severity.Error); |
| | 0 | 206 | | _isUploading = false; |
| | 0 | 207 | | _uploadStatus = string.Empty; |
| | 0 | 208 | | StateHasChanged(); |
| | 0 | 209 | | } |
| | 0 | 210 | | } |
| | | 211 | | |
| | | 212 | | private void Cancel() |
| | | 213 | | { |
| | 0 | 214 | | MudDialog.Cancel(); |
| | 0 | 215 | | } |
| | | 216 | | |
| | | 217 | | private static string FormatFileSize(long bytes) |
| | | 218 | | { |
| | 0 | 219 | | string[] sizes = { "B", "KB", "MB", "GB" }; |
| | 0 | 220 | | double len = bytes; |
| | 0 | 221 | | int order = 0; |
| | 0 | 222 | | while (len >= 1024 && order < sizes.Length - 1) |
| | | 223 | | { |
| | 0 | 224 | | order++; |
| | 0 | 225 | | len /= 1024; |
| | | 226 | | } |
| | 0 | 227 | | return $"{len:0.##} {sizes[order]}"; |
| | | 228 | | } |
| | | 229 | | } |