DeleteTranscodeFileTask.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.Model.IO;
  9. using MediaBrowser.Model.Tasks;
  10. using Microsoft.Extensions.Logging;
  11. using MediaBrowser.Model.Globalization;
  12. namespace Emby.Server.Implementations.ScheduledTasks.Tasks
  13. {
  14. /// <summary>
  15. /// Deletes all transcoding temp files.
  16. /// </summary>
  17. public class DeleteTranscodeFileTask : IScheduledTask, IConfigurableScheduledTask
  18. {
  19. private readonly ILogger<DeleteTranscodeFileTask> _logger;
  20. private readonly IConfigurationManager _configurationManager;
  21. private readonly IFileSystem _fileSystem;
  22. private readonly ILocalizationManager _localization;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="DeleteTranscodeFileTask" /> class.
  25. /// </summary>
  26. public DeleteTranscodeFileTask(
  27. ILogger<DeleteTranscodeFileTask> logger,
  28. IFileSystem fileSystem,
  29. IConfigurationManager configurationManager,
  30. ILocalizationManager localization)
  31. {
  32. _logger = logger;
  33. _fileSystem = fileSystem;
  34. _configurationManager = configurationManager;
  35. _localization = localization;
  36. }
  37. /// <summary>
  38. /// Creates the triggers that define when the task will run.
  39. /// </summary>
  40. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  41. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() => new List<TaskTriggerInfo>();
  42. /// <summary>
  43. /// Returns the task to be executed.
  44. /// </summary>
  45. /// <param name="cancellationToken">The cancellation token.</param>
  46. /// <param name="progress">The progress.</param>
  47. /// <returns>Task.</returns>
  48. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  49. {
  50. var minDateModified = DateTime.UtcNow.AddDays(-1);
  51. progress.Report(50);
  52. DeleteTempFilesFromDirectory(cancellationToken, _configurationManager.GetTranscodePath(), minDateModified, progress);
  53. return Task.CompletedTask;
  54. }
  55. /// <summary>
  56. /// Deletes the transcoded temp files from directory with a last write time less than a given date.
  57. /// </summary>
  58. /// <param name="cancellationToken">The task cancellation token.</param>
  59. /// <param name="directory">The directory.</param>
  60. /// <param name="minDateModified">The min date modified.</param>
  61. /// <param name="progress">The progress.</param>
  62. private void DeleteTempFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<double> progress)
  63. {
  64. var filesToDelete = _fileSystem.GetFiles(directory, true)
  65. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  66. .ToList();
  67. var index = 0;
  68. foreach (var file in filesToDelete)
  69. {
  70. double percent = index;
  71. percent /= filesToDelete.Count;
  72. progress.Report(100 * percent);
  73. cancellationToken.ThrowIfCancellationRequested();
  74. DeleteFile(file.FullName);
  75. index++;
  76. }
  77. DeleteEmptyFolders(directory);
  78. progress.Report(100);
  79. }
  80. private void DeleteEmptyFolders(string parent)
  81. {
  82. foreach (var directory in _fileSystem.GetDirectoryPaths(parent))
  83. {
  84. DeleteEmptyFolders(directory);
  85. if (!_fileSystem.GetFileSystemEntryPaths(directory).Any())
  86. {
  87. try
  88. {
  89. Directory.Delete(directory, false);
  90. }
  91. catch (UnauthorizedAccessException ex)
  92. {
  93. _logger.LogError(ex, "Error deleting directory {path}", directory);
  94. }
  95. catch (IOException ex)
  96. {
  97. _logger.LogError(ex, "Error deleting directory {path}", directory);
  98. }
  99. }
  100. }
  101. }
  102. private void DeleteFile(string path)
  103. {
  104. try
  105. {
  106. _fileSystem.DeleteFile(path);
  107. }
  108. catch (UnauthorizedAccessException ex)
  109. {
  110. _logger.LogError(ex, "Error deleting file {path}", path);
  111. }
  112. catch (IOException ex)
  113. {
  114. _logger.LogError(ex, "Error deleting file {path}", path);
  115. }
  116. }
  117. /// <inheritdoc />
  118. public string Name => _localization.GetLocalizedString("TaskCleanTranscode");
  119. /// <inheritdoc />
  120. public string Description => _localization.GetLocalizedString("TaskCleanTranscodeDescription");
  121. /// <inheritdoc />
  122. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  123. /// <inheritdoc />
  124. public string Key => "DeleteTranscodeFiles";
  125. /// <inheritdoc />
  126. public bool IsHidden => false;
  127. /// <inheritdoc />
  128. public bool IsEnabled => false;
  129. /// <inheritdoc />
  130. public bool IsLogged => true;
  131. }
  132. }