DeleteCacheFileTask.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Controller.IO;
  9. using MediaBrowser.Model.Globalization;
  10. using MediaBrowser.Model.IO;
  11. using MediaBrowser.Model.Tasks;
  12. using Microsoft.Extensions.Logging;
  13. namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
  14. /// <summary>
  15. /// Deletes old cache files.
  16. /// </summary>
  17. public class DeleteCacheFileTask : IScheduledTask, IConfigurableScheduledTask
  18. {
  19. /// <summary>
  20. /// Gets or sets the application paths.
  21. /// </summary>
  22. /// <value>The application paths.</value>
  23. private readonly IApplicationPaths _applicationPaths;
  24. private readonly ILogger<DeleteCacheFileTask> _logger;
  25. private readonly IFileSystem _fileSystem;
  26. private readonly ILocalizationManager _localization;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="DeleteCacheFileTask" /> class.
  29. /// </summary>
  30. /// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
  31. /// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
  32. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  33. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  34. public DeleteCacheFileTask(
  35. IApplicationPaths appPaths,
  36. ILogger<DeleteCacheFileTask> logger,
  37. IFileSystem fileSystem,
  38. ILocalizationManager localization)
  39. {
  40. _applicationPaths = appPaths;
  41. _logger = logger;
  42. _fileSystem = fileSystem;
  43. _localization = localization;
  44. }
  45. /// <inheritdoc />
  46. public string Name => _localization.GetLocalizedString("TaskCleanCache");
  47. /// <inheritdoc />
  48. public string Description => _localization.GetLocalizedString("TaskCleanCacheDescription");
  49. /// <inheritdoc />
  50. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  51. /// <inheritdoc />
  52. public string Key => "DeleteCacheFiles";
  53. /// <inheritdoc />
  54. public bool IsHidden => false;
  55. /// <inheritdoc />
  56. public bool IsEnabled => true;
  57. /// <inheritdoc />
  58. public bool IsLogged => true;
  59. /// <inheritdoc />
  60. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  61. {
  62. yield return new TaskTriggerInfo
  63. {
  64. Type = TaskTriggerInfoType.IntervalTrigger,
  65. IntervalTicks = TimeSpan.FromHours(24).Ticks
  66. };
  67. }
  68. /// <inheritdoc />
  69. public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  70. {
  71. var minDateModified = DateTime.UtcNow.AddDays(-30);
  72. try
  73. {
  74. DeleteCacheFilesFromDirectory(_applicationPaths.CachePath, minDateModified, progress, cancellationToken);
  75. }
  76. catch (DirectoryNotFoundException)
  77. {
  78. // No biggie here. Nothing to delete
  79. }
  80. progress.Report(90);
  81. minDateModified = DateTime.UtcNow.AddDays(-1);
  82. try
  83. {
  84. DeleteCacheFilesFromDirectory(_applicationPaths.TempDirectory, minDateModified, progress, cancellationToken);
  85. }
  86. catch (DirectoryNotFoundException)
  87. {
  88. // No biggie here. Nothing to delete
  89. }
  90. return Task.CompletedTask;
  91. }
  92. /// <summary>
  93. /// Deletes the cache files from directory with a last write time less than a given date.
  94. /// </summary>
  95. /// <param name="directory">The directory.</param>
  96. /// <param name="minDateModified">The min date modified.</param>
  97. /// <param name="progress">The progress.</param>
  98. /// <param name="cancellationToken">The task cancellation token.</param>
  99. private void DeleteCacheFilesFromDirectory(string directory, DateTime minDateModified, IProgress<double> progress, CancellationToken cancellationToken)
  100. {
  101. var filesToDelete = _fileSystem.GetFiles(directory, true)
  102. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  103. .ToList();
  104. var index = 0;
  105. foreach (var file in filesToDelete)
  106. {
  107. double percent = index;
  108. percent /= filesToDelete.Count;
  109. progress.Report(100 * percent);
  110. cancellationToken.ThrowIfCancellationRequested();
  111. FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger);
  112. index++;
  113. }
  114. FileSystemHelper.DeleteEmptyFolders(_fileSystem, directory, _logger);
  115. progress.Report(100);
  116. }
  117. }