diff --git a/MediaBrowser.Api/ApiService.cs b/MediaBrowser.Api/ApiService.cs index bb2427574..382d236ae 100644 --- a/MediaBrowser.Api/ApiService.cs +++ b/MediaBrowser.Api/ApiService.cs @@ -30,10 +30,21 @@ namespace MediaBrowser.Api Item = item, UserItemData = Kernel.Instance.GetUserItemData(userId, item.Id), Type = item.GetType().Name, - IsFolder = (item is Folder), - ParentLogoItemId = GetParentLogoItemId(item) + IsFolder = (item is Folder) }; + if (string.IsNullOrEmpty(item.LogoImagePath)) + { + wrapper.ParentLogoItemId = GetParentLogoItemId(item); + } + + if (item.BackdropImagePaths == null || !item.BackdropImagePaths.Any()) + { + int backdropCount; + wrapper.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount); + wrapper.ParentBackdropCount = backdropCount; + } + if (item.Parent != null) { wrapper.ParentId = item.Parent.Id; @@ -52,21 +63,38 @@ namespace MediaBrowser.Api return wrapper; } + private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount) + { + backdropCount = 0; + + var parent = item.Parent; + + while (parent != null) + { + if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any()) + { + backdropCount = parent.BackdropImagePaths.Count(); + return parent.Id; + } + + parent = parent.Parent; + } + + return null; + } + private static Guid? GetParentLogoItemId(BaseItem item) { - if (string.IsNullOrEmpty(item.LogoImagePath)) + var parent = item.Parent; + + while (parent != null) { - var parent = item.Parent; - - while (parent != null) + if (!string.IsNullOrEmpty(parent.LogoImagePath)) { - if (!string.IsNullOrEmpty(parent.LogoImagePath)) - { - return parent.Id; - } - - parent = parent.Parent; + return parent.Id; } + + parent = parent.Parent; } return null; diff --git a/MediaBrowser.ApiInteraction/ApiClient.cs b/MediaBrowser.ApiInteraction/ApiClient.cs index 16effcde0..813d984f6 100644 --- a/MediaBrowser.ApiInteraction/ApiClient.cs +++ b/MediaBrowser.ApiInteraction/ApiClient.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Net.Http; using System.Threading.Tasks; using MediaBrowser.Model.Configuration; @@ -69,6 +70,67 @@ namespace MediaBrowser.ApiInteraction return url; } + /// + /// This is a helper to get a list of backdrop url's from a given ApiBaseItemWrapper. If the actual item does not have any backdrops it will return backdrops from the first parent that does. + /// + /// A given item. + /// Use if a fixed width is required. Aspect ratio will be preserved. + /// Use if a fixed height is required. Aspect ratio will be preserved. + /// Use if a max width is required. Aspect ratio will be preserved. + /// Use if a max height is required. Aspect ratio will be preserved. + /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. + public IEnumerable GetBackdropImageUrls(ApiBaseItemWrapper itemWrapper, int? width, int? height, int? maxWidth, int? maxHeight, int? quality) + { + Guid? backdropItemId = null; + int backdropCount = 0; + + if (itemWrapper.Item.BackdropImagePaths == null || !itemWrapper.Item.BackdropImagePaths.Any()) + { + backdropItemId = itemWrapper.ParentBackdropItemId; + backdropCount = itemWrapper.ParentBackdropCount ?? 0; + } + else + { + backdropItemId = itemWrapper.Item.Id; + backdropCount = itemWrapper.Item.BackdropImagePaths.Count(); + } + + if (backdropItemId == null) + { + return new string[] { }; + } + + List files = new List(); + + for (int i = 0; i < backdropCount; i++) + { + files.Add(GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality)); + } + + return files; + } + + /// + /// This is a helper to get the logo image url from a given ApiBaseItemWrapper. If the actual item does not have a logo, it will return the logo from the first parent that does, or null. + /// + /// A given item. + /// Use if a fixed width is required. Aspect ratio will be preserved. + /// Use if a fixed height is required. Aspect ratio will be preserved. + /// Use if a max width is required. Aspect ratio will be preserved. + /// Use if a max height is required. Aspect ratio will be preserved. + /// Quality level, from 0-100. Currently only applies to JPG. The default value should suffice. + public string GetLogoImageUrl(ApiBaseItemWrapper itemWrapper, int? width, int? height, int? maxWidth, int? maxHeight, int? quality) + { + Guid? logoItemId = !string.IsNullOrEmpty(itemWrapper.Item.LogoImagePath) ? itemWrapper.Item.Id : itemWrapper.ParentLogoItemId; + + if (logoItemId.HasValue) + { + return GetImageUrl(logoItemId.Value, ImageType.Logo, null, width, height, maxWidth, maxHeight, quality); + } + + return null; + } + /// /// Gets an image stream based on a url /// diff --git a/MediaBrowser.ApiInteraction/BaseClient.cs b/MediaBrowser.ApiInteraction/BaseClient.cs index 887c1779f..b552bf32f 100644 --- a/MediaBrowser.ApiInteraction/BaseClient.cs +++ b/MediaBrowser.ApiInteraction/BaseClient.cs @@ -1,4 +1,5 @@ using System; +using System.Net; using System.Net.Http; namespace MediaBrowser.ApiInteraction @@ -16,13 +17,16 @@ namespace MediaBrowser.ApiInteraction /// /// Gets or sets the port number used by the API /// - public int ApiPort { get; set; } + public int ServerApiPort { get; set; } + /// + /// Gets the current api url based on hostname and port. + /// protected string ApiUrl { get { - return string.Format("http://{0}:{1}/mediabrowser/api", ServerHostName, ApiPort); + return string.Format("http://{0}:{1}/mediabrowser/api", ServerHostName, ServerApiPort); } } @@ -35,7 +39,7 @@ namespace MediaBrowser.ApiInteraction public BaseClient(HttpClientHandler clientHandler) { - clientHandler.AutomaticDecompression = System.Net.DecompressionMethods.GZip; + clientHandler.AutomaticDecompression = DecompressionMethods.GZip; HttpClient = new HttpClient(clientHandler); } diff --git a/MediaBrowser.Model/Entities/ApiBaseItem.cs b/MediaBrowser.Model/Entities/ApiBaseItem.cs index 0ec9ed7c6..ff4d13c0b 100644 --- a/MediaBrowser.Model/Entities/ApiBaseItem.cs +++ b/MediaBrowser.Model/Entities/ApiBaseItem.cs @@ -44,5 +44,9 @@ namespace MediaBrowser.Model.Entities /// If the item does not have a logo, this will hold the Id of the Parent that has one. /// public Guid? ParentLogoItemId { get; set; } + + public Guid? ParentBackdropItemId { get; set; } + + public int? ParentBackdropCount { get; set; } } }