| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics.CodeAnalysis; |
| | | 4 | | using System.Net.Http.Headers; |
| | | 5 | | using System.Threading; |
| | | 6 | | using MediaBrowser.Common; |
| | | 7 | | using MediaBrowser.Common.Configuration; |
| | | 8 | | using MediaBrowser.Common.Plugins; |
| | | 9 | | using MediaBrowser.Controller.Plugins; |
| | | 10 | | using MediaBrowser.Model.Plugins; |
| | | 11 | | using MediaBrowser.Model.Serialization; |
| | | 12 | | using MediaBrowser.Providers.Plugins.MusicBrainz.Configuration; |
| | | 13 | | using MetaBrainz.MusicBrainz; |
| | | 14 | | using Microsoft.Extensions.Logging; |
| | | 15 | | |
| | | 16 | | namespace MediaBrowser.Providers.Plugins.MusicBrainz; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Plugin instance. |
| | | 20 | | /// </summary> |
| | | 21 | | public class Plugin : BasePlugin<PluginConfiguration>, IHasWebPages, IHasEmbeddedImage, IDisposable |
| | | 22 | | { |
| | | 23 | | private readonly ILogger<Plugin> _logger; |
| | 35 | 24 | | private readonly Lock _queryLock = new(); |
| | | 25 | | private Query _musicBrainzQuery; |
| | | 26 | | private bool _disposed; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="Plugin"/> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="applicationPaths">Instance of the <see cref="IApplicationPaths"/> interface.</param> |
| | | 32 | | /// <param name="xmlSerializer">Instance of the <see cref="IXmlSerializer"/> interface.</param> |
| | | 33 | | /// <param name="applicationHost">Instance of the <see cref="IApplicationHost"/> interface.</param> |
| | | 34 | | /// <param name="logger">Instance of the <see cref="ILogger{Plugin}"/> interface.</param> |
| | | 35 | | public Plugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer, IApplicationHost applicationHost, IL |
| | 35 | 36 | | : base(applicationPaths, xmlSerializer) |
| | | 37 | | { |
| | 35 | 38 | | Instance = this; |
| | 35 | 39 | | _logger = logger; |
| | | 40 | | |
| | | 41 | | // TODO: Change this to "JellyfinMusicBrainzPlugin" once we take it out of the server repo. |
| | 35 | 42 | | Query.DefaultUserAgent.Add(new ProductInfoHeaderValue(applicationHost.Name.Replace(' ', '-'), applicationHost.Ap |
| | 35 | 43 | | Query.DefaultUserAgent.Add(new ProductInfoHeaderValue($"({applicationHost.ApplicationUserAgentAddress})")); |
| | | 44 | | |
| | 35 | 45 | | ApplyServerConfig(Configuration); |
| | 35 | 46 | | Query.DelayBetweenRequests = Configuration.RateLimit; |
| | 35 | 47 | | _musicBrainzQuery = new Query(); |
| | | 48 | | |
| | 35 | 49 | | ConfigurationChanged += OnConfigurationChanged; |
| | 35 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the current plugin instance. |
| | | 54 | | /// </summary> |
| | | 55 | | public static Plugin? Instance { get; private set; } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 24 | 58 | | public override Guid Id => new Guid("8c95c4d2-e50c-4fb0-a4f3-6c06ff0f9a1a"); |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | 29 | 61 | | public override string Name => "MusicBrainz"; |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | 1 | 64 | | public override string Description => "Get artist and album metadata from any MusicBrainz server."; |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | // TODO remove when plugin removed from server. |
| | 78 | 68 | | public override string ConfigurationFileName => "Jellyfin.Plugin.MusicBrainz.xml"; |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | 21 | 71 | | public string ImageResourceName => GetType().Namespace + ".jellyfin-plugin-musicbrainz.svg"; |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets the current MusicBrainz query client. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <remarks> |
| | | 77 | | /// Always read this property anew before each request — the underlying instance is |
| | | 78 | | /// replaced when the server URL changes. Old instances are intentionally left alive |
| | | 79 | | /// so in-flight requests can finish; their unmanaged resources leak until GC. |
| | | 80 | | /// </remarks> |
| | | 81 | | public Query MusicBrainzQuery |
| | | 82 | | { |
| | | 83 | | get |
| | 0 | 84 | | { |
| | | 85 | | lock (_queryLock) |
| | | 86 | | { |
| | 0 | 87 | | return _musicBrainzQuery; |
| | | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <inheritdoc /> |
| | | 93 | | public IEnumerable<PluginPageInfo> GetPages() |
| | | 94 | | { |
| | 5 | 95 | | yield return new PluginPageInfo |
| | 5 | 96 | | { |
| | 5 | 97 | | Name = Name, |
| | 5 | 98 | | EmbeddedResourcePath = GetType().Namespace + ".Configuration.config.html" |
| | 5 | 99 | | }; |
| | 5 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <inheritdoc /> |
| | | 103 | | public void Dispose() |
| | | 104 | | { |
| | 21 | 105 | | Dispose(true); |
| | 21 | 106 | | GC.SuppressFinalize(this); |
| | 21 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Releases unmanaged and managed resources. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <param name="disposing">Whether to dispose managed resources.</param> |
| | | 113 | | protected virtual void Dispose(bool disposing) |
| | | 114 | | { |
| | 21 | 115 | | if (_disposed) |
| | | 116 | | { |
| | 0 | 117 | | return; |
| | | 118 | | } |
| | | 119 | | |
| | 21 | 120 | | if (disposing) |
| | | 121 | | { |
| | 21 | 122 | | ConfigurationChanged -= OnConfigurationChanged; |
| | | 123 | | lock (_queryLock) |
| | | 124 | | { |
| | 21 | 125 | | _musicBrainzQuery.Dispose(); |
| | 21 | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | 21 | 129 | | _disposed = true; |
| | 21 | 130 | | } |
| | | 131 | | |
| | | 132 | | [SuppressMessage("IDisposableAnalyzers.Correctness", "IDISP003:Dispose previous before re-assigning", Justification |
| | | 133 | | private void OnConfigurationChanged(object? sender, BasePluginConfiguration e) |
| | | 134 | | { |
| | 0 | 135 | | var configuration = (PluginConfiguration)e; |
| | 0 | 136 | | ApplyServerConfig(configuration); |
| | 0 | 137 | | Query.DelayBetweenRequests = configuration.RateLimit; |
| | | 138 | | |
| | | 139 | | lock (_queryLock) |
| | | 140 | | { |
| | 0 | 141 | | _musicBrainzQuery = new Query(); |
| | 0 | 142 | | } |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | private void ApplyServerConfig(PluginConfiguration configuration) |
| | | 146 | | { |
| | 35 | 147 | | if (Uri.TryCreate(configuration.Server, UriKind.Absolute, out var server)) |
| | | 148 | | { |
| | 35 | 149 | | Query.DefaultServer = server.DnsSafeHost; |
| | 35 | 150 | | Query.DefaultPort = server.Port; |
| | 35 | 151 | | Query.DefaultUrlScheme = server.Scheme; |
| | | 152 | | } |
| | | 153 | | else |
| | | 154 | | { |
| | 0 | 155 | | _logger.LogWarning("Invalid MusicBrainz server specified, falling back to official server"); |
| | 0 | 156 | | var defaultServer = new Uri(PluginConfiguration.DefaultServer); |
| | 0 | 157 | | Query.DefaultServer = defaultServer.Host; |
| | 0 | 158 | | Query.DefaultPort = defaultServer.Port; |
| | 0 | 159 | | Query.DefaultUrlScheme = defaultServer.Scheme; |
| | | 160 | | } |
| | 0 | 161 | | } |
| | | 162 | | } |