diff options
| author | dkanada <dkanada@users.noreply.github.com> | 2019-01-31 15:12:18 +0900 |
|---|---|---|
| committer | dkanada <dkanada@users.noreply.github.com> | 2019-02-03 19:40:55 +0900 |
| commit | 07072d9f7bf269926a4d7aad580e2e78c444a2cd (patch) | |
| tree | ffbf89de9e37812e660dfdb5a4098a71d9b82e44 /Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs | |
| parent | 7e3c45c917d707fcea048b2fe9621cf0774e3f98 (diff) | |
move all scheduled tasks and triggers into folders
Diffstat (limited to 'Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs')
| -rw-r--r-- | Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs b/Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs new file mode 100644 index 000000000..5373b4392 --- /dev/null +++ b/Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs @@ -0,0 +1,182 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Controller.Dto; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.MediaEncoding; +using MediaBrowser.Controller.Persistence; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Tasks; +using Microsoft.Extensions.Logging; + +namespace Emby.Server.Implementations.ScheduledTasks +{ + /// <summary> + /// Class ChapterImagesTask + /// </summary> + class ChapterImagesTask : IScheduledTask + { + /// <summary> + /// The _logger + /// </summary> + private readonly ILogger _logger; + /// <summary> + /// The _library manager + /// </summary> + private readonly ILibraryManager _libraryManager; + + private readonly IItemRepository _itemRepo; + + private readonly IApplicationPaths _appPaths; + + private readonly IEncodingManager _encodingManager; + private readonly IFileSystem _fileSystem; + + /// <summary> + /// Initializes a new instance of the <see cref="ChapterImagesTask" /> class. + /// </summary> + public ChapterImagesTask(ILoggerFactory loggerFactory, ILibraryManager libraryManager, IItemRepository itemRepo, IApplicationPaths appPaths, IEncodingManager encodingManager, IFileSystem fileSystem) + { + _logger = loggerFactory.CreateLogger(GetType().Name); + _libraryManager = libraryManager; + _itemRepo = itemRepo; + _appPaths = appPaths; + _encodingManager = encodingManager; + _fileSystem = fileSystem; + } + + /// <summary> + /// Creates the triggers that define when the task will run + /// </summary> + public IEnumerable<TaskTriggerInfo> GetDefaultTriggers() + { + return new[] { + + new TaskTriggerInfo + { + Type = TaskTriggerInfo.TriggerDaily, + TimeOfDayTicks = TimeSpan.FromHours(2).Ticks, + MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks + } + }; + } + + public string Key => "RefreshChapterImages"; + + /// <summary> + /// Returns the task to be executed + /// </summary> + /// <param name="cancellationToken">The cancellation token.</param> + /// <param name="progress">The progress.</param> + /// <returns>Task.</returns> + public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress) + { + var videos = _libraryManager.GetItemList(new InternalItemsQuery + { + MediaTypes = new[] { MediaType.Video }, + IsFolder = false, + Recursive = true, + DtoOptions = new DtoOptions(false) + { + EnableImages = false + }, + SourceTypes = new SourceType[] { SourceType.Library }, + HasChapterImages = false, + IsVirtualItem = false + + }) + .OfType<Video>() + .ToList(); + + var numComplete = 0; + + var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt"); + + List<string> previouslyFailedImages; + + if (File.Exists(failHistoryPath)) + { + try + { + previouslyFailedImages = File.ReadAllText(failHistoryPath) + .Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries) + .ToList(); + } + catch (IOException) + { + previouslyFailedImages = new List<string>(); + } + } + else + { + previouslyFailedImages = new List<string>(); + } + + var directoryService = new DirectoryService(_logger, _fileSystem); + + foreach (var video in videos) + { + cancellationToken.ThrowIfCancellationRequested(); + + var key = video.Path + video.DateModified.Ticks; + + var extract = !previouslyFailedImages.Contains(key, StringComparer.OrdinalIgnoreCase); + + try + { + var chapters = _itemRepo.GetChapters(video); + + var success = await _encodingManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false); + + if (!success) + { + previouslyFailedImages.Add(key); + + var parentPath = Path.GetDirectoryName(failHistoryPath); + + Directory.CreateDirectory(parentPath); + + string text = string.Join("|", previouslyFailedImages); + File.WriteAllText(failHistoryPath, text); + } + + numComplete++; + double percent = numComplete; + percent /= videos.Count; + + progress.Report(100 * percent); + } + catch (ObjectDisposedException) + { + //TODO Investigate and properly fix. + break; + } + } + } + + /// <summary> + /// Gets the name of the task + /// </summary> + /// <value>The name.</value> + public string Name => "Chapter image extraction"; + + /// <summary> + /// Gets the description. + /// </summary> + /// <value>The description.</value> + public string Description => "Creates thumbnails for videos that have chapters."; + + /// <summary> + /// Gets the category. + /// </summary> + /// <value>The category.</value> + public string Category => "Library"; + } +} |
