Skip to content

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

C#

Added backend ability to preview par2 files.

nzbdav-dev37eae8fc3fb6
diff --git a/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs b/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs
index 4a1c71d3..9f63c8e0 100644
--- a/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs
+++ b/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs
@@ -1,15 +1,19 @@
-using Microsoft.AspNetCore.Http;
+using System.IO.Pipelines;
+using System.Text;
+using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NWebDav.Server.Stores;
+using NzbWebDAV.Config;
using NzbWebDAV.Extensions;
+using NzbWebDAV.Par2Recovery;
using NzbWebDAV.WebDav;
namespace NzbWebDAV.Api.Controllers.GetWebdavItem;
[ApiController]
[Route("view/{*path}")]
-public class ListWebdavDirectoryController(DatabaseStore store) : ControllerBase
+public class ListWebdavDirectoryController(DatabaseStore store, ConfigManager configManager) : ControllerBase
{
private static readonly FileExtensionContentTypeProvider MimeTypeProvider = new();
@@ -19,6 +23,10 @@ public class ListWebdavDirectoryController(DatabaseStore store) : ControllerBase
if (item is null) throw new BadHttpRequestException("The file does not exist.");
if (item is IStoreCollection) throw new BadHttpRequestException("The file does not exist.");
+ // handle par2 preview
+ if (Path.GetExtension(item.Name).ToLower() == ".par2" && configManager.IsPreviewPar2FilesEnabled())
+ return await GetPar2PreviewStream(item);
+
// get the file stream and set the file-size in header
var stream = await item.GetReadableStreamAsync(HttpContext.RequestAborted);
var fileSize = stream.Length;
@@ -74,4 +82,12 @@ public class ListWebdavDirectoryController(DatabaseStore store) : ControllerBase
: MimeTypeProvider.TryGetContentType(Path.GetFileName(item), out var mimeType) ? mimeType
: "application/octet-stream";
}
+
+ private async Task<Stream> GetPar2PreviewStream(IStoreItem item)
+ {
+ Response.Headers.ContentType = "text/plain";
+ await using var stream = await item.GetReadableStreamAsync(HttpContext.RequestAborted);
+ var fileDescriptors = await Par2.ReadFileDescriptions(stream, HttpContext.RequestAborted).GetAllAsync();
+ return new MemoryStream(Encoding.UTF8.GetBytes(fileDescriptors.ToIndentedJson()));
+ }
}
\ No newline at end of file
diff --git a/backend/Config/ConfigManager.cs b/backend/Config/ConfigManager.cs
index fb9fe874..60193097 100644
--- a/backend/Config/ConfigManager.cs
+++ b/backend/Config/ConfigManager.cs
@@ -142,6 +142,13 @@ public class ConfigManager
return (configValue != null ? bool.Parse(configValue) : defaultValue);
}
+ public bool IsPreviewPar2FilesEnabled()
+ {
+ var defaultValue = false;
+ var configValue = StringUtil.EmptyToNull(GetConfigValue("webdav.preview-par2-files"));
+ return (configValue != null ? bool.Parse(configValue) : defaultValue);
+ }
+
public class ConfigEventArgs : EventArgs
{
public Dictionary<string, string> ChangedConfig { get; set; } = new();