using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.ScheduledTasks
{
///
/// Class ImageCleanupTask
///
public class ImageCleanupTask : IScheduledTask
{
///
/// The _kernel
///
private readonly Kernel _kernel;
///
/// The _logger
///
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
private readonly IServerApplicationPaths _appPaths;
///
/// Initializes a new instance of the class.
///
/// The kernel.
/// The logger.
/// The library manager.
/// The app paths.
public ImageCleanupTask(Kernel kernel, ILogger logger, ILibraryManager libraryManager, IServerApplicationPaths appPaths)
{
_kernel = kernel;
_logger = logger;
_libraryManager = libraryManager;
_appPaths = appPaths;
}
///
/// Creates the triggers that define when the task will run
///
/// IEnumerable{BaseTaskTrigger}.
public IEnumerable GetDefaultTriggers()
{
return new ITaskTrigger[]
{
new DailyTrigger { TimeOfDay = TimeSpan.FromHours(2) }
};
}
///
/// Returns the task to be executed
///
/// The cancellation token.
/// The progress.
/// Task.
public async Task Execute(CancellationToken cancellationToken, IProgress progress)
{
await EnsureChapterImages(cancellationToken).ConfigureAwait(false);
// First gather all image files
var files = GetFiles(_kernel.FFMpegManager.AudioImagesDataPath)
.Concat(GetFiles(_kernel.FFMpegManager.VideoImagesDataPath))
.Concat(GetFiles(_appPaths.DownloadedImagesDataPath))
.ToList();
// Now gather all items
var items = _libraryManager.RootFolder.RecursiveChildren.ToList();
items.Add(_libraryManager.RootFolder);
// Determine all possible image paths
var pathsInUse = items.SelectMany(GetPathsInUse)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToDictionary(p => p, StringComparer.OrdinalIgnoreCase);
var numComplete = 0;
var tasks = files.Select(file => Task.Run(() =>
{
cancellationToken.ThrowIfCancellationRequested();
if (!pathsInUse.ContainsKey(file))
{
cancellationToken.ThrowIfCancellationRequested();
try
{
File.Delete(file);
}
catch (IOException ex)
{
_logger.ErrorException("Error deleting {0}", ex, file);
}
}
// Update progress
lock (progress)
{
numComplete++;
double percent = numComplete;
percent /= files.Count;
progress.Report(100 * percent);
}
}));
await Task.WhenAll(tasks).ConfigureAwait(false);
}
///
/// Ensures the chapter images.
///
/// The cancellation token.
/// Task.
private Task EnsureChapterImages(CancellationToken cancellationToken)
{
var videos = _libraryManager.RootFolder.RecursiveChildren.OfType