DeleteTranscodeFileTask.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /// <summary>
  14. /// Deletes all transcoding temp files.
  15. /// </summary>
  16. public class DeleteTranscodeFileTask : IScheduledTask, IConfigurableScheduledTask
  17. {
  18. private readonly ILogger<DeleteTranscodeFileTask> _logger;
  19. private readonly IConfigurationManager _configurationManager;
  20. private readonly IFileSystem _fileSystem;
  21. private readonly ILocalizationManager _localization;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="DeleteTranscodeFileTask"/> class.
  24. /// </summary>
  25. /// <param name="logger">Instance of the <see cref="ILogger{DeleteTranscodeFileTask}"/> interface.</param>
  26. /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
  27. /// <param name="configurationManager">Instance of the <see cref="IConfigurationManager"/> interface.</param>
  28. /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
  29. public DeleteTranscodeFileTask(
  30. ILogger<DeleteTranscodeFileTask> logger,
  31. IFileSystem fileSystem,
  32. IConfigurationManager configurationManager,
  33. ILocalizationManager localization)
  34. {
  35. _logger = logger;
  36. _fileSystem = fileSystem;
  37. _configurationManager = configurationManager;
  38. _localization = localization;
  39. }
  40. /// <inheritdoc />
  41. public string Name => _localization.GetLocalizedString("TaskCleanTranscode");
  42. /// <inheritdoc />
  43. public string Description => _localization.GetLocalizedString("TaskCleanTranscodeDescription");
  44. /// <inheritdoc />
  45. public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory");
  46. /// <inheritdoc />
  47. public string Key => "DeleteTranscodeFiles";
  48. /// <inheritdoc />
  49. public bool IsHidden => false;
  50. /// <inheritdoc />
  51. public bool IsEnabled => true;
  52. /// <inheritdoc />
  53. public bool IsLogged => true;
  54. /// <inheritdoc />
  55. public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
  56. {
  57. yield return new TaskTriggerInfo
  58. {
  59. Type = TaskTriggerInfoType.StartupTrigger
  60. };
  61. yield return new TaskTriggerInfo
  62. {
  63. Type = TaskTriggerInfoType.IntervalTrigger,
  64. IntervalTicks = TimeSpan.FromHours(24).Ticks
  65. };
  66. }
  67. /// <inheritdoc />
  68. public Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
  69. {
  70. var minDateModified = DateTime.UtcNow.AddDays(-1);
  71. progress.Report(50);
  72. DeleteTempFilesFromDirectory(_configurationManager.GetTranscodePath(), minDateModified, progress, cancellationToken);
  73. return Task.CompletedTask;
  74. }
  75. /// <summary>
  76. /// Deletes the transcoded temp files from directory with a last write time less than a given date.
  77. /// </summary>
  78. /// <param name="directory">The directory.</param>
  79. /// <param name="minDateModified">The min date modified.</param>
  80. /// <param name="progress">The progress.</param>
  81. /// <param name="cancellationToken">The task cancellation token.</param>
  82. private void DeleteTempFilesFromDirectory(string directory, DateTime minDateModified, IProgress<double> progress, CancellationToken cancellationToken)
  83. {
  84. var filesToDelete = _fileSystem.GetFiles(directory, true)
  85. .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified)
  86. .ToList();
  87. var index = 0;
  88. foreach (var file in filesToDelete)
  89. {
  90. double percent = index;
  91. percent /= filesToDelete.Count;
  92. progress.Report(100 * percent);
  93. cancellationToken.ThrowIfCancellationRequested();
  94. FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger);
  95. index++;
  96. }
  97. FileSystemHelper.DeleteEmptyFolders(_fileSystem, directory, _logger);
  98. progress.Report(100);
  99. }
  100. }