From ca2e0f3451b61899aeeddf3ae257395792b79b35 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 15 Oct 2013 11:29:19 -0400 Subject: [PATCH] fixes #588 - Add option to use xbmc convention when saving images --- .../UserLibrary/PersonsService.cs | 1 + .../Configuration/ServerConfiguration.cs | 9 ++ .../ImageFromMediaLocationProvider.cs | 3 +- .../Movies/MovieDbProvider.cs | 21 ++--- .../Providers/ImageSaver.cs | 82 +++++++++++++++++-- 5 files changed, 93 insertions(+), 23 deletions(-) diff --git a/MediaBrowser.Api/UserLibrary/PersonsService.cs b/MediaBrowser.Api/UserLibrary/PersonsService.cs index 57a9b01099..2df6cacda2 100644 --- a/MediaBrowser.Api/UserLibrary/PersonsService.cs +++ b/MediaBrowser.Api/UserLibrary/PersonsService.cs @@ -22,6 +22,7 @@ namespace MediaBrowser.Api.UserLibrary /// Gets or sets the person types. /// /// The person types. + [ApiMember(Name = "PersonTypes", Description = "Optional filter by person type. Accepts multiple, comma-delimited.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)] public string PersonTypes { get; set; } } diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index 40003e638f..6c6cc32f30 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -228,12 +228,15 @@ namespace MediaBrowser.Model.Configuration public bool EnableVideoImageExtraction { get; set; } + public ImageSavingConvention ImageSavingConvention { get; set; } + /// /// Initializes a new instance of the class. /// public ServerConfiguration() : base() { + ImageSavingConvention = ImageSavingConvention.Legacy; HttpServerPortNumber = 8096; LegacyWebSocketPortNumber = 8945; EnableHttpLevelLogging = true; @@ -277,4 +280,10 @@ namespace MediaBrowser.Model.Configuration SeasonZeroDisplayName = "Specials"; } } + + public enum ImageSavingConvention + { + Legacy, + Compatible + } } diff --git a/MediaBrowser.Providers/ImageFromMediaLocationProvider.cs b/MediaBrowser.Providers/ImageFromMediaLocationProvider.cs index 8ee82a1315..c3546c9361 100644 --- a/MediaBrowser.Providers/ImageFromMediaLocationProvider.cs +++ b/MediaBrowser.Providers/ImageFromMediaLocationProvider.cs @@ -206,7 +206,8 @@ namespace MediaBrowser.Providers if (!string.IsNullOrEmpty(name)) { - image = GetImage(item, args, name); + image = GetImage(item, args, name) ?? + GetImage(item, args, name + "-poster"); } } diff --git a/MediaBrowser.Providers/Movies/MovieDbProvider.cs b/MediaBrowser.Providers/Movies/MovieDbProvider.cs index 475e198835..6c65388482 100644 --- a/MediaBrowser.Providers/Movies/MovieDbProvider.cs +++ b/MediaBrowser.Providers/Movies/MovieDbProvider.cs @@ -509,8 +509,6 @@ namespace MediaBrowser.Providers.Movies var hasAltMeta = HasAltMeta(item); - var isRefreshingDueToTmdbUpdate = hasAltMeta && !isForcedRefresh; - if (string.IsNullOrEmpty(dataFilePath) || !File.Exists(dataFilePath)) { var isBoxSet = item is BoxSet; @@ -528,8 +526,6 @@ namespace MediaBrowser.Providers.Movies Directory.CreateDirectory(directory); JsonSerializer.SerializeToFile(mainResult, dataFilePath); - - isRefreshingDueToTmdbUpdate = false; } if (isForcedRefresh || ConfigurationManager.Configuration.EnableTmdbUpdates || !hasAltMeta) @@ -538,7 +534,7 @@ namespace MediaBrowser.Providers.Movies var mainResult = JsonSerializer.DeserializeFromFile(dataFilePath); - ProcessMainInfo(item, mainResult, isRefreshingDueToTmdbUpdate); + ProcessMainInfo(item, mainResult); } } @@ -650,8 +646,7 @@ namespace MediaBrowser.Providers.Movies /// /// The movie. /// The movie data. - /// if set to true [is refreshing due to TMDB update]. - protected virtual void ProcessMainInfo(BaseItem movie, CompleteMovieData movieData, bool isRefreshingDueToTmdbUpdate) + protected virtual void ProcessMainInfo(BaseItem movie, CompleteMovieData movieData) { if (movie != null && movieData != null) { @@ -691,14 +686,14 @@ namespace MediaBrowser.Providers.Movies // tmdb appears to have unified their numbers to always report "7.3" regardless of country // so I removed the culture-specific processing here because it was not working for other countries -ebr - // Don't import this when responding to tmdb updates because we don't want to blow away imdb data - if (!isRefreshingDueToTmdbUpdate && float.TryParse(voteAvg, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out rating)) + // Movies get this from imdb + if (movie is BoxSet && float.TryParse(voteAvg, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out rating)) { movie.CommunityRating = rating; } - // Don't import this when responding to tmdb updates because we don't want to blow away imdb data - if (!isRefreshingDueToTmdbUpdate) + // Movies get this from imdb + if (movie is BoxSet) { movie.VoteCount = movieData.vote_count; } @@ -784,8 +779,8 @@ namespace MediaBrowser.Providers.Movies } // genres - // Don't import this when responding to tmdb updates because we don't want to blow away imdb data - if (movieData.genres != null && !movie.LockedFields.Contains(MetadataFields.Genres) && !isRefreshingDueToTmdbUpdate) + // Movies get this from imdb + if (movieData.genres != null && !movie.LockedFields.Contains(MetadataFields.Genres) && movie is BoxSet) { movie.Genres.Clear(); diff --git a/MediaBrowser.Server.Implementations/Providers/ImageSaver.cs b/MediaBrowser.Server.Implementations/Providers/ImageSaver.cs index d0f4882e4b..cb780284fd 100644 --- a/MediaBrowser.Server.Implementations/Providers/ImageSaver.cs +++ b/MediaBrowser.Server.Implementations/Providers/ImageSaver.cs @@ -4,6 +4,7 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.IO; +using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Entities; using System; using System.Globalization; @@ -92,7 +93,11 @@ namespace MediaBrowser.Server.Implementations.Providers saveLocally = false; } - var path = GetSavePath(item, type, imageIndex, mimeType, saveLocally); + var path = saveLocally && _config.Configuration.ImageSavingConvention == ImageSavingConvention.Compatible ? + GetCompatibleSavePath(item, type, imageIndex, mimeType) : + GetLegacySavePath(item, type, imageIndex, mimeType, saveLocally); + + Directory.CreateDirectory(Path.GetDirectoryName(path)); var currentPath = GetCurrentImagePath(item, type, imageIndex); @@ -112,8 +117,8 @@ namespace MediaBrowser.Server.Implementations.Providers { file.Attributes &= ~FileAttributes.Hidden; } - } - + } + using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous)) { await source.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false); @@ -210,7 +215,7 @@ namespace MediaBrowser.Server.Implementations.Providers /// or /// imageIndex /// - private string GetSavePath(BaseItem item, ImageType type, int? imageIndex, string mimeType, bool saveLocally) + private string GetLegacySavePath(BaseItem item, ImageType type, int? imageIndex, string mimeType, bool saveLocally) { string filename; @@ -271,22 +276,81 @@ namespace MediaBrowser.Server.Implementations.Providers path = _remoteImageCache.GetResourcePath(item.GetType().FullName + item.Id, filename); } - var parentPath = Path.GetDirectoryName(path); + return path; + } - Directory.CreateDirectory(parentPath); + /// + /// Gets the compatible save path. + /// + /// The item. + /// The type. + /// Index of the image. + /// Type of the MIME. + /// System.String. + /// imageIndex + private string GetCompatibleSavePath(BaseItem item, ImageType type, int? imageIndex, string mimeType) + { + var extension = mimeType.Split('/').Last(); - return path; + if (string.Equals(extension, "jpeg", StringComparison.OrdinalIgnoreCase)) + { + extension = "jpg"; + } + extension = "." + extension.ToLower(); + + // Backdrop paths + if (type == ImageType.Backdrop) + { + if (!imageIndex.HasValue) + { + throw new ArgumentNullException("imageIndex"); + } + + if (imageIndex.Value == 0) + { + return Path.Combine(item.MetaLocation, "fanart" + extension); + } + + return Path.Combine(item.MetaLocation, "extrafanart", "fanart" + imageIndex.Value.ToString(UsCulture) + extension); + } + + if (type == ImageType.Primary) + { + if (item is Episode) + { + return Path.ChangeExtension(item.Path, extension); + } + + if (item.IsInMixedFolder) + { + return GetSavePathForItemInMixedFolder(item, type, string.Empty, extension); + } + + var filename = Path.GetFileNameWithoutExtension(item.Path) + "-poster" + extension; + return Path.Combine(item.MetaLocation, filename); + } + + // All other paths are the same + return GetLegacySavePath(item, type, imageIndex, mimeType, true); } + /// + /// Gets the save path for item in mixed folder. + /// + /// The item. + /// The type. + /// The image filename. + /// The extension. + /// System.String. private string GetSavePathForItemInMixedFolder(BaseItem item, ImageType type, string imageFilename, string extension) { if (type == ImageType.Primary) { - return Path.ChangeExtension(item.Path, extension); + imageFilename = "poster"; } var folder = Path.GetDirectoryName(item.Path); - return Path.Combine(folder, Path.GetFileNameWithoutExtension(item.Path) + "-" + imageFilename); + return Path.Combine(folder, Path.GetFileNameWithoutExtension(item.Path) + "-" + imageFilename + extension); } } }