浏览代码

Added some api client methods, as well as logo and backdrop parents

LukePulverenti Luke Pulverenti luke pulverenti 13 年之前
父节点
当前提交
2cab5712a7

+ 40 - 12
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? GetParentLogoItemId(BaseItem item)
+        private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
         {
-            if (string.IsNullOrEmpty(item.LogoImagePath))
-            {
-                var parent = item.Parent;
+            backdropCount = 0;
+
+            var parent = item.Parent;
 
-                while (parent != null)
+            while (parent != null)
+            {
+                if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
                 {
-                    if (!string.IsNullOrEmpty(parent.LogoImagePath))
-                    {
-                        return parent.Id;
-                    }
+                    backdropCount = parent.BackdropImagePaths.Count();
+                    return parent.Id;
+                }
+
+                parent = parent.Parent;
+            }
+
+            return null;
+        }
 
-                    parent = parent.Parent;
+        private static Guid? GetParentLogoItemId(BaseItem item)
+        {
+            var parent = item.Parent;
+
+            while (parent != null)
+            {
+                if (!string.IsNullOrEmpty(parent.LogoImagePath))
+                {
+                    return parent.Id;
                 }
+
+                parent = parent.Parent;
             }
 
             return null;

+ 62 - 0
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;
         }
 
+        /// <summary>
+        /// 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.
+        /// </summary>
+        /// <param name="itemWrapper">A given item.</param>
+        /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
+        /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
+        /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
+        /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
+        /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
+        public IEnumerable<string> GetBackdropImageUrls(ApiBaseItemWrapper<ApiBaseItem> 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<string> files = new List<string>();
+
+            for (int i = 0; i < backdropCount; i++)
+            {
+                files.Add(GetImageUrl(backdropItemId.Value, ImageType.Backdrop, i, width, height, maxWidth, maxHeight, quality));
+            }
+
+            return files;
+        }
+
+        /// <summary>
+        /// 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.
+        /// </summary>
+        /// <param name="itemWrapper">A given item.</param>
+        /// <param name="width">Use if a fixed width is required. Aspect ratio will be preserved.</param>
+        /// <param name="height">Use if a fixed height is required. Aspect ratio will be preserved.</param>
+        /// <param name="maxWidth">Use if a max width is required. Aspect ratio will be preserved.</param>
+        /// <param name="maxHeight">Use if a max height is required. Aspect ratio will be preserved.</param>
+        /// <param name="quality">Quality level, from 0-100. Currently only applies to JPG. The default value should suffice.</param>
+        public string GetLogoImageUrl(ApiBaseItemWrapper<ApiBaseItem> 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;
+        }
+        
         /// <summary>
         /// Gets an image stream based on a url
         /// </summary>

+ 7 - 3
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
         /// <summary>
         /// Gets or sets the port number used by the API
         /// </summary>
-        public int ApiPort { get; set; }
+        public int ServerApiPort { get; set; }
 
+        /// <summary>
+        /// Gets the current api url based on hostname and port.
+        /// </summary>
         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);
         }

+ 4 - 0
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.
         /// </summary>
         public Guid? ParentLogoItemId { get; set; }
+
+        public Guid? ParentBackdropItemId { get; set; }
+
+        public int? ParentBackdropCount { get; set; }
     }
 }