using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
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.Controller.ScheduledTasks
{
///
/// Class ImageCleanupTask
///
public class ImageCleanupTask : BaseScheduledTask
{
///
/// Initializes a new instance of the class.
///
/// The kernel.
/// The task manager.
/// The logger.
public ImageCleanupTask(Kernel kernel, ITaskManager taskManager, ILogger logger)
: base(kernel, taskManager, logger)
{
}
///
/// Creates the triggers that define when the task will run
///
/// IEnumerable{BaseTaskTrigger}.
public override IEnumerable GetDefaultTriggers()
{
return new ITaskTrigger[]
{
new DailyTrigger { TimeOfDay = TimeSpan.FromHours(2) }
};
}
///
/// Returns the task to be executed
///
/// The cancellation token.
/// The progress.
/// Task.
protected override async Task ExecuteInternal(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(Kernel.ProviderManager.ImagesDataPath))
.ToList();
// Now gather all items
var items = Kernel.RootFolder.RecursiveChildren.ToList();
items.Add(Kernel.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 = Kernel.RootFolder.RecursiveChildren.OfType