Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Support bulk remove-from-history api.

nzbdav-dev56c9fbd2a631
diff --git a/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryController.cs b/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryController.cs
index 1b089a59..f9fd4a06 100644
--- a/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryController.cs
+++ b/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryController.cs
@@ -1,10 +1,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
-using Microsoft.EntityFrameworkCore;
using NzbWebDAV.Config;
using NzbWebDAV.Database;
-using NzbWebDAV.Database.Models;
-using NzbWebDAV.Extensions;
using NzbWebDAV.Websocket;
namespace NzbWebDAV.Api.SabControllers.RemoveFromHistory;
@@ -19,28 +16,16 @@ public class RemoveFromHistoryController(
public async Task<RemoveFromHistoryResponse> RemoveFromHistory(RemoveFromHistoryRequest request)
{
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.RemoveHistoryItemsAsync(request.NzoIds, request.DeleteCompletedFiles, request.CancellationToken);
await dbClient.Ctx.SaveChangesAsync(request.CancellationToken);
await transaction.CommitAsync(request.CancellationToken);
- _ = websocketManager.SendMessage(WebsocketTopic.HistoryItemRemoved, request.NzoId);
+ _ = websocketManager.SendMessage(WebsocketTopic.HistoryItemRemoved, string.Join(",", request.NzoIds));
return new RemoveFromHistoryResponse() { Status = true };
}
- public async Task DeleteCompletedFiles(HistoryItem historyItem, CancellationToken ct)
- {
- if (historyItem.DownloadDirId is null) return;
- var downloadDir = await dbClient.Ctx.Items.FirstOrDefaultAsync(x => x.Id == historyItem.DownloadDirId, ct);
- if (downloadDir is null) return;
- if (downloadDir.IsProtected()) return;
- dbClient.Ctx.Items.Remove(downloadDir);
- }
-
protected override async Task<IActionResult> Handle()
{
- var request = new RemoveFromHistoryRequest(httpContext);
+ var request = await RemoveFromHistoryRequest.New(httpContext);
return Ok(await RemoveFromHistory(request));
}
}
\ No newline at end of file
diff --git a/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryRequest.cs b/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryRequest.cs
index 28a438b2..b13cf35e 100644
--- a/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryRequest.cs
+++ b/backend/Api/SabControllers/RemoveFromHistory/RemoveFromHistoryRequest.cs
@@ -1,15 +1,18 @@
-using Microsoft.AspNetCore.Http;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+using Microsoft.AspNetCore.Http;
using NzbWebDAV.Extensions;
+using NzbWebDAV.Utils;
namespace NzbWebDAV.Api.SabControllers.RemoveFromHistory;
-public class RemoveFromHistoryRequest()
+public class RemoveFromHistoryRequest
{
- public string NzoId { get; init; }
- public bool DeleteCompletedFiles { get; init; }
- public CancellationToken CancellationToken { get; init; }
+ public List<Guid> NzoIds { get; private init; } = [];
+ public bool DeleteCompletedFiles { get; private init; }
+ public CancellationToken CancellationToken { get; private init; }
- public RemoveFromHistoryRequest(HttpContext httpContext) : this()
+ public static async Task<RemoveFromHistoryRequest> New(HttpContext httpContext)
{
// 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.
@@ -21,8 +24,39 @@ public class RemoveFromHistoryRequest()
// 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;
+ var cancellationToken = SigtermUtil.GetCancellationToken();
+ return new RemoveFromHistoryRequest()
+ {
+ NzoIds = NzoIdsFromQueryParam(httpContext)
+ .Concat(await NzoIdsFromRequestBody(httpContext, cancellationToken))
+ .ToList(),
+ DeleteCompletedFiles = httpContext.GetQueryParam("del_completed_files") == "1",
+ CancellationToken = cancellationToken
+ };
+ }
+
+ private static IEnumerable<Guid> NzoIdsFromQueryParam(HttpContext httpContext)
+ {
+ return httpContext.GetQueryParamValues("value").Select(Guid.Parse);
+ }
+
+ private static async Task<List<Guid>> NzoIdsFromRequestBody(HttpContext httpContext, CancellationToken ct)
+ {
+ try
+ {
+ await using var stream = httpContext.Request.Body;
+ var deserialized = await JsonSerializer.DeserializeAsync<RequestBody>(stream, cancellationToken: ct);
+ return deserialized?.NzoIds ?? [];
+ }
+ catch
+ {
+ return [];
+ }
+ }
+
+ private class RequestBody
+ {
+ [JsonPropertyName("nzo_ids")]
+ public List<Guid> NzoIds { get; set; } = [];
}
}
\ No newline at end of file
diff --git a/backend/Database/DavDatabaseClient.cs b/backend/Database/DavDatabaseClient.cs
index 5a1d31f0..c5b9c854 100644
--- a/backend/Database/DavDatabaseClient.cs
+++ b/backend/Database/DavDatabaseClient.cs
@@ -135,18 +135,21 @@ public sealed class DavDatabaseClient(DavDatabaseContext ctx)
return await Ctx.HistoryItems.FirstOrDefaultAsync(x => x.Id == Guid.Parse(id));
}
- public async Task RemoveHistoryItemAsync(string id)
+ public async Task RemoveHistoryItemsAsync(List<Guid> ids, bool deleteFiles, CancellationToken ct = default)
{
- try
- {
- Ctx.HistoryItems.Remove(new HistoryItem() { Id = Guid.Parse(id) });
- await Ctx.SaveChangesAsync();
- }
- catch (DbUpdateConcurrencyException e)
+ if (deleteFiles)
{
- var ignoredErrorMessage = "expected to affect 1 row(s), but actually affected 0 row(s)";
- if (!e.Message.Contains(ignoredErrorMessage)) throw;
+ await Ctx.Items
+ .Where(d => Ctx.HistoryItems
+ .Where(h => ids.Contains(h.Id) && h.DownloadDirId != null)
+ .Select(h => h.DownloadDirId!)
+ .Contains(d.Id))
+ .ExecuteDeleteAsync(ct);
}
+
+ await Ctx.HistoryItems
+ .Where(x => ids.Contains(x.Id))
+ .ExecuteDeleteAsync(ct);
}
private class FileSizeResult
diff --git a/backend/Extensions/HttpContextExtensions.cs b/backend/Extensions/HttpContextExtensions.cs
index ad222eee..15174bfc 100644
--- a/backend/Extensions/HttpContextExtensions.cs
+++ b/backend/Extensions/HttpContextExtensions.cs
@@ -9,6 +9,13 @@ public static class HttpContextExtensions
return httpContext.Request.Query[name].FirstOrDefault();
}
+ public static IEnumerable<string> GetQueryParamValues(this HttpContext httpContext, string name)
+ {
+ return httpContext.Request.Query[name]
+ .Where(x => x is not null)
+ .Select(x => x!);
+ }
+
public static string? GetRequestApiKey(this HttpContext httpContext)
{
return httpContext.Request.Headers["x-api-key"].FirstOrDefault()
diff --git a/backend/WebDav/DatabaseStoreSymlinkFile.cs b/backend/WebDav/DatabaseStoreSymlinkFile.cs
index ddb5ce65..6b9da271 100644
--- a/backend/WebDav/DatabaseStoreSymlinkFile.cs
+++ b/backend/WebDav/DatabaseStoreSymlinkFile.cs
@@ -1,11 +1,7 @@
using System.Text;
-using NWebDav.Server;
-using NWebDav.Server.Stores;
using NzbWebDAV.Config;
using NzbWebDAV.Database.Models;
using NzbWebDAV.WebDav.Base;
-using NzbWebDAV.WebDav.Requests;
-using Serilog;
namespace NzbWebDAV.WebDav;