From 4390e2f7108f24f89a1bf7ef9f6f7c9c57b4f221 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 23 Apr 2013 15:17:21 -0400 Subject: [PATCH] #35 - Make IBN path configurable --- .../Configuration/BaseConfigurationManager.cs | 14 ++++-- .../IServerApplicationPaths.cs | 2 +- .../Providers/BaseProviderInfo.cs | 5 ++ .../Providers/ImagesByNameProvider.cs | 46 ++++++++++++++++--- .../Configuration/ServerConfiguration.cs | 6 +++ .../ServerConfigurationManager.cs | 46 ++++++++++++++++++- .../Library/LibraryManager.cs | 31 ++++++++----- .../ServerApplicationPaths.cs | 28 +++++++---- MediaBrowser.ServerApplication/App.xaml.cs | 25 ---------- 9 files changed, 147 insertions(+), 56 deletions(-) diff --git a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs index 2f50f5f7a..317a288ff 100644 --- a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs +++ b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs @@ -99,8 +99,16 @@ namespace MediaBrowser.Common.Implementations.Configuration lock (_configurationSaveLock) { XmlSerializer.SerializeToFile(CommonConfiguration, CommonApplicationPaths.SystemConfigurationFilePath); - } - + } + + OnConfigurationUpdated(); + } + + /// + /// Called when [configuration updated]. + /// + protected virtual void OnConfigurationUpdated() + { EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty, Logger); } @@ -109,7 +117,7 @@ namespace MediaBrowser.Common.Implementations.Configuration /// /// The new configuration. /// newConfiguration - public void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration) + public virtual void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration) { if (newConfiguration == null) { diff --git a/MediaBrowser.Controller/IServerApplicationPaths.cs b/MediaBrowser.Controller/IServerApplicationPaths.cs index 1898f862c..09f2f5e8e 100644 --- a/MediaBrowser.Controller/IServerApplicationPaths.cs +++ b/MediaBrowser.Controller/IServerApplicationPaths.cs @@ -26,7 +26,7 @@ namespace MediaBrowser.Controller /// Gets the path to the Images By Name directory /// /// The images by name path. - string ImagesByNamePath { get; } + string ItemsByNamePath { get; set; } /// /// Gets the path to the People directory diff --git a/MediaBrowser.Controller/Providers/BaseProviderInfo.cs b/MediaBrowser.Controller/Providers/BaseProviderInfo.cs index 5a72491c1..23779d382 100644 --- a/MediaBrowser.Controller/Providers/BaseProviderInfo.cs +++ b/MediaBrowser.Controller/Providers/BaseProviderInfo.cs @@ -27,6 +27,11 @@ namespace MediaBrowser.Controller.Providers /// /// The provider version. public string ProviderVersion { get; set; } + /// + /// Gets or sets the custom data. + /// + /// The custom data. + public string CustomData { get; set; } } /// diff --git a/MediaBrowser.Controller/Providers/ImagesByNameProvider.cs b/MediaBrowser.Controller/Providers/ImagesByNameProvider.cs index 1c4c783c4..98b9e9315 100644 --- a/MediaBrowser.Controller/Providers/ImagesByNameProvider.cs +++ b/MediaBrowser.Controller/Providers/ImagesByNameProvider.cs @@ -1,4 +1,6 @@ -using MediaBrowser.Controller.Configuration; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.IO; using System; @@ -41,6 +43,23 @@ namespace MediaBrowser.Controller.Providers } } + /// + /// Needses the refresh internal. + /// + /// The item. + /// The provider info. + /// true if XXXX, false otherwise + protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) + { + // Force a refresh if the IBN path changed + if (!string.Equals(providerInfo.CustomData, ConfigurationManager.ApplicationPaths.ItemsByNamePath, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + + return base.NeedsRefreshInternal(item, providerInfo); + } + /// /// Gets a value indicating whether [refresh on file system stamp change]. /// @@ -80,9 +99,25 @@ namespace MediaBrowser.Controller.Providers } /// - /// The us culture + /// Fetches metadata and returns true or false indicating if any work that requires persistence was done /// - private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); + /// The item. + /// if set to true [force]. + /// The cancellation token. + /// Task{System.Boolean}. + public override async Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken) + { + var result = await base.FetchAsync(item, force, cancellationToken).ConfigureAwait(false); + + BaseProviderInfo data; + + if (item.ProviderData.TryGetValue(Id, out data)) + { + data.CustomData = ConfigurationManager.ApplicationPaths.ItemsByNamePath; + } + + return result; + } /// /// Gets the location. @@ -91,10 +126,7 @@ namespace MediaBrowser.Controller.Providers /// System.String. protected string GetLocation(BaseItem item) { - var invalid = Path.GetInvalidFileNameChars(); - - var name = item.Name ?? string.Empty; - name = invalid.Aggregate(name, (current, c) => current.Replace(c.ToString(UsCulture), string.Empty)); + var name = FileSystem.GetValidFilename(item.Name); return Path.Combine(ConfigurationManager.ApplicationPaths.GeneralPath, name); } diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index 554e7afd1..522b2a4f8 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -39,6 +39,12 @@ namespace MediaBrowser.Model.Configuration /// The weather location. public string WeatherLocation { get; set; } + /// + /// Gets or sets the item by name path. + /// + /// The item by name path. + public string ItemsByNamePath { get; set; } + /// /// Gets or sets the weather unit to use when displaying weather /// diff --git a/MediaBrowser.Server.Implementations/Configuration/ServerConfigurationManager.cs b/MediaBrowser.Server.Implementations/Configuration/ServerConfigurationManager.cs index cb4c5a6cd..bdc4fcf45 100644 --- a/MediaBrowser.Server.Implementations/Configuration/ServerConfigurationManager.cs +++ b/MediaBrowser.Server.Implementations/Configuration/ServerConfigurationManager.cs @@ -1,4 +1,5 @@ -using MediaBrowser.Common.Configuration; +using System.IO; +using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Implementations.Configuration; using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; @@ -23,6 +24,7 @@ namespace MediaBrowser.Server.Implementations.Configuration public ServerConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer) : base(applicationPaths, logManager, xmlSerializer) { + UpdateItemsByNamePath(); } /// @@ -51,5 +53,47 @@ namespace MediaBrowser.Server.Implementations.Configuration { get { return (ServerConfiguration)CommonConfiguration; } } + + /// + /// Called when [configuration updated]. + /// + protected override void OnConfigurationUpdated() + { + UpdateItemsByNamePath(); + + base.OnConfigurationUpdated(); + } + + /// + /// Updates the items by name path. + /// + private void UpdateItemsByNamePath() + { + if (!string.IsNullOrEmpty(Configuration.ItemsByNamePath)) + { + ApplicationPaths.ItemsByNamePath = Configuration.ItemsByNamePath; + } + } + + /// + /// Replaces the configuration. + /// + /// The new configuration. + /// + public override void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration) + { + var newConfig = (ServerConfiguration) newConfiguration; + + if (!string.Equals(Configuration.ItemsByNamePath, newConfig.ItemsByNamePath)) + { + // Validate + if (!Directory.Exists(newConfig.ItemsByNamePath)) + { + throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newConfig.ItemsByNamePath)); + } + } + + base.ReplaceConfiguration(newConfiguration); + } } } diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 315abd49d..132dca4e2 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -212,9 +212,11 @@ namespace MediaBrowser.Server.Implementations.Library private bool _internetProvidersEnabled; private bool _peopleImageFetchingEnabled; + private string _itemsByNamePath; private void RecordConfigurationValues(ServerConfiguration configuration) { + _itemsByNamePath = ConfigurationManager.ApplicationPaths.ItemsByNamePath; _internetProvidersEnabled = configuration.EnableInternetProviders; _peopleImageFetchingEnabled = configuration.InternetProviderExcludeTypes == null || !configuration.InternetProviderExcludeTypes.Contains(typeof(Person).Name, StringComparer.OrdinalIgnoreCase); } @@ -239,6 +241,13 @@ namespace MediaBrowser.Server.Implementations.Library refreshPeopleAfterUpdate = newConfigurationFetchesPeopleImages && !_peopleImageFetchingEnabled; } + var ibnPathChanged = !string.Equals(_itemsByNamePath, ConfigurationManager.ApplicationPaths.ItemsByNamePath); + + if (ibnPathChanged) + { + _itemsByName.Clear(); + } + RecordConfigurationValues(config); Task.Run(() => @@ -528,7 +537,7 @@ namespace MediaBrowser.Server.Implementations.Library /// Task{Person}. private Task GetPerson(string name, CancellationToken cancellationToken, bool allowSlowProviders = false, bool forceCreation = false) { - return GetImagesByNameItem(ConfigurationManager.ApplicationPaths.PeoplePath, name, cancellationToken, allowSlowProviders, forceCreation); + return GetItemByName(ConfigurationManager.ApplicationPaths.PeoplePath, name, cancellationToken, allowSlowProviders, forceCreation); } /// @@ -539,7 +548,7 @@ namespace MediaBrowser.Server.Implementations.Library /// Task{Studio}. public Task GetStudio(string name, bool allowSlowProviders = false) { - return GetImagesByNameItem(ConfigurationManager.ApplicationPaths.StudioPath, name, CancellationToken.None, allowSlowProviders); + return GetItemByName(ConfigurationManager.ApplicationPaths.StudioPath, name, CancellationToken.None, allowSlowProviders); } /// @@ -550,7 +559,7 @@ namespace MediaBrowser.Server.Implementations.Library /// Task{Genre}. public Task GetGenre(string name, bool allowSlowProviders = false) { - return GetImagesByNameItem(ConfigurationManager.ApplicationPaths.GenrePath, name, CancellationToken.None, allowSlowProviders); + return GetItemByName(ConfigurationManager.ApplicationPaths.GenrePath, name, CancellationToken.None, allowSlowProviders); } /// @@ -574,7 +583,7 @@ namespace MediaBrowser.Server.Implementations.Library /// Task{Artist}. private Task GetArtist(string name, CancellationToken cancellationToken, bool allowSlowProviders = false, bool forceCreation = false) { - return GetImagesByNameItem(ConfigurationManager.ApplicationPaths.ArtistsPath, name, cancellationToken, allowSlowProviders, forceCreation); + return GetItemByName(ConfigurationManager.ApplicationPaths.ArtistsPath, name, cancellationToken, allowSlowProviders, forceCreation); } /// @@ -596,13 +605,13 @@ namespace MediaBrowser.Server.Implementations.Library throw new ArgumentOutOfRangeException(); } - return GetImagesByNameItem(ConfigurationManager.ApplicationPaths.YearPath, value.ToString(UsCulture), CancellationToken.None, allowSlowProviders); + return GetItemByName(ConfigurationManager.ApplicationPaths.YearPath, value.ToString(UsCulture), CancellationToken.None, allowSlowProviders); } /// /// The images by name item cache /// - private readonly ConcurrentDictionary _imagesByNameItemCache = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + private readonly ConcurrentDictionary _itemsByName = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); /// /// Generically retrieves an IBN item @@ -616,7 +625,7 @@ namespace MediaBrowser.Server.Implementations.Library /// Task{``0}. /// /// - private async Task GetImagesByNameItem(string path, string name, CancellationToken cancellationToken, bool allowSlowProviders = true, bool forceCreation = false) + private async Task GetItemByName(string path, string name, CancellationToken cancellationToken, bool allowSlowProviders = true, bool forceCreation = false) where T : BaseItem, new() { if (string.IsNullOrEmpty(path)) @@ -633,11 +642,11 @@ namespace MediaBrowser.Server.Implementations.Library BaseItem obj; - if (forceCreation || !_imagesByNameItemCache.TryGetValue(key, out obj)) + if (forceCreation || !_itemsByName.TryGetValue(key, out obj)) { - obj = await CreateImagesByNameItem(path, name, cancellationToken, allowSlowProviders).ConfigureAwait(false); + obj = await CreateItemByName(path, name, cancellationToken, allowSlowProviders).ConfigureAwait(false); - _imagesByNameItemCache.AddOrUpdate(key, obj, (keyName, oldValue) => obj); + _itemsByName.AddOrUpdate(key, obj, (keyName, oldValue) => obj); } return obj as T; @@ -653,7 +662,7 @@ namespace MediaBrowser.Server.Implementations.Library /// if set to true [allow slow providers]. /// Task{``0}. /// Path not created: + path - private async Task CreateImagesByNameItem(string path, string name, CancellationToken cancellationToken, bool allowSlowProviders = true) + private async Task CreateItemByName(string path, string name, CancellationToken cancellationToken, bool allowSlowProviders = true) where T : BaseItem, new() { cancellationToken.ThrowIfCancellationRequested(); diff --git a/MediaBrowser.Server.Implementations/ServerApplicationPaths.cs b/MediaBrowser.Server.Implementations/ServerApplicationPaths.cs index a723eb38e..6d345a99c 100644 --- a/MediaBrowser.Server.Implementations/ServerApplicationPaths.cs +++ b/MediaBrowser.Server.Implementations/ServerApplicationPaths.cs @@ -106,7 +106,7 @@ namespace MediaBrowser.Server.Implementations /// Gets the path to the Images By Name directory /// /// The images by name path. - public string ImagesByNamePath + public string ItemsByNamePath { get { @@ -121,6 +121,18 @@ namespace MediaBrowser.Server.Implementations return _ibnPath; } + set + { + _ibnPath = value; + + _peoplePath = null; + _studioPath = null; + _genrePath = null; + _yearPath = null; + _musicArtistsPath = null; + _generalPath = null; + _ratingsPath = null; + } } /// @@ -137,7 +149,7 @@ namespace MediaBrowser.Server.Implementations { if (_peoplePath == null) { - _peoplePath = Path.Combine(ImagesByNamePath, "People"); + _peoplePath = Path.Combine(ItemsByNamePath, "People"); if (!Directory.Exists(_peoplePath)) { Directory.CreateDirectory(_peoplePath); @@ -162,7 +174,7 @@ namespace MediaBrowser.Server.Implementations { if (_genrePath == null) { - _genrePath = Path.Combine(ImagesByNamePath, "Genre"); + _genrePath = Path.Combine(ItemsByNamePath, "Genre"); if (!Directory.Exists(_genrePath)) { Directory.CreateDirectory(_genrePath); @@ -187,7 +199,7 @@ namespace MediaBrowser.Server.Implementations { if (_studioPath == null) { - _studioPath = Path.Combine(ImagesByNamePath, "Studio"); + _studioPath = Path.Combine(ItemsByNamePath, "Studio"); if (!Directory.Exists(_studioPath)) { Directory.CreateDirectory(_studioPath); @@ -212,7 +224,7 @@ namespace MediaBrowser.Server.Implementations { if (_yearPath == null) { - _yearPath = Path.Combine(ImagesByNamePath, "Year"); + _yearPath = Path.Combine(ItemsByNamePath, "Year"); if (!Directory.Exists(_yearPath)) { Directory.CreateDirectory(_yearPath); @@ -237,7 +249,7 @@ namespace MediaBrowser.Server.Implementations { if (_generalPath == null) { - _generalPath = Path.Combine(ImagesByNamePath, "General"); + _generalPath = Path.Combine(ItemsByNamePath, "General"); if (!Directory.Exists(_generalPath)) { Directory.CreateDirectory(_generalPath); @@ -262,7 +274,7 @@ namespace MediaBrowser.Server.Implementations { if (_ratingsPath == null) { - _ratingsPath = Path.Combine(ImagesByNamePath, "Ratings"); + _ratingsPath = Path.Combine(ItemsByNamePath, "Ratings"); if (!Directory.Exists(_ratingsPath)) { Directory.CreateDirectory(_ratingsPath); @@ -363,7 +375,7 @@ namespace MediaBrowser.Server.Implementations { if (_musicArtistsPath == null) { - _musicArtistsPath = Path.Combine(ImagesByNamePath, "Artists"); + _musicArtistsPath = Path.Combine(ItemsByNamePath, "Artists"); if (!Directory.Exists(_musicArtistsPath)) { Directory.CreateDirectory(_musicArtistsPath); diff --git a/MediaBrowser.ServerApplication/App.xaml.cs b/MediaBrowser.ServerApplication/App.xaml.cs index 3c1524eec..762eb4cd2 100644 --- a/MediaBrowser.ServerApplication/App.xaml.cs +++ b/MediaBrowser.ServerApplication/App.xaml.cs @@ -222,34 +222,9 @@ namespace MediaBrowser.ServerApplication var url = "http://localhost:" + configurationManager.Configuration.HttpServerPortNumber + "/" + Kernel.Instance.WebApplicationName + "/dashboard/" + page; - if (loggedInUser != null) - { - url = AddAutoLoginToDashboardUrl(url, loggedInUser); - } - OpenUrl(url); } - /// - /// Adds the auto login to dashboard URL. - /// - /// The URL. - /// The user. - /// System.String. - public static string AddAutoLoginToDashboardUrl(string url, User user) - { - if (url.IndexOf('?') == -1) - { - url += "?u=" + user.Id; - } - else - { - url += "&u=" + user.Id; - } - - return url; - } - /// /// Opens the URL. ///