DeleteTranscodeFileTask.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Controller.IO;
  8. using MediaBrowser.Model.Globalization;
  9. using MediaBrowser.Model.IO;
  10. using MediaBrowser.Model.Tasks;
  11. using Microsoft.Extensions.Logging;
  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. /// <param name="logger">Instance of the <see cref="ILogger{DeleteTranscodeFileTask}"/> interface.</param>
  27. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  28. /// <param name="configurationManager">Instance of the <see cref="IConfigurationManager"/> interface.</param>
  29. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  30. public DeleteTranscodeFileTask(
  31. ILogger<DeleteTranscodeFileTask> logger,
  32. IFileSystem fileSystem,
  33. IConfigurationManager configurationManager,
  34. ILocalizationManager localization)
  35. {
  36. _logger = logger;
  37. _fileSystem = fileSystem;
  38. _configurationManager = configurationManager;
  39. _localization = localization;
  40. }
  41. /// <inheritdoc />
  42. public string Name => _localization.GetLocalizedString("TaskCleanTranscode");
  43. /// <inheritdoc />
  44. public string Description => _localization.GetLocalizedString("TaskCleanTranscodeDescription");
  45. /// <inheritdoc />
  46. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  47. /// <inheritdoc />
  48. public string Key => "DeleteTranscodeFiles";
  49. /// <inheritdoc />
  50. public bool IsHidden => false;
  51. /// <inheritdoc />
  52. public bool IsEnabled => true;
  53. /// <inheritdoc />
  54. public bool IsLogged => true;
  55. /// <inheritdoc />
  56. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  57. {
  58. return
  59. [
  60. new TaskTriggerInfo
  61. {
  62. Type = TaskTriggerInfoType.StartupTrigger
  63. },
  64. new TaskTriggerInfo
  65. {
  66. Type = TaskTriggerInfoType.IntervalTrigger,
  67. IntervalTicks = TimeSpan.FromHours(24).Ticks
  68. }
  69. ];
  70. }
  71. /// <inheritdoc />
  72. public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  73. {
  74. var minDateModified = DateTime.UtcNow.AddDays(-1);
  75. progress.Report(50);
  76. DeleteTempFilesFromDirectory(_configurationManager.GetTranscodePath(), minDateModified, progress, cancellationToken);
  77. return Task.CompletedTask;
  78. }
  79. /// <summary>
  80. /// Deletes the transcoded temp files from directory with a last write time less than a given date.
  81. /// </summary>
  82. /// <param name="directory">The directory.</param>
  83. /// <param name="minDateModified">The min date modified.</param>
  84. /// <param name="progress">The progress.</param>
  85. /// <param name="cancellationToken">The task cancellation token.</param>
  86. private void DeleteTempFilesFromDirectory(string directory, DateTime minDateModified, IProgress<double> progress, CancellationToken cancellationToken)
  87. {
  88. var filesToDelete = _fileSystem.GetFiles(directory, true)
  89. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  90. .ToList();
  91. var index = 0;
  92. foreach (var file in filesToDelete)
  93. {
  94. double percent = index;
  95. percent /= filesToDelete.Count;
  96. progress.Report(100 * percent);
  97. cancellationToken.ThrowIfCancellationRequested();
  98. FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger);
  99. index++;
  100. }
  101. FileSystemHelper.DeleteEmptyFolders(_fileSystem, directory, _logger);
  102. progress.Report(100);
  103. }
  104. }
  105. }