Update Media Segments when timestamps where changed in the editor

This commit is contained in:
rlauu 2024-10-30 16:57:23 +01:00
parent 87875c8a11
commit 69960bfa5b
2 changed files with 34 additions and 10 deletions

View File

@ -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
{
/// <summary>
/// Initializes a new instance of the <see cref="SkipIntroController"/> class.
/// </summary>
public SkipIntroController()
{
}
private readonly MediaSegmentUpdateManager _mediaSegmentUpdateManager = mediaSegmentUpdateManager;
/// <summary>
/// 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
/// <returns>No content.</returns>
[Authorize(Policy = Policies.RequiresElevation)]
[HttpPost("Episode/{Id}/Timestamps")]
public ActionResult UpdateTimestamps([FromRoute] Guid id, [FromBody] TimeStamps timestamps)
public async Task<ActionResult> 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();
}

View File

@ -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 <see cref="VisualizationController"/> class.
/// </remarks>
/// <param name="logger">Logger.</param>
/// <param name="mediaSegmentUpdateManager">Media Segment Update Manager.</param>
[Authorize(Policy = Policies.RequiresElevation)]
[ApiController]
[Produces(MediaTypeNames.Application.Json)]
[Route("Intros")]
public class VisualizationController(ILogger<VisualizationController> logger) : ControllerBase
public class VisualizationController(ILogger<VisualizationController> logger, MediaSegmentUpdateManager mediaSegmentUpdateManager) : ControllerBase
{
private readonly ILogger<VisualizationController> _logger = logger;
private readonly MediaSegmentUpdateManager _mediaSegmentUpdateManager = mediaSegmentUpdateManager;
/// <summary>
/// Returns all show names and seasons.
@ -180,7 +185,7 @@ public class VisualizationController(ILogger<VisualizationController> logger) :
/// <response code="404">Unable to find season in provided series.</response>
/// <returns>No content.</returns>
[HttpDelete("Show/{SeriesId}/{SeasonId}")]
public ActionResult EraseSeason([FromRoute] Guid seriesId, [FromRoute] Guid seasonId, [FromQuery] bool eraseCache = false)
public async Task<ActionResult> 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<VisualizationController> 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();
}