From 1738d262de6e64814e81c32fe558b7d808bfea36 Mon Sep 17 00:00:00 2001 From: Daniel Harvey Date: Mon, 18 May 2026 15:42:58 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20W3-T3=20review:=20attach=20JWT=20in=20Del?= =?UTF-8?q?eteTrackDialog,=20int=E2=86=92long=20TrackId,=20log=20RemoveRes?= =?UTF-8?q?ourceAsync=20failure,=20drop=20redundant=20{Reason}=20log=20par?= =?UTF-8?q?am?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DeepDrftCms/Components/DeleteTrackDialog.razor | 10 ++++++---- .../FileDatabase/Services/FileDatabase.cs | 13 ++++++++----- DeepDrftContent/Startup.cs | 18 +++++++++++++----- DeepDrftWeb/Controllers/CmsDeleteController.cs | 8 ++++---- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/DeepDrftCms/Components/DeleteTrackDialog.razor b/DeepDrftCms/Components/DeleteTrackDialog.razor index 09d272d..367ba81 100644 --- a/DeepDrftCms/Components/DeleteTrackDialog.razor +++ b/DeepDrftCms/Components/DeleteTrackDialog.razor @@ -1,6 +1,8 @@ @using System.Net.Http +@using System.Net.Http.Headers @using Microsoft.AspNetCore.Components @inject IHttpClientFactory HttpClientFactory +@inject AuthBlocksWeb.Services.ITokenService TokenService @@ -34,7 +36,7 @@ @code { [CascadingParameter] private IMudDialogInstance MudDialog { get; set; } = default!; - [Parameter] public int TrackId { get; set; } + [Parameter] public long TrackId { get; set; } [Parameter] public string TrackName { get; set; } = ""; [Parameter] public EventCallback OnDeleted { get; set; } @@ -48,10 +50,10 @@ try { - // "DeepDrft.API" is the named client pointing at DeepDrftWeb's own host. - // The Admin role gate on the endpoint is enforced server-side by AuthBlocks; - // the registered HTTP client/auth handler stack attaches the JWT. var client = HttpClientFactory.CreateClient("DeepDrft.API"); + var token = await TokenService.GetAccessTokenAsync(); + if (!string.IsNullOrEmpty(token)) + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var response = await client.DeleteAsync($"api/cms/track/{TrackId}"); if (response.IsSuccessStatusCode) diff --git a/DeepDrftContent.Services/FileDatabase/Services/FileDatabase.cs b/DeepDrftContent.Services/FileDatabase/Services/FileDatabase.cs index d50ae1b..a3b2893 100644 --- a/DeepDrftContent.Services/FileDatabase/Services/FileDatabase.cs +++ b/DeepDrftContent.Services/FileDatabase/Services/FileDatabase.cs @@ -1,5 +1,6 @@ using DeepDrftContent.Services.FileDatabase.Models; using DeepDrftContent.Services.FileDatabase.Utils; +using Microsoft.Extensions.Logging; namespace DeepDrftContent.Services.FileDatabase.Services; @@ -12,19 +13,20 @@ public class FileDatabase : DirectoryIndexDirectory, IDisposable private readonly StructuralMap _vaults; private readonly IndexWatcher _indexWatcher; private readonly IndexFactoryService _indexFactory; + private readonly ILogger _logger; private bool _disposed; /// /// Factory method to create a FileDatabase instance /// - public static async Task FromAsync(string rootPath) + public static async Task FromAsync(string rootPath, ILogger? logger = null) { var factoryService = new IndexFactoryService(); var rootIndex = await factoryService.LoadOrCreateDirectoryIndexAsync(rootPath); if (rootIndex != null) { - var db = new FileDatabase(rootPath, rootIndex, factoryService); + var db = new FileDatabase(rootPath, rootIndex, factoryService, logger); await db.InitVaultsAsync(); return db; } @@ -32,11 +34,12 @@ public class FileDatabase : DirectoryIndexDirectory, IDisposable return null; } - private FileDatabase(string rootPath, IDirectoryIndex index, IndexFactoryService indexFactory) : base(rootPath, index) + private FileDatabase(string rootPath, IDirectoryIndex index, IndexFactoryService indexFactory, ILogger? logger = null) : base(rootPath, index) { _vaults = new StructuralMap(); _indexWatcher = new IndexWatcher(); _indexFactory = indexFactory; + _logger = logger ?? Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance; } /// @@ -192,9 +195,9 @@ public class FileDatabase : DirectoryIndexDirectory, IDisposable return await directoryVault.RemoveEntryAsync(entryId); } - catch + catch (Exception ex) { - // Swallow exceptions and return null, matching the FileDatabase error contract. + _logger.LogError(ex, "RemoveResourceAsync failed for vault {VaultName} key {Key}", vaultId, entryId); return null; } } diff --git a/DeepDrftContent/Startup.cs b/DeepDrftContent/Startup.cs index 8c0b601..bf462d8 100644 --- a/DeepDrftContent/Startup.cs +++ b/DeepDrftContent/Startup.cs @@ -5,12 +5,13 @@ using DeepDrftContent.Services.FileDatabase.Models; using DeepDrftContent.Services.FileDatabase.Services; using DeepDrftContent.Services.Processors; using DeepDrftContent.Models; +using Microsoft.Extensions.Logging; namespace DeepDrftContent { public static class Startup { - public static async Task ConfigureDomainServices(WebApplicationBuilder builder) + public static Task ConfigureDomainServices(WebApplicationBuilder builder) { // Audio services builder.Services.AddSingleton(); @@ -22,10 +23,17 @@ namespace DeepDrftContent var fileDatabaseSettings = builder.Configuration.GetSection(nameof(FileDatabaseSettings)).Get(); if (fileDatabaseSettings is null) { throw new Exception("File database settings are not configured"); } - var fileDatabase = await FileDatabase.FromAsync(fileDatabaseSettings.VaultPath); - if (fileDatabase is null) { throw new Exception("Unable to initialize file database"); } - builder.Services.AddSingleton(fileDatabase); - await InitializeTrackVault(fileDatabase); + var vaultPath = fileDatabaseSettings.VaultPath; + builder.Services.AddSingleton(sp => + { + var logger = sp.GetRequiredService>(); + var db = FileDatabase.FromAsync(vaultPath, logger).GetAwaiter().GetResult(); + if (db is null) throw new Exception("Unable to initialize file database"); + InitializeTrackVault(db).GetAwaiter().GetResult(); + return db; + }); + + return Task.CompletedTask; } private static async Task InitializeTrackVault(FileDatabase fileDatabase) diff --git a/DeepDrftWeb/Controllers/CmsDeleteController.cs b/DeepDrftWeb/Controllers/CmsDeleteController.cs index 2d3eabb..943c765 100644 --- a/DeepDrftWeb/Controllers/CmsDeleteController.cs +++ b/DeepDrftWeb/Controllers/CmsDeleteController.cs @@ -68,16 +68,16 @@ public class CmsDeleteController : ControllerBase if (!response.IsSuccessStatusCode) { _logger.LogWarning( - "Vault delete failed after SQL delete. {TrackId} {EntryKey} {Reason} {StatusCode}", - id, entryKey, "vault delete failed after SQL delete", (int)response.StatusCode); + "Vault delete failed after SQL delete. {TrackId} {EntryKey} {StatusCode}", + id, entryKey, (int)response.StatusCode); } } catch (Exception ex) { _logger.LogWarning( ex, - "Vault delete threw after SQL delete. {TrackId} {EntryKey} {Reason}", - id, entryKey, "vault delete failed after SQL delete"); + "Vault delete threw after SQL delete. {TrackId} {EntryKey}", + id, entryKey); } return Ok();