Skip to content

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

C#

fix: ensure `audio/flac` content-type mapping for flac files.

nzbdav-dev5253fe3f03cb
diff --git a/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs b/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs
index 6a19d4c0..4364ea90 100644
--- a/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs
+++ b/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs
@@ -1,11 +1,11 @@
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.Utils;
using NzbWebDAV.WebDav;
namespace NzbWebDAV.Api.Controllers.GetWebdavItem;
@@ -14,8 +14,6 @@ namespace NzbWebDAV.Api.Controllers.GetWebdavItem;
[Route("view/{*path}")]
public class GetWebdavItemController(DatabaseStore store, ConfigManager configManager) : ControllerBase
{
- private static readonly FileExtensionContentTypeProvider MimeTypeProvider = new();
-
private async Task<Stream> GetWebdavItem(GetWebdavItemRequest request)
{
var item = await store.GetItemAsync(request.Item, HttpContext.RequestAborted).ConfigureAwait(false);
@@ -103,8 +101,7 @@ public class GetWebdavItemController(DatabaseStore store, ConfigManager configMa
return extension == ".mkv" ? "video/webm"
: extension == ".rclonelink" ? "text/plain"
: extension == ".nfo" ? "text/plain"
- : MimeTypeProvider.TryGetContentType(Path.GetFileName(item), out var mimeType) ? mimeType
- : "application/octet-stream";
+ : ContentTypeUtil.GetContentType(Path.GetFileName(item));
}
private static string GetContentDisposition(string filename, bool shouldDownload)
diff --git a/backend/Api/SabControllers/AddFile/AddFileRequest.cs b/backend/Api/SabControllers/AddFile/AddFileRequest.cs
index 993537a6..53df8ed0 100644
--- a/backend/Api/SabControllers/AddFile/AddFileRequest.cs
+++ b/backend/Api/SabControllers/AddFile/AddFileRequest.cs
@@ -8,7 +8,7 @@ namespace NzbWebDAV.Api.SabControllers.AddFile;
public class AddFileRequest()
{
public string FileName { get; init; }
- public string? MimeType { get; init; }
+ public string? ContentType { get; init; }
public Stream NzbFileStream { get; init; }
public string Category { get; init; }
public QueueItem.PriorityOption Priority { get; init; }
@@ -26,7 +26,7 @@ public class AddFileRequest()
return new AddFileRequest()
{
FileName = file.FileName,
- MimeType = file.ContentType,
+ ContentType = file.ContentType,
NzbFileStream = file.OpenReadStream(),
Category = context.GetQueryParam("cat") ?? configManager.GetManualUploadCategory(),
Priority = MapPriorityOption(context.GetQueryParam("priority")),
diff --git a/backend/Api/SabControllers/AddUrl/AddUrlRequest.cs b/backend/Api/SabControllers/AddUrl/AddUrlRequest.cs
index a274e697..230b9c91 100644
--- a/backend/Api/SabControllers/AddUrl/AddUrlRequest.cs
+++ b/backend/Api/SabControllers/AddUrl/AddUrlRequest.cs
@@ -20,7 +20,7 @@ public class AddUrlRequest() : AddFileRequest
return new AddUrlRequest()
{
FileName = nzbFile.FileName,
- MimeType = nzbFile.ContentType,
+ ContentType = nzbFile.ContentType,
NzbFileStream = nzbFile.FileStream,
Category = context.GetQueryParam("cat") ?? configManager.GetManualUploadCategory(),
Priority = MapPriorityOption(context.GetQueryParam("priority")),
diff --git a/backend/Utils/ContentTypeUtil.cs b/backend/Utils/ContentTypeUtil.cs
new file mode 100644
index 00000000..bdebcfe0
--- /dev/null
+++ b/backend/Utils/ContentTypeUtil.cs
@@ -0,0 +1,22 @@
+using Microsoft.AspNetCore.StaticFiles;
+
+namespace NzbWebDAV.Utils;
+
+public static class ContentTypeUtil
+{
+ private static readonly FileExtensionContentTypeProvider ContentTypeProvider;
+
+ static ContentTypeUtil()
+ {
+ // ReSharper disable once UseObjectOrCollectionInitializer
+ ContentTypeProvider = new FileExtensionContentTypeProvider();
+ ContentTypeProvider.Mappings[".flac"] = "audio/flac";
+ }
+
+ public static string GetContentType(string fileName)
+ {
+ return !ContentTypeProvider.TryGetContentType(Path.GetFileName(fileName), out var contentType)
+ ? "application/octet-stream"
+ : contentType;
+ }
+}
\ No newline at end of file
diff --git a/backend/WebDav/Base/BaseStoreItemPropertyManager.cs b/backend/WebDav/Base/BaseStoreItemPropertyManager.cs
index 2bcb518a..28982af4 100644
--- a/backend/WebDav/Base/BaseStoreItemPropertyManager.cs
+++ b/backend/WebDav/Base/BaseStoreItemPropertyManager.cs
@@ -1,13 +1,12 @@
using System.Xml.Linq;
-using Microsoft.AspNetCore.StaticFiles;
using NWebDav.Server;
using NWebDav.Server.Props;
+using NzbWebDAV.Utils;
namespace NzbWebDAV.WebDav.Base;
public class BaseStoreItemPropertyManager() : PropertyManager<BaseStoreItem>(DavProperties)
{
- private static readonly FileExtensionContentTypeProvider MimeTypeProvider = new();
private static readonly XElement DavResourceType = new(WebDavNamespaces.DavNs + "item");
private static readonly DavProperty<BaseStoreItem>[] DavProperties =
@@ -22,9 +21,7 @@ public class BaseStoreItemPropertyManager() : PropertyManager<BaseStoreItem>(Dav
},
new DavGetContentType<BaseStoreItem>
{
- Getter = item => !MimeTypeProvider.TryGetContentType(item.Name, out var mimeType)
- ? "application/octet-stream"
- : mimeType
+ Getter = item => ContentTypeUtil.GetContentType(item.Name)
},
new DavGetLastModified<BaseStoreItem>
{
diff --git a/backend/WebDav/DatabaseStoreWatchFolder.cs b/backend/WebDav/DatabaseStoreWatchFolder.cs
index e6fac088..c1568466 100644
--- a/backend/WebDav/DatabaseStoreWatchFolder.cs
+++ b/backend/WebDav/DatabaseStoreWatchFolder.cs
@@ -55,7 +55,7 @@ public class DatabaseStoreWatchFolder(
var addFileRequest = new AddFileRequest()
{
FileName = request.Name,
- MimeType = "application/x-nzb",
+ ContentType = "application/x-nzb",
Category = configManager.GetManualUploadCategory(),
Priority = QueueItem.PriorityOption.Normal,
PostProcessing = QueueItem.PostProcessingOption.RepairUnpackDelete,