DeleteTranscodeFileTask.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 _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. {
  31. _logger = logger;
  32. _fileSystem = fileSystem;
  33. _configurationManager = configurationManager;
  34. }
  35. /// <summary>
  36. /// Creates the triggers that define when the task will run.
  37. /// </summary>
  38. /// <returns>IEnumerable{BaseTaskTrigger}.</returns>
  39. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() => new List<TaskTriggerInfo>();
  40. /// <summary>
  41. /// Returns the task to be executed.
  42. /// </summary>
  43. /// <param name="cancellationToken">The cancellation token.</param>
  44. /// <param name="progress">The progress.</param>
  45. /// <returns>Task.</returns>
  46. public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
  47. {
  48. var minDateModified = DateTime.UtcNow.AddDays(-1);
  49. progress.Report(50);
  50. DeleteTempFilesFromDirectory(cancellationToken, _configurationManager.GetTranscodePath(), minDateModified, progress);
  51. return Task.CompletedTask;
  52. }
  53. /// <summary>
  54. /// Deletes the transcoded temp files from directory with a last write time less than a given date.
  55. /// </summary>
  56. /// <param name="cancellationToken">The task cancellation token.</param>
  57. /// <param name="directory">The directory.</param>
  58. /// <param name="minDateModified">The min date modified.</param>
  59. /// <param name="progress">The progress.</param>
  60. private void DeleteTempFilesFromDirectory(CancellationToken cancellationToken, string directory, DateTime minDateModified, IProgress<double> progress)
  61. {
  62. var filesToDelete = _fileSystem.GetFiles(directory, true)
  63. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  64. .ToList();
  65. var index = 0;
  66. foreach (var file in filesToDelete)
  67. {
  68. double percent = index;
  69. percent /= filesToDelete.Count;
  70. progress.Report(100 * percent);
  71. cancellationToken.ThrowIfCancellationRequested();
  72. DeleteFile(file.FullName);
  73. index++;
  74. }
  75. DeleteEmptyFolders(directory);
  76. progress.Report(100);
  77. }
  78. private void DeleteEmptyFolders(string parent)
  79. {
  80. foreach (var directory in _fileSystem.GetDirectoryPaths(parent))
  81. {
  82. DeleteEmptyFolders(directory);
  83. if (!_fileSystem.GetFileSystemEntryPaths(directory).Any())
  84. {
  85. try
  86. {
  87. Directory.Delete(directory, false);
  88. }
  89. catch (UnauthorizedAccessException ex)
  90. {
  91. _logger.LogError(ex, "Error deleting directory {path}", directory);
  92. }
  93. catch (IOException ex)
  94. {
  95. _logger.LogError(ex, "Error deleting directory {path}", directory);
  96. }
  97. }
  98. }
  99. }
  100. private void DeleteFile(string path)
  101. {
  102. try
  103. {
  104. _fileSystem.DeleteFile(path);
  105. }
  106. catch (UnauthorizedAccessException ex)
  107. {
  108. _logger.LogError(ex, "Error deleting file {path}", path);
  109. }
  110. catch (IOException ex)
  111. {
  112. _logger.LogError(ex, "Error deleting file {path}", path);
  113. }
  114. }
  115. public string Name => _localization.GetLocalizedString("TaskCleanTranscode");
  116. public string Description => _localization.GetLocalizedString("TaskCleanTranscodeDescription");
  117. public string Category => _localization.GetLocalizedString("TasksMaintenance");
  118. public string Key => "DeleteTranscodeFiles";
  119. public bool IsHidden => false;
  120. public bool IsEnabled => false;
  121. public bool IsLogged => true;
  122. }
  123. }