DeleteTranscodingTempTask.cs 5.1 KB

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