init
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
@using Microsoft.AspNetCore.Components.Forms;
|
||||
@using OpenArchival.Blazor.Config
|
||||
@using OpenArchival.Blazor.AdminPages.Shared;
|
||||
|
||||
@using OpenArchival.DataAccess.FileAccessManager;
|
||||
@page "/admin/blogedit"
|
||||
@page "/admin/blogedit/{Id:int}"
|
||||
@layout AdminControlPanelLayout;
|
||||
@@ -28,7 +28,7 @@
|
||||
<ActivatorContent>
|
||||
<MudButton Variant="Variant.Filled"
|
||||
Color="Color.Primary">
|
||||
Upload Main Photo
|
||||
Upload Main Photo
|
||||
</MudButton>
|
||||
</ActivatorContent>
|
||||
<SelectedTemplate>
|
||||
@@ -82,6 +82,7 @@
|
||||
@inject IDbContextFactory<ApplicationDbContext> ContextFactory;
|
||||
@inject ISnackbar Snackbar;
|
||||
@inject NavigationManager NavigationManager;
|
||||
@inject IFileAccessManager FileAccessManager;
|
||||
|
||||
@*OnKeyDown="@(ev => HandleChipContainerEnter<string>(ev, _tagsChipContainer, _tagsInputValue, () => _tagsInputValue = string.Empty))"*@
|
||||
@code {
|
||||
@@ -187,32 +188,22 @@
|
||||
|
||||
private async Task OnPublishBlogPost(Microsoft.AspNetCore.Components.Web.MouseEventArgs args)
|
||||
{
|
||||
// ... (content and title checks) ...
|
||||
|
||||
await using var context = await ContextFactory.CreateDbContextAsync();
|
||||
|
||||
bool isCreatingNewPost = BlogPostModel.Id == 0;
|
||||
FilePathListing? newPhoto = null; // Will hold the new photo entity, if one is uploaded
|
||||
FilePathListing? oldPhotoToDelete = null; // Will hold the old photo entity for deletion
|
||||
|
||||
// --- 1. Handle New Photo Upload ---
|
||||
// Handle New Photo Upload
|
||||
if (_mainPhotoFile is not null)
|
||||
{
|
||||
// A new file was uploaded.
|
||||
try
|
||||
{
|
||||
var diskFileName = $"{Guid.NewGuid()}{Path.GetExtension(_mainPhotoFile.Name)}";
|
||||
var destinationPath = Path.Combine(FileOptions.Value.UploadFolderPath, diskFileName);
|
||||
newPhoto = await FileAccessManager.UploadFileAsync(_mainPhotoFile);
|
||||
|
||||
await using var browserUploadStream = _mainPhotoFile.OpenReadStream(maxAllowedSize: FileOptions.Value.MaxUploadSizeBytes);
|
||||
await using var outFileStream = new FileStream(destinationPath, FileMode.Create);
|
||||
await browserUploadStream.CopyToAsync(outFileStream);
|
||||
|
||||
// Create the new entity
|
||||
newPhoto = new FilePathListing() { Path = destinationPath, OriginalName = Path.GetFileName(_mainPhotoFile.Name) };
|
||||
|
||||
// Add it to the context so it's tracked
|
||||
context.ArtifactFilePaths.Add(newPhoto);
|
||||
// Track the new file on this dbcontext
|
||||
context.Attach(newPhoto);
|
||||
|
||||
// We update the model's reference just in case
|
||||
BlogPostModel.MainPhoto = newPhoto;
|
||||
@@ -232,7 +223,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
// --- 2. Database Save Logic ---
|
||||
// Database Save Logic
|
||||
BlogPostModel.ModifiedTime = DateTime.UtcNow;
|
||||
BlogPostModel.ArtifactGroupings = await _artifactGroupingTable.SelectedItems();
|
||||
|
||||
@@ -310,14 +301,7 @@
|
||||
// Now that the DB save is successful, delete the old file
|
||||
try
|
||||
{
|
||||
if (File.Exists(oldPhotoToDelete.Path))
|
||||
{
|
||||
File.Delete(oldPhotoToDelete.Path);
|
||||
}
|
||||
|
||||
// And remove its record (which is tracked) from the database
|
||||
context.ArtifactFilePaths.Remove(oldPhotoToDelete);
|
||||
await context.SaveChangesAsync();
|
||||
await FileAccessManager.DeleteFileAsync(oldPhotoToDelete.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -327,7 +311,7 @@
|
||||
|
||||
Snackbar.Add("Changes saved!", Severity.Success);
|
||||
|
||||
// --- 7. Reset Page ---
|
||||
// --- Reset Page ---
|
||||
BlogPostModel = new();
|
||||
_mainPhotoFile = null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user