using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Enums;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Trickplay;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.Trickplay;
///
/// Class TrickplayMoveImagesTask.
///
public class TrickplayMoveImagesTask : IScheduledTask
{
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
private readonly ILocalizationManager _localization;
private readonly ITrickplayManager _trickplayManager;
///
/// Initializes a new instance of the class.
///
/// The logger.
/// The library manager.
/// The localization manager.
/// The trickplay manager.
public TrickplayMoveImagesTask(
ILogger logger,
ILibraryManager libraryManager,
ILocalizationManager localization,
ITrickplayManager trickplayManager)
{
_libraryManager = libraryManager;
_logger = logger;
_localization = localization;
_trickplayManager = trickplayManager;
}
///
public string Name => _localization.GetLocalizedString("TaskMoveTrickplayImages");
///
public string Description => _localization.GetLocalizedString("TaskMoveTrickplayImagesDescription");
///
public string Key => "MoveTrickplayImages";
///
public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
///
public IEnumerable GetDefaultTriggers() => [];
///
public async Task ExecuteAsync(IProgress progress, CancellationToken cancellationToken)
{
const int Limit = 100;
int itemCount = 0, offset = 0, previousCount;
// This count may not be accurate, but just get something to show progress on the dashboard.
var totalVideoCount = _libraryManager.GetCount(new InternalItemsQuery
{
MediaTypes = [MediaType.Video],
SourceTypes = [SourceType.Library],
IsVirtualItem = false,
IsFolder = false,
Recursive = true
});
var trickplayQuery = new InternalItemsQuery
{
MediaTypes = [MediaType.Video],
SourceTypes = [SourceType.Library],
IsVirtualItem = false,
IsFolder = false
};
do
{
var trickplayInfos = await _trickplayManager.GetTrickplayItemsAsync(Limit, offset).ConfigureAwait(false);
previousCount = trickplayInfos.Count;
offset += Limit;
trickplayQuery.ItemIds = trickplayInfos.Select(i => i.ItemId).Distinct().ToArray();
var items = _libraryManager.GetItemList(trickplayQuery);
foreach (var trickplayInfo in trickplayInfos)
{
cancellationToken.ThrowIfCancellationRequested();
var video = items.OfType