Skip to content

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

C#

[Refactor] Move LongRange object to Models namespace.

nzbdav-dev01283b16cff5
diff --git a/backend/Models/LongRange.cs b/backend/Models/LongRange.cs
new file mode 100644
index 00000000..3bd6ad96
--- /dev/null
+++ b/backend/Models/LongRange.cs
@@ -0,0 +1,18 @@
+namespace NzbWebDAV.Models;
+
+public record LongRange(long StartInclusive, long EndExclusive)
+{
+ public long Count => EndExclusive - StartInclusive;
+
+ public bool Contains(long value) =>
+ value >= StartInclusive && value < EndExclusive;
+
+ public bool Contains(LongRange range) =>
+ range.StartInclusive >= StartInclusive && range.EndExclusive <= EndExclusive;
+
+ public bool IsContainedWithin(LongRange range) =>
+ range.Contains(this);
+
+ public static LongRange FromStartAndSize(long startInclusive, long size) =>
+ new(startInclusive, startInclusive + size);
+}
\ No newline at end of file
diff --git a/backend/Streams/NzbFileStream.cs b/backend/Streams/NzbFileStream.cs
index ba17286f..d7921ec3 100644
--- a/backend/Streams/NzbFileStream.cs
+++ b/backend/Streams/NzbFileStream.cs
@@ -1,5 +1,6 @@
using NzbWebDAV.Clients;
using NzbWebDAV.Extensions;
+using NzbWebDAV.Models;
using NzbWebDAV.Utils;
namespace NzbWebDAV.Streams;
@@ -71,12 +72,12 @@ public class NzbFileStream(
{
return await InterpolationSearch.Find(
byteOffset,
- new InterpolationSearch.Range(0, fileSegmentIds.Length),
- new InterpolationSearch.Range(0, fileSize),
+ new LongRange(0, fileSegmentIds.Length),
+ new LongRange(0, fileSize),
async (guess) =>
{
var header = await client.GetSegmentYencHeaderAsync(fileSegmentIds[guess], ct);
- return new InterpolationSearch.Range(header.PartOffset, header.PartOffset + header.PartSize);
+ return new LongRange(header.PartOffset, header.PartOffset + header.PartSize);
},
ct
);
diff --git a/backend/Utils/InterpolationSearch.cs b/backend/Utils/InterpolationSearch.cs
index 99f70024..3cef3a30 100644
--- a/backend/Utils/InterpolationSearch.cs
+++ b/backend/Utils/InterpolationSearch.cs
@@ -1,4 +1,5 @@
using NzbWebDAV.Exceptions;
+using NzbWebDAV.Models;
namespace NzbWebDAV.Utils;
@@ -7,9 +8,9 @@ public static class InterpolationSearch
public static async Task<Result> Find
(
long searchByte,
- Range indexRangeToSearch,
- Range byteRangeToSearch,
- Func<int, Task<Range>> getByteRangeOfGuessedIndex,
+ LongRange indexRangeToSearch,
+ LongRange byteRangeToSearch,
+ Func<int, Task<LongRange>> getByteRangeOfGuessedIndex,
CancellationToken cancellationToken
)
{
@@ -52,19 +53,5 @@ public static class InterpolationSearch
}
}
- public record Range(long StartInclusive, long EndExclusive)
- {
- public long Count => EndExclusive - StartInclusive;
-
- public bool Contains(long value) =>
- value >= StartInclusive && value < EndExclusive;
-
- public bool Contains(Range range) =>
- range.StartInclusive >= StartInclusive && range.EndExclusive <= EndExclusive;
-
- public bool IsContainedWithin(Range range) =>
- range.Contains(this);
- }
-
- public record Result(int FoundIndex, Range FoundByteRange);
+ public record Result(int FoundIndex, LongRange FoundByteRange);
}
\ No newline at end of file