2024-10-25 14:31:50 -04:00
|
|
|
// Copyright (C) 2024 Intro-Skipper contributors <intro-skipper.org>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only.
|
2024-10-25 14:15:12 -04:00
|
|
|
|
2024-06-15 10:57:20 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2024-10-19 23:50:41 +02:00
|
|
|
using IntroSkipper.Manager;
|
2024-06-15 10:57:20 +02:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Model.Tasks;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
2024-10-19 23:50:41 +02:00
|
|
|
namespace IntroSkipper.ScheduledTasks;
|
2024-06-15 10:57:20 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Analyze all television episodes for introduction sequences.
|
|
|
|
/// </summary>
|
2024-10-16 16:05:59 +02:00
|
|
|
public class CleanCacheTask : IScheduledTask
|
2024-06-15 10:57:20 +02:00
|
|
|
{
|
2024-10-16 16:05:59 +02:00
|
|
|
private readonly ILogger<CleanCacheTask> _logger;
|
2024-06-15 10:57:20 +02:00
|
|
|
|
2024-10-16 16:05:59 +02:00
|
|
|
private readonly ILoggerFactory _loggerFactory;
|
2024-06-15 10:57:20 +02:00
|
|
|
|
2024-10-16 16:05:59 +02:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="CleanCacheTask"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="loggerFactory">Logger factory.</param>
|
|
|
|
/// <param name="libraryManager">Library manager.</param>
|
|
|
|
/// <param name="logger">Logger.</param>
|
|
|
|
public CleanCacheTask(
|
|
|
|
ILogger<CleanCacheTask> logger,
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
ILibraryManager libraryManager)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_loggerFactory = loggerFactory;
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
}
|
2024-06-15 10:57:20 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the task name.
|
|
|
|
/// </summary>
|
|
|
|
public string Name => "Clean Intro Skipper Cache";
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the task category.
|
|
|
|
/// </summary>
|
|
|
|
public string Category => "Intro Skipper";
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the task description.
|
|
|
|
/// </summary>
|
|
|
|
public string Description => "Clear Intro Skipper cache of unused files.";
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the task key.
|
|
|
|
/// </summary>
|
|
|
|
public string Key => "CPBIntroSkipperCleanCache";
|
|
|
|
|
|
|
|
/// <summary>
|
2024-09-20 14:18:04 +03:00
|
|
|
/// Cleans the cache of unused files.
|
|
|
|
/// Clears the Segment cache by removing files that are no longer associated with episodes in the library.
|
|
|
|
/// Clears the IgnoreList cache by removing items that are no longer associated with seasons in the library.
|
2024-06-15 10:57:20 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="progress">Task progress.</param>
|
|
|
|
/// <param name="cancellationToken">Cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2024-11-02 18:17:22 +01:00
|
|
|
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
|
2024-06-15 10:57:20 +02:00
|
|
|
{
|
|
|
|
if (_libraryManager is null)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Library manager was null");
|
|
|
|
}
|
|
|
|
|
|
|
|
var queueManager = new QueueManager(
|
|
|
|
_loggerFactory.CreateLogger<QueueManager>(),
|
|
|
|
_libraryManager);
|
|
|
|
|
|
|
|
// Retrieve media items and get valid episode IDs
|
|
|
|
var queue = queueManager.GetMediaItems();
|
2024-11-02 18:17:22 +01:00
|
|
|
var validEpisodeIds = queue.Values
|
|
|
|
.SelectMany(episodes => episodes.Select(e => e.EpisodeId))
|
|
|
|
.ToHashSet();
|
|
|
|
|
|
|
|
await Plugin.Instance!.CleanTimestamps(validEpisodeIds).ConfigureAwait(false);
|
2024-06-15 10:57:20 +02:00
|
|
|
|
|
|
|
// Identify invalid episode IDs
|
|
|
|
var invalidEpisodeIds = Directory.EnumerateFiles(Plugin.Instance!.FingerprintCachePath)
|
2024-11-02 18:17:22 +01:00
|
|
|
.Select(filePath => Path.GetFileNameWithoutExtension(filePath).Split('-')[0])
|
|
|
|
.Where(episodeIdStr => Guid.TryParse(episodeIdStr, out var episodeId) && !validEpisodeIds.Contains(episodeId))
|
|
|
|
.Select(Guid.Parse)
|
2024-06-15 10:57:20 +02:00
|
|
|
.ToHashSet();
|
|
|
|
|
|
|
|
// Delete cache files for invalid episode IDs
|
|
|
|
foreach (var episodeId in invalidEpisodeIds)
|
|
|
|
{
|
|
|
|
_logger.LogDebug("Deleting cache files for episode ID: {EpisodeId}", episodeId);
|
|
|
|
FFmpegWrapper.DeleteEpisodeCache(episodeId);
|
|
|
|
}
|
|
|
|
|
2024-11-02 18:17:22 +01:00
|
|
|
// Clean up Season information by removing items that are no longer exist.
|
2024-11-21 15:42:55 +01:00
|
|
|
await Plugin.Instance!.CleanSeasonInfoAsync(queue.Keys).ConfigureAwait(false);
|
|
|
|
|
|
|
|
Plugin.Instance!.AnalyzeAgain = true;
|
2024-09-20 14:18:04 +03:00
|
|
|
|
2024-11-02 18:17:22 +01:00
|
|
|
progress.Report(100);
|
2024-06-15 10:57:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get task triggers.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Task triggers.</returns>
|
|
|
|
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
|
|
|
|
{
|
2024-09-10 18:08:42 +02:00
|
|
|
return [];
|
2024-06-15 10:57:20 +02:00
|
|
|
}
|
|
|
|
}
|