From 5c9f70c3752bd7297cb85bdc7ce748363a16ad8b Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Wed, 30 Apr 2025 09:29:13 +0200 Subject: Cleanup Tasks and Validators --- .../Tasks/DeleteTranscodeFileTask.cs | 171 ++++++++++----------- 1 file changed, 84 insertions(+), 87 deletions(-) (limited to 'Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteTranscodeFileTask.cs') diff --git a/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteTranscodeFileTask.cs b/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteTranscodeFileTask.cs index d0896cc81..9cc2cc512 100644 --- a/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteTranscodeFileTask.cs +++ b/Emby.Server.Implementations/ScheduledTasks/Tasks/DeleteTranscodeFileTask.cs @@ -10,118 +10,115 @@ using MediaBrowser.Model.IO; using MediaBrowser.Model.Tasks; using Microsoft.Extensions.Logging; -namespace Emby.Server.Implementations.ScheduledTasks.Tasks +namespace Emby.Server.Implementations.ScheduledTasks.Tasks; + +/// +/// Deletes all transcoding temp files. +/// +public class DeleteTranscodeFileTask : IScheduledTask, IConfigurableScheduledTask { + private readonly ILogger _logger; + private readonly IConfigurationManager _configurationManager; + private readonly IFileSystem _fileSystem; + private readonly ILocalizationManager _localization; + /// - /// Deletes all transcoding temp files. + /// Initializes a new instance of the class. /// - public class DeleteTranscodeFileTask : IScheduledTask, IConfigurableScheduledTask + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + /// Instance of the interface. + public DeleteTranscodeFileTask( + ILogger logger, + IFileSystem fileSystem, + IConfigurationManager configurationManager, + ILocalizationManager localization) { - private readonly ILogger _logger; - private readonly IConfigurationManager _configurationManager; - private readonly IFileSystem _fileSystem; - private readonly ILocalizationManager _localization; - - /// - /// Initializes a new instance of the class. - /// - /// Instance of the interface. - /// Instance of the interface. - /// Instance of the interface. - /// Instance of the interface. - public DeleteTranscodeFileTask( - ILogger logger, - IFileSystem fileSystem, - IConfigurationManager configurationManager, - ILocalizationManager localization) - { - _logger = logger; - _fileSystem = fileSystem; - _configurationManager = configurationManager; - _localization = localization; - } + _logger = logger; + _fileSystem = fileSystem; + _configurationManager = configurationManager; + _localization = localization; + } - /// - public string Name => _localization.GetLocalizedString("TaskCleanTranscode"); + /// + public string Name => _localization.GetLocalizedString("TaskCleanTranscode"); - /// - public string Description => _localization.GetLocalizedString("TaskCleanTranscodeDescription"); + /// + public string Description => _localization.GetLocalizedString("TaskCleanTranscodeDescription"); - /// - public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory"); + /// + public string Category => _localization.GetLocalizedString("TasksMaintenanceCategory"); - /// - public string Key => "DeleteTranscodeFiles"; + /// + public string Key => "DeleteTranscodeFiles"; - /// - public bool IsHidden => false; + /// + public bool IsHidden => false; - /// - public bool IsEnabled => true; + /// + public bool IsEnabled => true; - /// - public bool IsLogged => true; + /// + public bool IsLogged => true; - /// - public IEnumerable GetDefaultTriggers() + /// + public IEnumerable GetDefaultTriggers() + { + yield return new TaskTriggerInfo { - return - [ - new TaskTriggerInfo - { - Type = TaskTriggerInfoType.StartupTrigger - }, - new TaskTriggerInfo - { - Type = TaskTriggerInfoType.IntervalTrigger, - IntervalTicks = TimeSpan.FromHours(24).Ticks - } - ]; - } + Type = TaskTriggerInfoType.StartupTrigger + }; - /// - public Task ExecuteAsync(IProgress progress, CancellationToken cancellationToken) + yield return new TaskTriggerInfo { - var minDateModified = DateTime.UtcNow.AddDays(-1); - progress.Report(50); - - DeleteTempFilesFromDirectory(_configurationManager.GetTranscodePath(), minDateModified, progress, cancellationToken); + Type = TaskTriggerInfoType.IntervalTrigger, + IntervalTicks = TimeSpan.FromHours(24).Ticks + }; + } - return Task.CompletedTask; - } + /// + public Task ExecuteAsync(IProgress progress, CancellationToken cancellationToken) + { + var minDateModified = DateTime.UtcNow.AddDays(-1); + progress.Report(50); - /// - /// Deletes the transcoded temp files from directory with a last write time less than a given date. - /// - /// The directory. - /// The min date modified. - /// The progress. - /// The task cancellation token. - private void DeleteTempFilesFromDirectory(string directory, DateTime minDateModified, IProgress progress, CancellationToken cancellationToken) - { - var filesToDelete = _fileSystem.GetFiles(directory, true) - .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified) - .ToList(); + DeleteTempFilesFromDirectory(_configurationManager.GetTranscodePath(), minDateModified, progress, cancellationToken); - var index = 0; + return Task.CompletedTask; + } - foreach (var file in filesToDelete) - { - double percent = index; - percent /= filesToDelete.Count; + /// + /// Deletes the transcoded temp files from directory with a last write time less than a given date. + /// + /// The directory. + /// The min date modified. + /// The progress. + /// The task cancellation token. + private void DeleteTempFilesFromDirectory(string directory, DateTime minDateModified, IProgress progress, CancellationToken cancellationToken) + { + var filesToDelete = _fileSystem.GetFiles(directory, true) + .Where(f => _fileSystem.GetLastWriteTimeUtc(f) < minDateModified) + .ToList(); - progress.Report(100 * percent); + var index = 0; - cancellationToken.ThrowIfCancellationRequested(); + foreach (var file in filesToDelete) + { + double percent = index; + percent /= filesToDelete.Count; - FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger); + progress.Report(100 * percent); - index++; - } + cancellationToken.ThrowIfCancellationRequested(); - FileSystemHelper.DeleteEmptyFolders(_fileSystem, directory, _logger); + FileSystemHelper.DeleteFile(_fileSystem, file.FullName, _logger); - progress.Report(100); + index++; } + + FileSystemHelper.DeleteEmptyFolders(_fileSystem, directory, _logger); + + progress.Report(100); } } -- cgit v1.2.3