From 4c8e7f62597d02611309ea3a2c714d755502e92b Mon Sep 17 00:00:00 2001 From: Namo Date: Sun, 9 Jun 2024 23:58:47 +0200 Subject: [PATCH] Added Episode Play function to Web version --- .../Api/InPlayerPreviewController.cs | 38 +++++++++++++++++-- .../Web/Components/EpisodeElementTemplate.ts | 2 +- .../PlaybackHandler/PlaybackHandler.ts | 2 +- .../PlaybackHandler/WebPlaybackHandler.ts | 11 +++--- .../Web/inPlayerPreview.ts | 4 +- 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Namo.Plugin.InPlayerEpisodePreview/Api/InPlayerPreviewController.cs b/Namo.Plugin.InPlayerEpisodePreview/Api/InPlayerPreviewController.cs index ca15144..f8f7412 100644 --- a/Namo.Plugin.InPlayerEpisodePreview/Api/InPlayerPreviewController.cs +++ b/Namo.Plugin.InPlayerEpisodePreview/Api/InPlayerPreviewController.cs @@ -6,12 +6,11 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using System.ComponentModel.DataAnnotations; -using System.Net.Mime; using System.Reflection; -using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Persistence; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.Session; using Namo.Plugin.InPlayerEpisodePreview.Configuration; namespace Namo.Plugin.InPlayerEpisodePreview.Api; @@ -35,6 +34,7 @@ public class InPlayerPreviewController : ControllerBase private readonly ILibraryMonitor _libraryMonitor; private readonly IMediaEncoder _mediaEncoder; private readonly IServerConfigurationManager _configurationManager; + private readonly ISessionManager _sessionManager; private readonly EncodingHelper _encodingHelper; private readonly PluginConfiguration _config; @@ -52,6 +52,7 @@ public InPlayerPreviewController( ILibraryMonitor libraryMonitor, IMediaEncoder mediaEncoder, IServerConfigurationManager configurationManager, + ISessionManager sessionManager, EncodingHelper encodingHelper) { _assembly = Assembly.GetExecutingAssembly(); @@ -66,6 +67,7 @@ public InPlayerPreviewController( _libraryMonitor = libraryMonitor; _mediaEncoder = mediaEncoder; _configurationManager = configurationManager; + _sessionManager = sessionManager; _encodingHelper = encodingHelper; _config = InPlayerEpisodePreviewPlugin.Instance!.Configuration; @@ -90,4 +92,34 @@ public ActionResult GetClientScript() return File(scriptStream, "application/javascript"); } + + [HttpGet("Users/{userId}/Items/{id}/Play/{ticks}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public ActionResult StartMedia([FromRoute] Guid userId, [FromRoute] Guid id, [FromRoute] long ticks = 0) + { + SessionInfo? session = _sessionManager.Sessions.FirstOrDefault(session => session.UserId.Equals(userId)); + if (session is null) + { + _logger.LogInformation("Couldn't find a valid session for this user"); + return NotFound("Couldn't find a valid session for this user"); + } + + BaseItem? item = _libraryManager.GetItemById(id); + if (item is null) + { + _logger.LogInformation("Couldn't find item to play"); + return NotFound("Couldn't find item to play"); + } + + _sessionManager.SendPlayCommand(session.Id, session.Id, + new PlayRequest + { + ItemIds = [item.Id], + StartPositionTicks = ticks, + PlayCommand = PlayCommand.PlayNow, + }, CancellationToken.None); + + return Ok(); + } } diff --git a/Namo.Plugin.InPlayerEpisodePreview/Web/Components/EpisodeElementTemplate.ts b/Namo.Plugin.InPlayerEpisodePreview/Web/Components/EpisodeElementTemplate.ts index e5ad1df..4924555 100644 --- a/Namo.Plugin.InPlayerEpisodePreview/Web/Components/EpisodeElementTemplate.ts +++ b/Namo.Plugin.InPlayerEpisodePreview/Web/Components/EpisodeElementTemplate.ts @@ -60,7 +60,7 @@ export class EpisodeElementTemplate extends BaseTemplate { -