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