@page "/manage-uploads" @attribute [Authorize] @using BLAIzor.Components.Layout @using BLAIzor.Models.Editor @using BLAIzor.Services @using Microsoft.AspNetCore.Components.Authorization @using SixLabors.ImageSharp @using SixLabors.ImageSharp.Processing @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); // Load IMAGES var imagesPath = Path.Combine(basePath, "images"); var thumbnailsPath = Path.Combine(imagesPath, "thumbnails"); if (!Directory.Exists(thumbnailsPath)) { Directory.CreateDirectory(thumbnailsPath); } if (Directory.Exists(imagesPath)) { var imageFiles = Directory.GetFiles(imagesPath) .Where(f => !Path.GetFileName(f).Equals("thumbnails", StringComparison.OrdinalIgnoreCase)) // skip folder .Where(f => !Directory.Exists(f)) // skip directories .Select(f => { var fileName = Path.GetFileName(f); var originalUrl = $"/uploads/{userId}/images/{fileName}"; var thumbnailPath = Path.Combine(thumbnailsPath, fileName); var thumbnailUrl = $"/uploads/{userId}/images/thumbnails/{fileName}"; // Generate thumbnail if missing if (!File.Exists(thumbnailPath)) { try { using var image = Image.Load(f); image.Mutate(x => x.Resize(new ResizeOptions { Mode = ResizeMode.Max, Size = new Size(300, 300) })); image.Save(thumbnailPath); // Will auto-detect format } catch (Exception ex) { Console.WriteLine($"❌ Error generating thumbnail for {fileName}: {ex.Message}"); thumbnailUrl = string.Empty; } } return new UploadedImage { OriginalUrl = originalUrl, ThumbnailUrl = thumbnailUrl }; }) .ToList(); files.Images = imageFiles; } else { files.Images = new List(); } // Load VIDEOS var videosPath = Path.Combine(basePath, "videos"); files.Videos = Directory.Exists(videosPath) ? Directory.GetFiles(videosPath) .Select(f => $"/uploads/{userId}/videos/{Path.GetFileName(f)}") .ToList() : new List(); // Load AUDIO var audioPath = Path.Combine(basePath, "audio"); files.Audio = Directory.Exists(audioPath) ? Directory.GetFiles(audioPath) .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 target directory Directory.CreateDirectory(folderPath); var filePath = Path.Combine(folderPath, file.Name); await using (var stream = new FileStream(filePath, FileMode.Create)) { await file.OpenReadStream(50 * 1024 * 1024).CopyToAsync(stream); } var relativePath = $"/uploads/{userId}/{folder}/{file.Name}"; AppendFilePathToContent(file.ContentType, relativePath); // Generate thumbnail if it's an image string? thumbnailRelativePath = null; if (file.ContentType.StartsWith("image/")) { var thumbnailFolder = Path.Combine(folderPath, "thumbnails"); Directory.CreateDirectory(thumbnailFolder); var thumbnailPath = Path.Combine(thumbnailFolder, file.Name); using var image = await Image.LoadAsync(file.OpenReadStream()); image.Mutate(x => x.Resize(new ResizeOptions { Size = new Size(300, 0), Mode = ResizeMode.Max })); await image.SaveAsync(thumbnailPath); thumbnailRelativePath = $"/uploads/{userId}/{folder}/thumbnails/{file.Name}"; } AppendFilePathToContent(file.ContentType, relativePath, thumbnailRelativePath); } } catch (Exception ex) { Console.WriteLine($"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, string? thumbnailPath = null) { if (contentType.StartsWith("image/")) { files.Images.Add(new UploadedImage { OriginalUrl = relativePath, ThumbnailUrl = thumbnailPath ?? string.Empty }); } else if (contentType.StartsWith("video/")) { files.Videos.Add(relativePath); } else if (contentType.StartsWith("audio/")) { files.Audio.Add(relativePath); } StateHasChanged(); } }