Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Added download support to `/view` route.
nzbdav-dev0499cdc1e5aa
diff --git a/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs b/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.csindex a5c125fd..6a19d4c0 100644--- a/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs+++ b/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs@@ -35,7 +35,7 @@ public class GetWebdavItemController(DatabaseStore store, ConfigManager configMa// set the content-type and content-disposition headersResponse.Headers["Content-Type"] = GetContentType(item.Name);- Response.Headers["Content-Disposition"] = GetContentDisposition(item.Name);+ Response.Headers["Content-Disposition"] = GetContentDisposition(item.Name, request.ShouldDownload);// disable compression to keep Content-Length intact for clients that need seekingResponse.Headers["Content-Encoding"] = "identity";@@ -71,8 +71,8 @@ public class GetWebdavItemController(DatabaseStore store, ConfigManager configMa{HttpContext.Items["configManager"] = configManager;var request = new GetWebdavItemRequest(HttpContext);- await using var response = await GetWebdavItem(request).ConfigureAwait(false);- await response.CopyToAsync(Response.Body, bufferSize: 1024, HttpContext.RequestAborted).ConfigureAwait(false);+ await using var response = await GetWebdavItem(request);+ await response.CopyToAsync(Response.Body, bufferSize: 1024, HttpContext.RequestAborted);}catch (UnauthorizedAccessException){@@ -107,7 +107,7 @@ public class GetWebdavItemController(DatabaseStore store, ConfigManager configMa: "application/octet-stream";}- private static string GetContentDisposition(string filename)+ private static string GetContentDisposition(string filename, bool shouldDownload){// Remove control characters (header safety)filename = new string(filename.Where(c => !char.IsControl(c)).ToArray());@@ -119,7 +119,9 @@ public class GetWebdavItemController(DatabaseStore store, ConfigManager configMa// RFC 5987 UTF-8 filenamevar utf8 = Uri.EscapeDataString(filename);- return $"inline; filename=\"{ascii}\"; filename*=UTF-8''{utf8}";+ // return+ var type = shouldDownload ? "attachment" : "inline";+ return $"{type}; filename=\"{ascii}\"; filename*=UTF-8''{utf8}";}private async Task<Stream> GetPar2PreviewStream(IStoreItem item)diff --git a/backend/Api/Controllers/GetWebdavItem/GetWebdavItemRequest.cs b/backend/Api/Controllers/GetWebdavItem/GetWebdavItemRequest.csindex 1c1699af..778b7527 100644--- a/backend/Api/Controllers/GetWebdavItem/GetWebdavItemRequest.cs+++ b/backend/Api/Controllers/GetWebdavItem/GetWebdavItemRequest.cs@@ -2,6 +2,7 @@using System.Text;using Microsoft.AspNetCore.Http;using NzbWebDAV.Config;+using NzbWebDAV.Extensions;using NzbWebDAV.Utils;namespace NzbWebDAV.Api.Controllers.GetWebdavItem;@@ -11,6 +12,7 @@ public class GetWebdavItemRequestpublic string Item { get; init; }public long? RangeStart { get; init; }public long? RangeEnd { get; init; }+ public bool ShouldDownload { get; init; }public GetWebdavItemRequest(HttpContext context){@@ -21,6 +23,9 @@ public class GetWebdavItemRequestif (path.StartsWith("/")) path = path[1..];Item = path;+ // determine whether to download+ ShouldDownload = context.GetQueryParam("download")?.ToLower() == "true";+// authenticate the downloadKeyvar downloadKey = context.Request.Query["downloadKey"];var configManager = (ConfigManager)context.Items["configManager"]!;