Use IHostedService for NFO user data
This commit is contained in:
parent
24b4d02596
commit
690e603b90
|
@ -19,6 +19,7 @@ using Jellyfin.Server.Infrastructure;
|
|||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
using MediaBrowser.Controller.Extensions;
|
||||
using MediaBrowser.XbmcMetadata;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
@ -128,6 +129,7 @@ namespace Jellyfin.Server
|
|||
|
||||
services.AddHostedService<AutoDiscoveryHost>();
|
||||
services.AddHostedService<PortForwardingHost>();
|
||||
services.AddHostedService<NfoUserDataSaver>();
|
||||
services.AddHostedService<LibraryChangedNotifier>();
|
||||
services.AddHostedService<UserDataChangeNotifier>();
|
||||
services.AddHostedService<RecordingNotifier>();
|
||||
|
|
|
@ -1,78 +0,0 @@
|
|||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Controller.Entities;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Plugins;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.XbmcMetadata.Configuration;
|
||||
using MediaBrowser.XbmcMetadata.Savers;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MediaBrowser.XbmcMetadata
|
||||
{
|
||||
public sealed class EntryPoint : IServerEntryPoint
|
||||
{
|
||||
private readonly IUserDataManager _userDataManager;
|
||||
private readonly ILogger<EntryPoint> _logger;
|
||||
private readonly IProviderManager _providerManager;
|
||||
private readonly IConfigurationManager _config;
|
||||
|
||||
public EntryPoint(
|
||||
IUserDataManager userDataManager,
|
||||
ILogger<EntryPoint> logger,
|
||||
IProviderManager providerManager,
|
||||
IConfigurationManager config)
|
||||
{
|
||||
_userDataManager = userDataManager;
|
||||
_logger = logger;
|
||||
_providerManager = providerManager;
|
||||
_config = config;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task RunAsync()
|
||||
{
|
||||
_userDataManager.UserDataSaved += OnUserDataSaved;
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private void OnUserDataSaved(object? sender, UserDataSaveEventArgs e)
|
||||
{
|
||||
if (e.SaveReason == UserDataSaveReason.PlaybackFinished || e.SaveReason == UserDataSaveReason.TogglePlayed || e.SaveReason == UserDataSaveReason.UpdateUserRating)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(_config.GetNfoConfiguration().UserId))
|
||||
{
|
||||
_ = SaveMetadataForItemAsync(e.Item, ItemUpdateType.MetadataDownload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
_userDataManager.UserDataSaved -= OnUserDataSaved;
|
||||
}
|
||||
|
||||
private async Task SaveMetadataForItemAsync(BaseItem item, ItemUpdateType updateReason)
|
||||
{
|
||||
if (!item.IsFileProtocol || !item.SupportsLocalMetadata)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _providerManager.SaveMetadataAsync(item, updateReason, new[] { BaseNfoSaver.SaverName }).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error saving metadata for {Path}", item.Path ?? item.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
87
MediaBrowser.XbmcMetadata/NfoUserDataSaver.cs
Normal file
87
MediaBrowser.XbmcMetadata/NfoUserDataSaver.cs
Normal file
|
@ -0,0 +1,87 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Providers;
|
||||
using MediaBrowser.Model.Entities;
|
||||
using MediaBrowser.XbmcMetadata.Configuration;
|
||||
using MediaBrowser.XbmcMetadata.Savers;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MediaBrowser.XbmcMetadata;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="IHostedService"/> responsible for updating NFO files' user data.
|
||||
/// </summary>
|
||||
public sealed class NfoUserDataSaver : IHostedService
|
||||
{
|
||||
private readonly ILogger<NfoUserDataSaver> _logger;
|
||||
private readonly IConfigurationManager _config;
|
||||
private readonly IUserDataManager _userDataManager;
|
||||
private readonly IProviderManager _providerManager;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="NfoUserDataSaver"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The <see cref="ILogger"/>.</param>
|
||||
/// <param name="config">The <see cref="IConfigurationManager"/>.</param>
|
||||
/// <param name="userDataManager">The <see cref="IUserDataManager"/>.</param>
|
||||
/// <param name="providerManager">The <see cref="IProviderManager"/>.</param>
|
||||
public NfoUserDataSaver(
|
||||
ILogger<NfoUserDataSaver> logger,
|
||||
IConfigurationManager config,
|
||||
IUserDataManager userDataManager,
|
||||
IProviderManager providerManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_config = config;
|
||||
_userDataManager = userDataManager;
|
||||
_providerManager = providerManager;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_userDataManager.UserDataSaved += OnUserDataSaved;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_userDataManager.UserDataSaved -= OnUserDataSaved;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async void OnUserDataSaved(object? sender, UserDataSaveEventArgs e)
|
||||
{
|
||||
if (e.SaveReason is not (UserDataSaveReason.PlaybackFinished
|
||||
or UserDataSaveReason.TogglePlayed or UserDataSaveReason.UpdateUserRating))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_config.GetNfoConfiguration().UserId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var item = e.Item;
|
||||
if (!item.IsFileProtocol || !item.SupportsLocalMetadata)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _providerManager.SaveMetadataAsync(item, ItemUpdateType.MetadataDownload, [BaseNfoSaver.SaverName])
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error saving metadata for {Path}", item.Path ?? item.Name);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user