diff --git a/IntroSkipper/Controllers/SkipIntroController.cs b/IntroSkipper/Controllers/SkipIntroController.cs index 68b66cb..e188193 100644 --- a/IntroSkipper/Controllers/SkipIntroController.cs +++ b/IntroSkipper/Controllers/SkipIntroController.cs @@ -3,9 +3,13 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Net.Mime; +using System.Threading; +using System.Threading.Tasks; using IntroSkipper.Configuration; using IntroSkipper.Data; +using IntroSkipper.Manager; using MediaBrowser.Common.Api; using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Entities.TV; @@ -20,14 +24,9 @@ namespace IntroSkipper.Controllers; [Authorize] [ApiController] [Produces(MediaTypeNames.Application.Json)] -public class SkipIntroController : ControllerBase +public class SkipIntroController(MediaSegmentUpdateManager mediaSegmentUpdateManager) : ControllerBase { - /// - /// Initializes a new instance of the class. - /// - public SkipIntroController() - { - } + private readonly MediaSegmentUpdateManager _mediaSegmentUpdateManager = mediaSegmentUpdateManager; /// /// Returns the timestamps of the introduction in a television episode. Responses are in API version 1 format. @@ -63,7 +62,7 @@ public class SkipIntroController : ControllerBase /// No content. [Authorize(Policy = Policies.RequiresElevation)] [HttpPost("Episode/{Id}/Timestamps")] - public ActionResult UpdateTimestamps([FromRoute] Guid id, [FromBody] TimeStamps timestamps) + public async Task UpdateTimestampsAsync([FromRoute] Guid id, [FromBody] TimeStamps timestamps) { // only update existing episodes var rawItem = Plugin.Instance!.GetItem(id); @@ -87,6 +86,20 @@ public class SkipIntroController : ControllerBase Plugin.Instance!.SaveTimestamps(AnalysisMode.Introduction); Plugin.Instance!.SaveTimestamps(AnalysisMode.Credits); + if (Plugin.Instance.Configuration.UpdateMediaSegments) + { + var seasonId = rawItem is Episode e ? e.SeasonId : rawItem.Id; + var episode = Plugin.Instance!.QueuedMediaItems + .FirstOrDefault(kvp => kvp.Key == seasonId).Value + .FirstOrDefault(e => e.EpisodeId == rawItem.Id); + + if (episode is not null) + { + using var ct = new CancellationTokenSource(); + await _mediaSegmentUpdateManager.UpdateMediaSegmentsAsync([episode], ct.Token).ConfigureAwait(false); + } + } + return NoContent(); } diff --git a/IntroSkipper/Controllers/VisualizationController.cs b/IntroSkipper/Controllers/VisualizationController.cs index 8bf2f6f..17262af 100644 --- a/IntroSkipper/Controllers/VisualizationController.cs +++ b/IntroSkipper/Controllers/VisualizationController.cs @@ -6,7 +6,10 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Net.Mime; +using System.Threading; +using System.Threading.Tasks; using IntroSkipper.Data; +using IntroSkipper.Manager; using MediaBrowser.Common.Api; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -21,13 +24,15 @@ namespace IntroSkipper.Controllers; /// Initializes a new instance of the class. /// /// Logger. +/// Media Segment Update Manager. [Authorize(Policy = Policies.RequiresElevation)] [ApiController] [Produces(MediaTypeNames.Application.Json)] [Route("Intros")] -public class VisualizationController(ILogger logger) : ControllerBase +public class VisualizationController(ILogger logger, MediaSegmentUpdateManager mediaSegmentUpdateManager) : ControllerBase { private readonly ILogger _logger = logger; + private readonly MediaSegmentUpdateManager _mediaSegmentUpdateManager = mediaSegmentUpdateManager; /// /// Returns all show names and seasons. @@ -180,7 +185,7 @@ public class VisualizationController(ILogger logger) : /// Unable to find season in provided series. /// No content. [HttpDelete("Show/{SeriesId}/{SeasonId}")] - public ActionResult EraseSeason([FromRoute] Guid seriesId, [FromRoute] Guid seasonId, [FromQuery] bool eraseCache = false) + public async Task EraseSeason([FromRoute] Guid seriesId, [FromRoute] Guid seasonId, [FromQuery] bool eraseCache = false) { var episodes = Plugin.Instance!.QueuedMediaItems .Where(kvp => kvp.Key == seasonId) @@ -207,6 +212,12 @@ public class VisualizationController(ILogger logger) : Plugin.Instance!.SaveTimestamps(AnalysisMode.Introduction | AnalysisMode.Credits); + if (Plugin.Instance.Configuration.UpdateMediaSegments) + { + using var ct = new CancellationTokenSource(); + await _mediaSegmentUpdateManager.UpdateMediaSegmentsAsync(episodes, ct.Token).ConfigureAwait(false); + } + return NoContent(); }