Skip to content

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

C#

Updated BlobStore interface to support annotated blob models.

nzbdav-devd2cee00e1d3b
diff --git a/backend/Database/BlobStore.cs b/backend/Database/BlobStore.cs
index b2d4e139..6253ca07 100644
--- a/backend/Database/BlobStore.cs
+++ b/backend/Database/BlobStore.cs
@@ -1,9 +1,11 @@
-using NzbWebDAV.Utils;
+using MemoryPack;
+using ZstdSharp;
namespace NzbWebDAV.Database;
public class BlobStore
{
+ private static readonly int CompressionLevel = 1;
private static readonly string ConfigPath = DavDatabaseContext.ConfigPath;
private static readonly Lock LockObj = new();
@@ -17,7 +19,7 @@ public class BlobStore
return Path.Combine(ConfigPath, "blobs", firstTwo, nextTwo, fileName);
}
- public static async Task WriteBlob(Guid id, Stream stream)
+ private static FileStream OpenBlobWrite(Guid id)
{
var blobPath = GetBlobPath(id);
var directory = Path.GetDirectoryName(blobPath);
@@ -31,11 +33,20 @@ public class BlobStore
fileStream = File.Create(blobPath);
}
- // Write data outside lock to avoid blocking other operations during I/O
- await using (fileStream)
- {
- await stream.CopyToAsync(fileStream);
- }
+ return fileStream;
+ }
+
+ public static async Task WriteBlob(Guid id, Stream stream)
+ {
+ await using var fileStream = OpenBlobWrite(id);
+ await stream.CopyToAsync(fileStream);
+ }
+
+ public static async Task WriteBlob<T>(Guid id, T blob)
+ {
+ await using var fileStream = OpenBlobWrite(id);
+ await using var compressionStream = new CompressionStream(fileStream, CompressionLevel);
+ await MemoryPackSerializer.SerializeAsync(compressionStream, blob);
}
public static Stream? ReadBlob(Guid id)
@@ -44,6 +55,15 @@ public class BlobStore
return File.Exists(blobPath) ? File.OpenRead(blobPath) : null;
}
+ public static async Task<T?> ReadBlob<T>(Guid id)
+ {
+ var stream = ReadBlob(id);
+ if (stream == null) return default;
+ await using var fileStream = stream;
+ await using var decompressionStream = new DecompressionStream(fileStream);
+ return await MemoryPackSerializer.DeserializeAsync<T>(decompressionStream);
+ }
+
public static void Delete(Guid id)
{
var blobPath = GetBlobPath(id);
diff --git a/backend/NzbWebDAV.csproj b/backend/NzbWebDAV.csproj
index 77382e4e..1760b523 100644
--- a/backend/NzbWebDAV.csproj
+++ b/backend/NzbWebDAV.csproj
@@ -20,6 +20,7 @@
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
<PackageReference Include="SharpCompress" Version="0.39.0" />
<PackageReference Include="UsenetSharp" Version="1.0.6" />
+ <PackageReference Include="ZstdSharp.Port" Version="0.8.7" />
</ItemGroup>
<ItemGroup>