Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Added `del_completed_files` param to RemoveFromHistory api.
nzbdav-dev590c14a40303
diff --git a/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryController.cs b/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryController.csindex 15571bf3..69f4b1aa 100644--- a/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryController.cs+++ b/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryController.cs@@ -1,22 +1,40 @@using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;+using NWebDav.Server.Stores;using NzbWebDAV.Config;using NzbWebDAV.Database;+using NzbWebDAV.Database.Models;+using NzbWebDAV.WebDav;namespace NzbWebDAV.Api.SabControllers.RemoveFromHistory;public class RemoveFromHistoryController(HttpContext httpContext,DavDatabaseClient dbClient,- ConfigManager configManager+ ConfigManager configManager,+ DatabaseStore store) : SabApiController.BaseController(httpContext, configManager){public async Task<RemoveFromHistoryResponse> RemoveFromHistory(RemoveFromHistoryRequest request){- await dbClient.RemoveHistoryItemAsync(request.NzoId);+ await using var transaction = await dbClient.Ctx.Database.BeginTransactionAsync();+ var historyItem = await dbClient.GetHistoryItemAsync(request.NzoId);+ if (historyItem is null) return new RemoveFromHistoryResponse() { Status = true };+ if (request.DeleteCompletedFiles) await DeleteCompletedFiles(historyItem, request.CancellationToken);+ dbClient.Ctx.HistoryItems.Remove(historyItem);+ await dbClient.Ctx.SaveChangesAsync();+ await transaction.CommitAsync();return new RemoveFromHistoryResponse() { Status = true };}+ public async Task DeleteCompletedFiles(HistoryItem historyItem, CancellationToken cancellationToken)+ {+ var categoryPath = Path.Join(DavItem.ContentFolder.Name, historyItem.Category);+ var item = await store.GetItemAsync(categoryPath, cancellationToken);+ if (item is not IStoreCollection dir) return;+ await dir.DeleteItemAsync(historyItem.JobName, cancellationToken);+ }+protected override async Task<IActionResult> Handle(){var request = new RemoveFromHistoryRequest(httpContext);diff --git a/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryRequest.cs b/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryRequest.csindex c95117e0..28a438b2 100644--- a/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryRequest.cs+++ b/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryRequest.cs@@ -6,11 +6,23 @@ namespace NzbWebDAV.Api.SabControllers.RemoveFromHistory;public class RemoveFromHistoryRequest(){public string NzoId { get; init; }+ public bool DeleteCompletedFiles { get; init; }public CancellationToken CancellationToken { get; init; }- public RemoveFromHistoryRequest(HttpContext httpContext): this()+ public RemoveFromHistoryRequest(HttpContext httpContext) : this(){+ // Note: The official SABnzbd api has a query param named `del_files`+ // which only applies to Failed jobs and does not apply to Completed jobs.+ // However, Failed jobs in nzbdav never add anything to the webdav anyway,+ // so there is never anything to delete for Failed jobs. For this reason,+ // the `del_files` query param from SABnzbd is not needed here at all.+ //+ // Instead, a non-standard `del_completed_files` query param is added.+ // It applies to Completed jobs and does not apply to Failed jobs. It is+ // only used by the nzbdav web-ui when manually clearing History items to+ // provide users the option to delete all related files.NzoId = httpContext.GetQueryParam("value")!;+ DeleteCompletedFiles = httpContext.GetQueryParam("del_completed_files") == "1";CancellationToken = httpContext.RequestAborted;}}\ No newline at end of filediff --git a/backend/Api/SabControllers/SabApiController.cs b/backend/Api/SabControllers/SabApiController.csindex de77476a..5c667003 100644--- a/backend/Api/SabControllers/SabApiController.cs+++ b/backend/Api/SabControllers/SabApiController.cs@@ -14,6 +14,7 @@ using NzbWebDAV.Database;using NzbWebDAV.Extensions;using NzbWebDAV.Queue;using NzbWebDAV.Utils;+using NzbWebDAV.WebDav;namespace NzbWebDAV.Api.SabControllers;@@ -22,7 +23,8 @@ namespace NzbWebDAV.Api.SabControllers;public class SabApiController(DavDatabaseClient dbClient,ConfigManager configManager,- QueueManager queueManager+ QueueManager queueManager,+ DatabaseStore databaseStore) : ControllerBase{[HttpGet]@@ -81,7 +83,7 @@ public class SabApiController(return new GetQueueController(HttpContext, dbClient, queueManager, configManager);case "history" when HttpContext.GetQueryParam("name") == "delete":- return new RemoveFromHistoryController(HttpContext, dbClient, configManager);+ return new RemoveFromHistoryController(HttpContext, dbClient, configManager, databaseStore);case "history":return new GetHistoryController(HttpContext, dbClient, configManager);diff --git a/backend/Database/DavDatabaseClient.cs b/backend/Database/DavDatabaseClient.csindex 9a035450..ea0fb896 100644--- a/backend/Database/DavDatabaseClient.cs+++ b/backend/Database/DavDatabaseClient.cs@@ -115,6 +115,11 @@ public sealed class DavDatabaseClient(DavDatabaseContext ctx)}// history+ public async Task<HistoryItem?> GetHistoryItemAsync(string id)+ {+ return await Ctx.HistoryItems.FirstOrDefaultAsync(x => x.Id == Guid.Parse(id));+ }+public async Task RemoveHistoryItemAsync(string id){try