DeleteTranscodeFileTask.cs 5.0 KB

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