Files
jellyfin-jellyfin-1/Emby.Server.Implementations/ScheduledTasks/Tasks/ChapterImagesTask.cs

169 lines
5.7 KiB
C#
Raw Normal View History

using System;
2013-02-20 20:33:05 -05:00
using System.Collections.Generic;
using System.IO;
2013-02-20 20:33:05 -05:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Data.Enums;
2021-12-20 13:31:07 +01:00
using Jellyfin.Extensions;
using MediaBrowser.Common.Configuration;
2024-10-09 10:36:08 +00:00
using MediaBrowser.Controller.Chapters;
2017-05-21 03:25:49 -04:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
2021-04-17 11:37:55 +01:00
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Tasks;
2022-11-23 15:58:11 +01:00
using Microsoft.Extensions.Logging;
2013-02-20 20:33:05 -05:00
2025-04-30 09:29:13 +02:00
namespace Emby.Server.Implementations.ScheduledTasks.Tasks;
/// <summary>
/// Class ChapterImagesTask.
/// </summary>
public class ChapterImagesTask : IScheduledTask
2013-02-20 20:33:05 -05:00
{
2025-04-30 09:29:13 +02:00
private readonly ILogger<ChapterImagesTask> _logger;
private readonly ILibraryManager _libraryManager;
private readonly IApplicationPaths _appPaths;
private readonly IChapterManager _chapterManager;
private readonly IFileSystem _fileSystem;
private readonly ILocalizationManager _localization;
2013-02-23 02:57:11 -05:00
/// <summary>
2025-04-30 09:29:13 +02:00
/// Initializes a new instance of the <see cref="ChapterImagesTask" /> class.
2013-02-23 02:57:11 -05:00
/// </summary>
2025-04-30 09:29:13 +02:00
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
/// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param>
/// <param name="appPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param>
/// <param name="chapterManager">Instance of the <see cref="IChapterManager"/> interface.</param>
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
/// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param>
public ChapterImagesTask(
ILogger<ChapterImagesTask> logger,
ILibraryManager libraryManager,
IApplicationPaths appPaths,
IChapterManager chapterManager,
IFileSystem fileSystem,
ILocalizationManager localization)
2013-02-20 20:33:05 -05:00
{
2025-04-30 09:29:13 +02:00
_logger = logger;
_libraryManager = libraryManager;
_appPaths = appPaths;
_chapterManager = chapterManager;
_fileSystem = fileSystem;
_localization = localization;
}
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Name => _localization.GetLocalizedString("TaskRefreshChapterImages");
2021-07-06 00:01:33 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Description => _localization.GetLocalizedString("TaskRefreshChapterImagesDescription");
2021-07-06 00:01:33 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Category => _localization.GetLocalizedString("TasksLibraryCategory");
2021-07-06 00:01:33 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public string Key => "RefreshChapterImages";
2021-07-06 00:01:33 +02:00
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
yield return new TaskTriggerInfo
2013-02-20 20:33:05 -05:00
{
2025-04-30 09:29:13 +02:00
Type = TaskTriggerInfoType.DailyTrigger,
TimeOfDayTicks = TimeSpan.FromHours(2).Ticks,
MaxRuntimeTicks = TimeSpan.FromHours(4).Ticks
};
}
2025-04-30 09:29:13 +02:00
/// <inheritdoc />
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
var videos = _libraryManager.GetItemList(new InternalItemsQuery
2013-02-20 20:33:05 -05:00
{
2025-04-30 09:29:13 +02:00
MediaTypes = [MediaType.Video],
IsFolder = false,
Recursive = true,
DtoOptions = new DtoOptions(false)
2016-05-09 00:56:41 -04:00
{
2025-04-30 09:29:13 +02:00
EnableImages = false
},
SourceTypes = [SourceType.Library],
IsVirtualItem = false
})
.OfType<Video>()
.ToList();
2013-02-20 20:33:05 -05:00
2025-04-30 09:29:13 +02:00
var numComplete = 0;
2013-02-20 20:33:05 -05:00
2025-04-30 09:29:13 +02:00
var failHistoryPath = Path.Combine(_appPaths.CachePath, "chapter-failures.txt");
2025-04-30 09:29:13 +02:00
List<string> previouslyFailedImages;
2025-04-30 09:29:13 +02:00
if (File.Exists(failHistoryPath))
{
try
{
2025-04-30 09:29:13 +02:00
previouslyFailedImages = (await File.ReadAllTextAsync(failHistoryPath, cancellationToken).ConfigureAwait(false))
.Split('|', StringSplitOptions.RemoveEmptyEntries)
.ToList();
}
2025-04-30 09:29:13 +02:00
catch (IOException)
2013-09-25 11:11:23 -04:00
{
previouslyFailedImages = [];
2013-09-25 11:11:23 -04:00
}
2025-04-30 09:29:13 +02:00
}
else
{
previouslyFailedImages = [];
}
2025-04-30 09:29:13 +02:00
var directoryService = new DirectoryService(_fileSystem);
2013-04-15 14:45:58 -04:00
2025-04-30 09:29:13 +02:00
foreach (var video in videos)
{
cancellationToken.ThrowIfCancellationRequested();
2025-04-30 09:29:13 +02:00
var key = video.Path + video.DateModified.Ticks;
2025-04-30 09:29:13 +02:00
var extract = !previouslyFailedImages.Contains(key, StringComparison.OrdinalIgnoreCase);
2014-02-20 11:37:41 -05:00
2025-04-30 09:29:13 +02:00
try
{
var chapters = _chapterManager.GetChapters(video.Id);
2013-06-03 22:02:49 -04:00
2025-04-30 09:29:13 +02:00
var success = await _chapterManager.RefreshChapterImages(video, directoryService, chapters, extract, true, cancellationToken).ConfigureAwait(false);
2013-06-03 22:02:49 -04:00
2025-04-30 09:29:13 +02:00
if (!success)
{
previouslyFailedImages.Add(key);
2013-02-20 20:33:05 -05:00
2025-04-30 09:29:13 +02:00
var parentPath = Path.GetDirectoryName(failHistoryPath);
if (parentPath is not null)
{
Directory.CreateDirectory(parentPath);
2016-03-17 12:39:39 -04:00
}
2025-04-30 09:29:13 +02:00
string text = string.Join('|', previouslyFailedImages);
await File.WriteAllTextAsync(failHistoryPath, text, cancellationToken).ConfigureAwait(false);
2016-03-17 12:39:39 -04:00
}
2025-04-30 09:29:13 +02:00
numComplete++;
double percent = numComplete;
percent /= videos.Count;
progress.Report(100 * percent);
}
catch (ObjectDisposedException ex)
{
// TODO Investigate and properly fix.
_logger.LogError(ex, "Object Disposed");
break;
2013-04-15 14:45:58 -04:00
}
2013-02-20 20:33:05 -05:00
}
}
}