@page "/manage-uploads" @attribute [Authorize] @using BLAIzor.Components.Layout @using BLAIzor.Models.Editor @using BLAIzor.Services @using Microsoft.AspNetCore.Components.Authorization @layout AdminLayout @inject NavigationManager NavigationManager @inject IHttpContextAccessor HttpContextAccessor @inject AuthenticationStateProvider AuthenticationStateProvider @inject CustomAuthenticationStateProvider CustomAuthProvider @inject IJSRuntime JSRuntime @if (IsLoading) {

Loading content...

} else {

Manage Uploaded Files

@if (files == null || (!files.Images.Any() && !files.Videos.Any() && !files.Audio.Any())) {

No files uploaded yet.

} else {

Uploaded Images

@foreach (var image in files.Images) {
Uploaded Image
}

Uploaded Videos

@foreach (var video in files.Videos) {
}

Uploaded Audio

@foreach (var audio in files.Audio) {
}
} } @code { private UploadedFilesModel files = new UploadedFilesModel(); // Properly declared and initialized private string? userId; private string? userName; private AuthenticationState? authState; public bool IsLoading = false; private async Task CopyToClipboard(string filePath) { await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", filePath); await JSRuntime.InvokeVoidAsync("alert", "FilePath copied to clipboard"); } protected override async Task OnInitializedAsync() { authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); if (authState.User.Identity?.IsAuthenticated == true) { userId = CustomAuthProvider.GetUserId(); userName = CustomAuthProvider.GetUserName(); } await LoadFiles(); } private async Task LoadFiles() { var basePath = Path.Combine("wwwroot", "uploads", userId); files.Images = Directory.Exists(Path.Combine(basePath, "images")) ? Directory.GetFiles(Path.Combine(basePath, "images")) .Select(f => $"/uploads/{userId}/images/{Path.GetFileName(f)}") .ToList() : new List(); files.Videos = Directory.Exists(Path.Combine(basePath, "videos")) ? Directory.GetFiles(Path.Combine(basePath, "videos")) .Select(f => $"/uploads/{userId}/videos/{Path.GetFileName(f)}") .ToList() : new List(); files.Audio = Directory.Exists(Path.Combine(basePath, "audio")) ? Directory.GetFiles(Path.Combine(basePath, "audio")) .Select(f => $"/uploads/{userId}/audio/{Path.GetFileName(f)}") .ToList() : new List(); } private async Task DeleteFile(string filePath, string fileType) { var physicalPath = Path.Combine("wwwroot", "uploads", userId, fileType.ToLower(), Path.GetFileName(filePath)); if (File.Exists(physicalPath)) { File.Delete(physicalPath); await LoadFiles(); } } private async Task HandleFileUpload(InputFileChangeEventArgs e) { if (e.FileCount == 0) return; IsLoading = true; try { var uploadPath = Path.Combine("wwwroot", "uploads", userId); foreach (var file in e.GetMultipleFiles()) { var folder = GetFolderForFile(file.ContentType); var folderPath = Path.Combine(uploadPath, folder); // Create directories if they don't exist if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } // Save file var filePath = Path.Combine(folderPath, file.Name); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.OpenReadStream(50 * 1024 * 1024).CopyToAsync(stream); } // Add relative path to content var relativePath = $"/uploads/{userId}/{folder}/{file.Name}"; AppendFilePathToContent(file.ContentType, relativePath); } } catch (Exception ex) { Console.Write($"Error uploading files: {ex.Message}"); } finally { IsLoading = false; } } private string GetFolderForFile(string contentType) { return contentType switch { var type when type.StartsWith("image/") => "images", var type when type.StartsWith("video/") => "videos", var type when type.StartsWith("audio/") => "audio", _ => "others" }; } private void AppendFilePathToContent(string contentType, string relativePath) { if (contentType.StartsWith("image/")) { files.Images.Add(relativePath); } else if (contentType.StartsWith("video/")) { files.Videos.Add(relativePath); } else if (contentType.StartsWith("audio/")) { files.Audio.Add(relativePath); } StateHasChanged(); } }