From ce5ebfbdc1f0c1ed75c58420e471f2ffcb087b02 Mon Sep 17 00:00:00 2001 From: Lukas Nagel Date: Fri, 27 Sep 2024 16:54:57 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Basic=20api=20endpoints?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #1 --- OpenMediaServer/Endpoints/ApiEndpoints.cs | 47 +++++++++++++++++++ OpenMediaServer/Endpoints/MovieEndpoints.cs | 19 -------- .../Endpoints/StreamingEndpoints.cs | 31 ++++++++++++ .../{IMovieEndpoints.cs => IApiEndpoints.cs} | 2 +- .../Endpoints/IStreamingEndpoints.cs | 8 ++++ .../Interfaces/Services/IInventoryService.cs | 3 ++ .../Interfaces/Services/IStreamingService.cs | 8 ++++ OpenMediaServer/Program.cs | 11 ++--- OpenMediaServer/Services/InventoryService.cs | 15 ++++++ OpenMediaServer/Services/StreamingService.cs | 21 +++++++++ 10 files changed, 139 insertions(+), 26 deletions(-) create mode 100644 OpenMediaServer/Endpoints/ApiEndpoints.cs delete mode 100644 OpenMediaServer/Endpoints/MovieEndpoints.cs create mode 100644 OpenMediaServer/Endpoints/StreamingEndpoints.cs rename OpenMediaServer/Interfaces/Endpoints/{IMovieEndpoints.cs => IApiEndpoints.cs} (76%) create mode 100644 OpenMediaServer/Interfaces/Endpoints/IStreamingEndpoints.cs create mode 100644 OpenMediaServer/Interfaces/Services/IStreamingService.cs create mode 100644 OpenMediaServer/Services/StreamingService.cs diff --git a/OpenMediaServer/Endpoints/ApiEndpoints.cs b/OpenMediaServer/Endpoints/ApiEndpoints.cs new file mode 100644 index 0000000..15ebb0a --- /dev/null +++ b/OpenMediaServer/Endpoints/ApiEndpoints.cs @@ -0,0 +1,47 @@ +using System; +using OpenMediaServer.Interfaces.Endpoints; +using OpenMediaServer.Interfaces.Services; + +namespace OpenMediaServer.Endpoints; + +public class ApiEndpoints : IApiEndpoints +{ + private readonly ILogger _logger; + private readonly IInventoryService _inventoryService; + + public ApiEndpoints(ILogger logger, IInventoryService inventoryService) + { + _logger = logger; + _inventoryService = inventoryService; + } + + public void Map(WebApplication app) + { + var group = app.MapGroup("/api"); + + group.MapGet("/categories", ListCategories); + group.MapGet("/items", ListItems); + group.MapGet("/item", GetItem); + } + + public async Task ListCategories() + { + var categories = await _inventoryService.ListCategories(); + + return Results.Ok(categories); + } + + public async Task ListItems(string category) + { + var items = await _inventoryService.ListItems(category); + + return Results.Ok(items); + } + + public async Task GetItem(string category, string id) + { + var item = await _inventoryService.GetItem(id: id, category: category); + + return Results.Ok(item); + } +} diff --git a/OpenMediaServer/Endpoints/MovieEndpoints.cs b/OpenMediaServer/Endpoints/MovieEndpoints.cs deleted file mode 100644 index 1a15397..0000000 --- a/OpenMediaServer/Endpoints/MovieEndpoints.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using OpenMediaServer.Interfaces.Endpoints; - -namespace OpenMediaServer.Endpoints; - -public class MovieEndpoints : IMovieEndpoints -{ - public void Map(WebApplication app) - { - app.MapGet("/video", GetData); - } - - public IResult GetData(string name) - { - string path = Path.Combine(Globals.MediaFolder, "Movies", name); - - return Results.Stream(new FileStream(path, FileMode.Open), enableRangeProcessing: true, contentType: "video/webm"); - } -} diff --git a/OpenMediaServer/Endpoints/StreamingEndpoints.cs b/OpenMediaServer/Endpoints/StreamingEndpoints.cs new file mode 100644 index 0000000..5d922e7 --- /dev/null +++ b/OpenMediaServer/Endpoints/StreamingEndpoints.cs @@ -0,0 +1,31 @@ +using System; +using OpenMediaServer.Interfaces.Endpoints; +using OpenMediaServer.Interfaces.Services; + +namespace OpenMediaServer.Endpoints; + +public class StreamingEndpoints : IStreamingEndpoints +{ + private readonly ILogger _logger; + private readonly IStreamingService _streamingService; + + public StreamingEndpoints(ILogger logger, IStreamingService streamingService) + { + _logger = logger; + _streamingService = streamingService; + } + + public void Map(WebApplication app) + { + var group = app.MapGroup("/stream"); + + group.MapGet("/{category}/{id}", StreamContent); + } + + public async Task StreamContent(string id, string category) + { + using var stream = await _streamingService.GetMediaStream(id, category); + + return Results.Stream(stream, enableRangeProcessing: true, contentType: "video/webm"); // TODO content type + } +} diff --git a/OpenMediaServer/Interfaces/Endpoints/IMovieEndpoints.cs b/OpenMediaServer/Interfaces/Endpoints/IApiEndpoints.cs similarity index 76% rename from OpenMediaServer/Interfaces/Endpoints/IMovieEndpoints.cs rename to OpenMediaServer/Interfaces/Endpoints/IApiEndpoints.cs index 132a81c..0eff793 100644 --- a/OpenMediaServer/Interfaces/Endpoints/IMovieEndpoints.cs +++ b/OpenMediaServer/Interfaces/Endpoints/IApiEndpoints.cs @@ -2,7 +2,7 @@ namespace OpenMediaServer.Interfaces.Endpoints; -public interface IMovieEndpoints +public interface IApiEndpoints { public void Map(WebApplication app); } diff --git a/OpenMediaServer/Interfaces/Endpoints/IStreamingEndpoints.cs b/OpenMediaServer/Interfaces/Endpoints/IStreamingEndpoints.cs new file mode 100644 index 0000000..7c10ecd --- /dev/null +++ b/OpenMediaServer/Interfaces/Endpoints/IStreamingEndpoints.cs @@ -0,0 +1,8 @@ +using System; + +namespace OpenMediaServer.Interfaces.Endpoints; + +public interface IStreamingEndpoints +{ + public void Map(WebApplication app); +} diff --git a/OpenMediaServer/Interfaces/Services/IInventoryService.cs b/OpenMediaServer/Interfaces/Services/IInventoryService.cs index 3728a9e..ce96096 100644 --- a/OpenMediaServer/Interfaces/Services/IInventoryService.cs +++ b/OpenMediaServer/Interfaces/Services/IInventoryService.cs @@ -8,4 +8,7 @@ public interface IInventoryService public void AddItem(T item) where T : InventoryItem; public void AddItems(IEnumerable items); public void CreateFromPaths(IEnumerable paths); + public Task> ListCategories(); + public Task> ListItems(string category); + public Task GetItem(string id, string category); } diff --git a/OpenMediaServer/Interfaces/Services/IStreamingService.cs b/OpenMediaServer/Interfaces/Services/IStreamingService.cs new file mode 100644 index 0000000..9929957 --- /dev/null +++ b/OpenMediaServer/Interfaces/Services/IStreamingService.cs @@ -0,0 +1,8 @@ +using System; + +namespace OpenMediaServer.Interfaces.Services; + +public interface IStreamingService +{ + public Task GetMediaStream(string id, string category); +} diff --git a/OpenMediaServer/Program.cs b/OpenMediaServer/Program.cs index 39e61ec..b67d6fe 100644 --- a/OpenMediaServer/Program.cs +++ b/OpenMediaServer/Program.cs @@ -16,26 +16,25 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); -builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddHttpClient(); var app = builder.Build(); - app.UseSwagger(); app.UseSwaggerUI(); - app.UseHttpsRedirection(); - var contentDiscoveryService = app.Services.GetService(); contentDiscoveryService?.ActiveScan(Globals.MediaFolder); contentDiscoveryService?.Watch(Globals.MediaFolder); -var movieEndpoints = app.Services.GetService(); -movieEndpoints?.Map(app); +app.Services.GetService()?.Map(app); +app.Services.GetService()?.Map(app); app.Run(); \ No newline at end of file diff --git a/OpenMediaServer/Services/InventoryService.cs b/OpenMediaServer/Services/InventoryService.cs index 37ec6e8..483c1e7 100644 --- a/OpenMediaServer/Services/InventoryService.cs +++ b/OpenMediaServer/Services/InventoryService.cs @@ -21,6 +21,21 @@ public InventoryService(ILogger logger, IStorageRepository sto _configuration = configuration; } + public async Task> ListCategories() + { + return new List { }; // TODO Implement + } + + public async Task> ListItems(string category) + { + return new List { }; // TODO Implement + } + + public async Task GetItem(string id, string category) + { + return new InventoryItem(); // TODO Implement + } + public async void CreateFromPaths(IEnumerable paths) { foreach (var path in paths) diff --git a/OpenMediaServer/Services/StreamingService.cs b/OpenMediaServer/Services/StreamingService.cs new file mode 100644 index 0000000..6000c9c --- /dev/null +++ b/OpenMediaServer/Services/StreamingService.cs @@ -0,0 +1,21 @@ +using System; +using OpenMediaServer.Interfaces.Services; + +namespace OpenMediaServer.Services; + +public class StreamingService : IStreamingService +{ + private readonly ILogger _logger; + + public StreamingService(ILogger logger) + { + _logger = logger; + } + + public async Task GetMediaStream(string id, string category) + { + // TODO Implement + + throw new NotImplementedException(); + } +}