DeleteTranscodeFileTask.cs 4.8 KB

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