From 9fc6bc3385596f0c2b7c6217d434cfc34ae192ae Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 18 Jun 2013 16:54:32 -0400 Subject: [PATCH] fix rating values --- .../Localization/LocalizationManager.cs | 130 ++++++++---------- 1 file changed, 58 insertions(+), 72 deletions(-) diff --git a/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs b/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs index 2c67c97965..0e1d977085 100644 --- a/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs +++ b/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs @@ -37,6 +37,49 @@ namespace MediaBrowser.Server.Implementations.Localization public LocalizationManager(IServerConfigurationManager configurationManager) { _configurationManager = configurationManager; + + ExtractAll(); + } + + private void ExtractAll() + { + var type = GetType(); + var resourcePath = type.Namespace + ".Ratings."; + + var localizationPath = LocalizationPath; + + var existingFiles = Directory.EnumerateFiles(localizationPath, "ratings-*.txt", SearchOption.TopDirectoryOnly) + .Select(Path.GetFileName) + .ToList(); + + if (!Directory.Exists(localizationPath)) + { + Directory.CreateDirectory(localizationPath); + } + + // Extract from the assembly + foreach (var resource in type.Assembly + .GetManifestResourceNames() + .Where(i => i.StartsWith(resourcePath))) + { + var filename = "ratings-" + resource.Substring(resourcePath.Length); + + if (!existingFiles.Contains(filename)) + { + using (var stream = type.Assembly.GetManifestResourceStream(resource)) + { + using (var fs = new FileStream(Path.Combine(localizationPath, filename), FileMode.Create, FileAccess.Write, FileShare.Read)) + { + stream.CopyTo(fs); + } + } + } + } + + foreach (var file in Directory.EnumerateFiles(localizationPath, "ratings-*.txt", SearchOption.TopDirectoryOnly)) + { + LoadRatings(file); + } } /// @@ -128,15 +171,7 @@ namespace MediaBrowser.Server.Implementations.Localization { Dictionary value; - if (!_allParentalRatings.TryGetValue(countryCode, out value)) - { - value = LoadRatings(countryCode); - - if (value != null) - { - _allParentalRatings.TryAdd(countryCode, value); - } - } + _allParentalRatings.TryGetValue(countryCode, out value); return value; } @@ -144,17 +179,11 @@ namespace MediaBrowser.Server.Implementations.Localization /// /// Loads the ratings. /// - /// The country code. - private Dictionary LoadRatings(string countryCode) + /// The file. + /// Dictionary{System.StringParentalRating}. + private void LoadRatings(string file) { - var path = GetRatingsFilePath(countryCode); - - if (string.IsNullOrEmpty(path)) - { - return null; - } - - return File.ReadAllLines(path).Select(i => + var dict = File.ReadAllLines(file).Select(i => { if (!string.IsNullOrWhiteSpace(i)) { @@ -176,47 +205,10 @@ namespace MediaBrowser.Server.Implementations.Localization }) .Where(i => i != null) .ToDictionary(i => i.Name); - } - /// - /// Gets the ratings file. - /// - /// The country code. - private string GetRatingsFilePath(string countryCode) - { - countryCode = countryCode.ToLower(); + var countryCode = Path.GetFileNameWithoutExtension(file).Split('-').Last(); - var path = Path.Combine(LocalizationPath, "ratings-" + countryCode + ".txt"); - - if (!File.Exists(path)) - { - // Extract embedded resource - - var type = GetType(); - var resourcePath = type.Namespace + ".Ratings." + countryCode + ".txt"; - - using (var stream = type.Assembly.GetManifestResourceStream(resourcePath)) - { - if (stream == null) - { - return null; - } - - var parentPath = Path.GetDirectoryName(path); - - if (!Directory.Exists(parentPath)) - { - Directory.CreateDirectory(parentPath); - } - - using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read)) - { - stream.CopyTo(fs); - } - } - } - - return path; + _allParentalRatings.TryAdd(countryCode, dict); } /// @@ -235,23 +227,17 @@ namespace MediaBrowser.Server.Implementations.Localization if (!ratingsDictionary.TryGetValue(rating, out value)) { - var stripped = StripCountry(rating); - - ratingsDictionary.TryGetValue(stripped, out value); + // If we don't find anything check all ratings systems + foreach (var dictionary in _allParentalRatings.Values) + { + if (dictionary.TryGetValue(rating, out value)) + { + return value.Value; + } + } } return value == null ? (int?)null : value.Value; } - - /// - /// Strips the country. - /// - /// The rating. - /// System.String. - private static string StripCountry(string rating) - { - int start = rating.IndexOf('-'); - return start > 0 ? rating.Substring(start + 1) : rating; - } } }