212 lines
7.1 KiB
Plaintext
212 lines
7.1 KiB
Plaintext
@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)
|
|
{
|
|
<p>Loading content...</p>
|
|
}
|
|
else
|
|
{
|
|
<h3>Manage Uploaded Files</h3>
|
|
@if (files == null || (!files.Images.Any() && !files.Videos.Any() && !files.Audio.Any()))
|
|
{
|
|
<p>No files uploaded yet.</p>
|
|
}
|
|
else
|
|
{
|
|
<div>
|
|
<h4>Uploaded Images</h4>
|
|
<div class="row">
|
|
@foreach (var image in files.Images)
|
|
{
|
|
<div class="col-xs-12 col-sm-6 col-md-3 col-lg-2">
|
|
<div class="card">
|
|
<div class="upload-image-container" @onclick="() => CopyToClipboard(image)">
|
|
<img class="img-fluid square-thumbnail" src="@image" alt="Uploaded Image" />
|
|
</div>
|
|
<button class="btn btn-danger" @onclick="@(() => DeleteFile(image, "Images"))">Delete</button>
|
|
</div>
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<h4>Uploaded Videos</h4>
|
|
<div class="row">
|
|
@foreach (var video in files.Videos)
|
|
{
|
|
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
|
|
<div class="card">
|
|
<video controls style="width: 100%; height: auto">
|
|
<source src="@video" type="video/mp4">
|
|
</video>
|
|
<button class="btn btn-danger" @onclick="@(() => DeleteFile(video, "Videos"))">Delete</button>
|
|
</div>
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<h4>Uploaded Audio</h4>
|
|
<div class="row">
|
|
@foreach (var audio in files.Audio)
|
|
{
|
|
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
|
|
<div class="card">
|
|
<audio controls>
|
|
<source src="@audio" type="audio/mpeg">
|
|
</audio>
|
|
<button class="btn btn-danger" @onclick="@(() => DeleteFile(audio, "Audio"))">Delete</button>
|
|
</div>
|
|
</div>
|
|
}
|
|
</div>
|
|
</div>
|
|
<InputFile class="btn btn-default" type="file" multiple OnChange=HandleFileUpload accept=".mp3,.mp4,.jpg,.png" />
|
|
}
|
|
}
|
|
|
|
|
|
@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<string>();
|
|
|
|
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<string>();
|
|
|
|
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<string>();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|