Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Added static README file to webdav root.
nzbdav-dev5be3c7ea53de
diff --git a/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs b/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.csindex 575d84b4..cdb5a515 100644--- a/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs+++ b/backend/Api/Controllers/GetWebdavItem/GetWebdavItemController.cs@@ -76,6 +76,7 @@ public class ListWebdavDirectoryController(DatabaseStore store, ConfigManager coprivate static string GetContentType(string item){+ if (item == "README") return "text/plain";var extension = Path.GetExtension(item).ToLower();return extension == ".mkv" ? "video/webm": extension == ".rclonelink" ? "text/plain"diff --git a/backend/NzbWebDAV.csproj b/backend/NzbWebDAV.csprojindex 6503d0b5..7a4a6d62 100644--- a/backend/NzbWebDAV.csproj+++ b/backend/NzbWebDAV.csproj@@ -24,6 +24,7 @@<ItemGroup><EmbeddedResource Include="Api\SabControllers\GetConfig\config_template.json" />+ <EmbeddedResource Include="WebDav\StaticFiles\root\README.md" /></ItemGroup></Project>diff --git a/backend/Utils/EmbeddedResourceUtil.cs b/backend/Utils/EmbeddedResourceUtil.csindex f7a3e1cd..baaa5edf 100644--- a/backend/Utils/EmbeddedResourceUtil.cs+++ b/backend/Utils/EmbeddedResourceUtil.cs@@ -19,6 +19,12 @@ public static class EmbeddedResourceUtilreturn reader.ReadToEnd();}+ public static long GetLength(string resourcePath)+ {+ using var stream = GetStream(resourcePath);+ return stream.Length;+ }+public static async Task<string> ReadAllTextAsync(string resourcePath){await using var stream = GetStream(resourcePath);diff --git a/backend/WebDav/DatabaseStoreCollection.cs b/backend/WebDav/DatabaseStoreCollection.csindex 7a50ce09..c40ef07b 100644--- a/backend/WebDav/DatabaseStoreCollection.cs+++ b/backend/WebDav/DatabaseStoreCollection.cs@@ -26,19 +26,32 @@ public class DatabaseStoreCollection(public override string Name => davDirectory.Name;public override string UniqueKey => davDirectory.Id.ToString();public override DateTime CreatedAt => davDirectory.CreatedAt;+ private static readonly StaticEmbeddedFile Readme = new("StaticFiles.root.README.md", "README");protected override async Task<IStoreItem?> GetItemAsync(GetItemRequest request){- var child = await dbClient.GetDirectoryChildAsync(davDirectory.Id, request.Name, request.CancellationToken).ConfigureAwait(false);+ if (davDirectory.Id == DavItem.Root.Id && request.Name == Readme.Name) return Readme;+ var child = await dbClient.GetDirectoryChildAsync(davDirectory.Id, request.Name, request.CancellationToken)+ .ConfigureAwait(false);if (child is null) return null;return GetItem(child);}protected override async Task<IStoreItem[]> GetAllItemsAsync(CancellationToken cancellationToken){- return (await dbClient.GetDirectoryChildrenAsync(davDirectory.Id, cancellationToken).ConfigureAwait(false))- .Select(GetItem)- .ToArray();+ // read DavItems from the database+ var children = await dbClient+ .GetDirectoryChildrenAsync(davDirectory.Id, cancellationToken)+ .ConfigureAwait(false);++ // map DavItems to IStoreItems+ var result = children.Select(GetItem);++ // include the readme file+ if (davDirectory.Id == DavItem.Root.Id)+ result = result.Append(Readme);++ return result.ToArray();}protected override bool SupportsFastMove(SupportsFastMoveRequest request)@@ -57,7 +70,8 @@ public class DatabaseStoreCollection(return DavStatusCode.Forbidden;// Get the item being deleted- var davItem = await dbClient.GetDirectoryChildAsync(davDirectory.Id, request.Name, request.CancellationToken).ConfigureAwait(false);+ var davItem = await dbClient.GetDirectoryChildAsync(davDirectory.Id, request.Name, request.CancellationToken)+ .ConfigureAwait(false);if (davItem is null) return DavStatusCode.NotFound;// If the item is a file, simply delete it and we're done.diff --git a/backend/WebDav/DatabaseStoreSymlinkCollection.cs b/backend/WebDav/DatabaseStoreSymlinkCollection.csindex b4bfb30d..720a6e4d 100644--- a/backend/WebDav/DatabaseStoreSymlinkCollection.cs+++ b/backend/WebDav/DatabaseStoreSymlinkCollection.cs@@ -27,7 +27,8 @@ public class DatabaseStoreSymlinkCollection({if (DeletedFiles.IsDeleted(request.Name)) return null;var name = Regex.Replace(request.Name, @"\.rclonelink$", "");- var child = await dbClient.GetDirectoryChildAsync(TargetId, name, request.CancellationToken).ConfigureAwait(false);+ var child = await dbClient.GetDirectoryChildAsync(TargetId, name, request.CancellationToken)+ .ConfigureAwait(false);if (child is null) return null;return GetItem(child);}@@ -38,9 +39,11 @@ public class DatabaseStoreSymlinkCollection(// then we only want to show children that correspond to Completed History items.var isCategoryFolder = davDirectory.ParentId == DavItem.ContentFolder.Id;var children = isCategoryFolder- ? await dbClient.GetCompletedSymlinkCategoryChildren(davDirectory.Name, cancellationToken).ConfigureAwait(false)+ ? await dbClient.GetCompletedSymlinkCategoryChildren(davDirectory.Name, cancellationToken)+ .ConfigureAwait(false): await dbClient.GetDirectoryChildrenAsync(TargetId, cancellationToken).ConfigureAwait(false);+ // map DavItems to IStoreItemsreturn children.Select(GetItem).Where(x => !DeletedFiles.IsDeleted(x.Name)) // must appear after Select(GetItem) for correct Name.diff --git a/backend/WebDav/StaticEmbeddedFile.cs b/backend/WebDav/StaticEmbeddedFile.csnew file mode 100644index 00000000..894208fa--- /dev/null+++ b/backend/WebDav/StaticEmbeddedFile.cs@@ -0,0 +1,17 @@+using NzbWebDAV.Utils;+using NzbWebDAV.WebDav.Base;++namespace NzbWebDAV.WebDav;++public class StaticEmbeddedFile(string resourcePath, string name) : BaseStoreReadonlyItem+{+ public override string Name => name;+ public override string UniqueKey => resourcePath;+ public override long FileSize { get; } = EmbeddedResourceUtil.GetLength(resourcePath);+ public override DateTime CreatedAt { get; } = DateTime.Now;++ public override Task<Stream> GetReadableStreamAsync(CancellationToken cancellationToken)+ {+ return Task.FromResult(EmbeddedResourceUtil.GetStream(resourcePath));+ }+}\ No newline at end of filediff --git a/backend/WebDav/StaticFiles/root/README.md b/backend/WebDav/StaticFiles/root/README.mdnew file mode 100644index 00000000..da65441b--- /dev/null+++ b/backend/WebDav/StaticFiles/root/README.md@@ -0,0 +1,36 @@+# About `/content`++This directory contains all streamable files that have finished processing from the nzbdav queue.++It is mostly read-only. Files cannot be created, renamed, or moved within this directory.++However, files can be deleted from this directory if you no longer want them, using any compatible webdav client (or using Rclone). But only when the setting under `Settings -> WebDAV -> Enforce Read-Only` is disabled. Deleting items from this directory may break symlinks that point to those items, so be certain you no longer need them before doing so.++> Note: You can run the maintenance task under `Settings -> Maintenance -> Remove Orphaned Files` to remove all files from the `/content` folder that are no longer symlinked by your media library.++---++# About `/completed-symlinks`++This directory contains symlinks for items that have finished processing the nzbdav queue and are still present in the nzbdav history table.++It is read-only. Files cannot be created, renamed, moved, or deleted from this directory. Files in this directory can only be read and copied out of the webdav.++All items under this directory have the *.rclonelink extension, since true symlinks cannot exist on a webdav. Instead, the *.rclonelink files are simple text files whose contents contain the target path of where the symlink should point to.++If using Rclone to mount the webdav onto your filesystem, then Rclone will take care of translating these *.rclonelink files to actual symlinks. You'll need to use the `--links` argument for Rclone to perform this translation.++> NOTE: Be sure to use an updated version of rclone that supports the `--links` argument.+> * Version `v1.70.3` has been known to support it.+> * Version `v1.60.1-DEV` has been known _not_ to support it.++---++# About `/nzbs`++This directory mirrors the nzbdav queue+* Any nzb currently in the queue can be retrieved from this directory.+* You can remove items from the queue by deleting the corresponding nzb from this directory+* You can add items to the queue by uploading nzb files to this directory++> Note: You must perform file operations using any compatible webdav client (or Rclone). The "Dav Explore" page on nzbdav UI does not currently support file operations.\ No newline at end of filediff --git a/frontend/app/routes/explore/route.tsx b/frontend/app/routes/explore/route.tsxindex 26e2801c..ad5a3d6c 100644--- a/frontend/app/routes/explore/route.tsx+++ b/frontend/app/routes/explore/route.tsx@@ -33,7 +33,7 @@ export async function loader({ request }: Route.LoaderArgs) {return {...x,mimeType: getMimeType(x.name),- downloadKey: getDownloadKey(`${path}/${x.name}`)+ downloadKey: getDownloadKey(getRelativePath(path, x.name))};})}@@ -61,7 +61,8 @@ function Body(props: ExplorePageData) {const getFilePath = useCallback((file: ExploreFile) => {var pathname = getWebdavPath(location.pathname);- return `/view/${pathname}/${encodeURIComponent(file.name)}?downloadKey=${file.downloadKey}`;+ var relativePath = getRelativePath(pathname, encodeURIComponent(file.name));+ return `/view/${relativePath}?downloadKey=${file.downloadKey}`;}, [location.pathname]);return (@@ -109,6 +110,11 @@ function getWebdavPathDecoded(pathname: string): string {return decodeURIComponent(getWebdavPath(pathname));}+function getRelativePath(path: string, filename: string) {+ if (path === "") return filename;+ return `${path}/${filename}`;+}+function getParentDirectories(webdavPath: string): string[] {return webdavPath == "" ? [] : webdavPath.split('/');}