using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Model.Tasks;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.ScheduledTasks
{
///
/// Class ImageCleanupTask
///
[Export(typeof(IScheduledTask))]
public class ImageCleanupTask : BaseScheduledTask
{
///
/// Creates the triggers that define when the task will run
///
/// IEnumerable{BaseTaskTrigger}.
protected override IEnumerable GetDefaultTriggers()
{
return new BaseTaskTrigger[]
{
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(new TaskProgress { PercentComplete = 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