Files
blackBarRemover/JellyfinPlugin/Tasks/LibraryForceRescanTask.cs
T
karol cb16bf0074
Build and Release Plugin / release (push) Successful in 31s
fix(server-mode): handle quote stripping in wrapper and empty crop checks
2026-09-17 19:54:01 +02:00

89 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Tasks;
using Microsoft.Extensions.Logging;
namespace JellyfinPlugin.Tasks
{
public class LibraryForceRescanTask : IScheduledTask
{
private readonly ILogger<LibraryForceRescanTask> _logger;
private readonly ILibraryManager _libraryManager;
public LibraryForceRescanTask(ILogger<LibraryForceRescanTask> logger, ILibraryManager libraryManager)
{
_logger = logger;
_libraryManager = libraryManager;
}
public string Name => "Force Rescan All Black Bars (Black Bar Remover)";
public string Key => "BlackBarForceRescanTask";
public string Description => "Forces a rescan of all files, overwriting any previously saved crop data. Use this if you changed the detection limit.";
public string Category => "Library";
public async Task ExecuteAsync(IProgress<double> progress, CancellationToken cancellationToken)
{
var config = Plugin.Instance!.Configuration;
if (!config.IsEnabled)
{
_logger.LogInformation("Plugin is disabled. Skipping.");
return;
}
var items = _libraryManager.RootFolder.GetRecursiveChildren()
.Where(x => x is Movie || x is Episode)
.ToList();
int total = items.Count;
int processed = 0;
foreach (var item in items)
{
cancellationToken.ThrowIfCancellationRequested();
// Wymuszamy skanowanie, więc nie sprawdzamy czy ma już BlackBarCrop
var path = item.Path;
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
try
{
string cropData = await CropHelper.DetectCropAsync(path, config.CropDetectLimit);
if (!string.IsNullOrEmpty(cropData))
{
if (item.ProviderIds == null)
{
item.ProviderIds = new Dictionary<string, string>();
}
item.ProviderIds["BlackBarCrop"] = cropData;
await _libraryManager.UpdateItemAsync(item, item, ItemUpdateType.MetadataEdit, cancellationToken);
_logger.LogInformation("Found black bars {Crop} in {Path}", cropData, path);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error scanning file {Path}", path);
}
}
processed++;
progress.Report((processed / (double)total) * 100);
}
}
public IEnumerable<TaskTriggerInfo> GetDefaultTriggers()
{
return Array.Empty<TaskTriggerInfo>();
}
}
}