diff --git a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs index d4b6c0b9e8..812269ea8a 100644 --- a/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs +++ b/MediaBrowser.Common.Implementations/ScheduledTasks/Tasks/DeleteCacheFileTask.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Model.Logging; namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks { @@ -20,13 +21,16 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks /// The application paths. private IApplicationPaths ApplicationPaths { get; set; } + private readonly ILogger _logger; + /// /// Initializes a new instance of the class. /// /// The app paths. - public DeleteCacheFileTask(IApplicationPaths appPaths) + public DeleteCacheFileTask(IApplicationPaths appPaths, ILogger logger) { ApplicationPaths = appPaths; + _logger = logger; } /// @@ -54,12 +58,29 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks /// Task. public Task Execute(CancellationToken cancellationToken, IProgress progress) { - return Task.Run(() => - { - var minDateModified = DateTime.UtcNow.AddDays(-30); + var minDateModified = DateTime.UtcNow.AddDays(-30); + try + { DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.CachePath, minDateModified, progress); - }); + } + catch (DirectoryNotFoundException) + { + // No biggie here. Nothing to delete + } + + progress.Report(90); + + try + { + DeleteCacheFilesFromDirectory(cancellationToken, ApplicationPaths.TempDirectory, DateTime.MaxValue, progress); + } + catch (DirectoryNotFoundException) + { + // No biggie here. Nothing to delete + } + + return Task.FromResult(true); } @@ -72,8 +93,8 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks /// The progress. private void DeleteCacheFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress progress) { - var filesToDelete = new DirectoryInfo(directory).EnumerateFileSystemInfos("*", SearchOption.AllDirectories) - .Where(f => !f.Attributes.HasFlag(FileAttributes.Directory) && f.LastWriteTimeUtc < minDateModified) + var filesToDelete = new DirectoryInfo(directory).EnumerateFiles("*", SearchOption.AllDirectories) + .Where(f => f.LastWriteTimeUtc < minDateModified) .ToList(); var index = 0; @@ -87,7 +108,7 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks cancellationToken.ThrowIfCancellationRequested(); - File.Delete(file.FullName); + DeleteFile(file.FullName); index++; } @@ -95,6 +116,18 @@ namespace MediaBrowser.Common.Implementations.ScheduledTasks.Tasks progress.Report(100); } + private void DeleteFile(string path) + { + try + { + File.Delete(path); + } + catch (IOException ex) + { + _logger.ErrorException("Error deleting file {0}", ex, path); + } + } + /// /// Gets the name of the task ///