Beispiel #1
0
        /// <summary>
        /// Attaches the primary image aspect ratio.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <param name="item">The item.</param>
        /// <returns>Task.</returns>
        public void AttachPrimaryImageAspectRatio(IItemDto dto, IHasImages item)
        {
            var imageInfo = item.GetImageInfo(ImageType.Primary, 0);

            if (imageInfo == null)
            {
                return;
            }

            var path = imageInfo.Path;

            // See if we can avoid a file system lookup by looking for the file in ResolveArgs
            var dateModified = imageInfo.DateModified;

            ImageSize size;

            try
            {
                size = _imageProcessor.GetImageSize(path, dateModified);
            }
            catch (FileNotFoundException)
            {
                _logger.Error("Image file does not exist: {0}", path);
                return;
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Failed to determine primary image aspect ratio for {0}", ex, path);
                return;
            }

            dto.OriginalPrimaryImageAspectRatio = size.Width / size.Height;

            var supportedEnhancers = _imageProcessor.GetSupportedEnhancers(item, ImageType.Primary).ToList();

            foreach (var enhancer in supportedEnhancers)
            {
                try
                {
                    size = enhancer.GetEnhancedImageSize(item, ImageType.Primary, 0, size);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error in image enhancer: {0}", ex, enhancer.GetType().Name);
                }
            }

            dto.PrimaryImageAspectRatio = size.Width / size.Height;
        }
Beispiel #2
0
        /// <summary>
        /// Gets the image.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="item">The item.</param>
        /// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
        /// <returns>System.Object.</returns>
        /// <exception cref="ResourceNotFoundException"></exception>
        public Task <object> GetImage(ImageRequest request, Guid itemId, BaseItem item, bool isHeadRequest)
        {
            if (request.PercentPlayed.HasValue)
            {
                if (request.PercentPlayed.Value <= 0)
                {
                    request.PercentPlayed = null;
                }
                else if (request.PercentPlayed.Value >= 100)
                {
                    request.PercentPlayed      = null;
                    request.AddPlayedIndicator = true;
                }
            }
            if (request.PercentPlayed.HasValue)
            {
                request.UnplayedCount = null;
            }
            if (request.UnplayedCount.HasValue)
            {
                if (request.UnplayedCount.Value <= 0)
                {
                    request.UnplayedCount = null;
                }
            }

            if (item == null)
            {
                item = _libraryManager.GetItemById(itemId);

                if (item == null)
                {
                    throw new ResourceNotFoundException(string.Format("Item {0} not found.", itemId.ToString("N")));
                }
            }

            var imageInfo = GetImageInfo(request, item);

            if (imageInfo == null)
            {
                var displayText = item == null?itemId.ToString() : item.Name;

                throw new ResourceNotFoundException(string.Format("{0} does not have an image of type {1}", displayText, request.Type));
            }

            IImageEnhancer[] supportedImageEnhancers;

            if (_imageProcessor.ImageEnhancers.Length > 0)
            {
                if (item == null)
                {
                    item = _libraryManager.GetItemById(itemId);
                }

                supportedImageEnhancers = request.EnableImageEnhancers ? _imageProcessor.GetSupportedEnhancers(item, request.Type) : Array.Empty <IImageEnhancer>();
            }
            else
            {
                supportedImageEnhancers = Array.Empty <IImageEnhancer>();
            }

            var cropwhitespace = request.Type == ImageType.Logo ||
                                 request.Type == ImageType.Art;

            if (request.CropWhitespace.HasValue)
            {
                cropwhitespace = request.CropWhitespace.Value;
            }

            var outputFormats = GetOutputFormats(request);

            TimeSpan?cacheDuration = null;

            if (!string.IsNullOrEmpty(request.Tag))
            {
                cacheDuration = TimeSpan.FromDays(365);
            }

            var responseHeaders = new Dictionary <string, string>
            {
                { "transferMode.dlna.org", "Interactive" },
                { "realTimeInfo.dlna.org", "DLNA.ORG_TLAG=*" }
            };

            return(GetImageResult(item,
                                  itemId,
                                  request,
                                  imageInfo,
                                  cropwhitespace,
                                  outputFormats,
                                  supportedImageEnhancers,
                                  cacheDuration,
                                  responseHeaders,
                                  isHeadRequest));
        }
Beispiel #3
0
        public double?GetPrimaryImageAspectRatio(BaseItem item)
        {
            var imageInfo = item.GetImageInfo(ImageType.Primary, 0);

            if (imageInfo == null)
            {
                return(null);
            }

            var supportedEnhancers = _imageProcessor.GetSupportedEnhancers(item, ImageType.Primary);

            ImageDimensions size;

            var defaultAspectRatio = item.GetDefaultPrimaryImageAspectRatio();

            if (defaultAspectRatio > 0)
            {
                if (supportedEnhancers.Length == 0)
                {
                    return(defaultAspectRatio);
                }

                int dummyWidth  = 200;
                int dummyHeight = Convert.ToInt32(dummyWidth / defaultAspectRatio);
                size = new ImageDimensions(dummyWidth, dummyHeight);
            }
            else
            {
                if (!imageInfo.IsLocalFile)
                {
                    return(null);
                }

                try
                {
                    size = _imageProcessor.GetImageDimensions(item, imageInfo);

                    if (size.Width <= 0 || size.Height <= 0)
                    {
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Failed to determine primary image aspect ratio for {0}", imageInfo.Path);
                    return(null);
                }
            }

            foreach (var enhancer in supportedEnhancers)
            {
                try
                {
                    size = enhancer.GetEnhancedImageSize(item, ImageType.Primary, 0, size);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error in image enhancer: {0}", enhancer.GetType().Name);
                }
            }

            var width  = size.Width;
            var height = size.Height;

            if (width <= 0 || height <= 0)
            {
                return(null);
            }

            return(width / height);
        }
Beispiel #4
0
        /// <summary>
        /// Gets the image.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="item">The item.</param>
        /// <param name="isHeadRequest">if set to <c>true</c> [is head request].</param>
        /// <returns>System.Object.</returns>
        /// <exception cref="ResourceNotFoundException"></exception>
        public Task <object> GetImage(ImageRequest request, IHasMetadata item, bool isHeadRequest)
        {
            if (request.PercentPlayed.HasValue)
            {
                if (request.PercentPlayed.Value <= 0)
                {
                    request.PercentPlayed = null;
                }
                else if (request.PercentPlayed.Value >= 100)
                {
                    request.PercentPlayed      = null;
                    request.AddPlayedIndicator = true;
                }
            }
            if (request.PercentPlayed.HasValue)
            {
                request.UnplayedCount = null;
            }
            if (request.UnplayedCount.HasValue)
            {
                if (request.UnplayedCount.Value <= 0)
                {
                    request.UnplayedCount = null;
                }
            }

            var imageInfo = GetImageInfo(request, item);

            if (imageInfo == null)
            {
                throw new ResourceNotFoundException(string.Format("{0} does not have an image of type {1}", item.Name, request.Type));
            }

            var supportedImageEnhancers = request.EnableImageEnhancers ? _imageProcessor.GetSupportedEnhancers(item, request.Type) : new List <IImageEnhancer>();

            var cropwhitespace = request.Type == ImageType.Logo ||
                                 request.Type == ImageType.Art ||
                                 (request.Type == ImageType.Primary && item is LiveTvChannel);

            if (request.CropWhitespace.HasValue)
            {
                cropwhitespace = request.CropWhitespace.Value;
            }

            var outputFormats = GetOutputFormats(request);

            TimeSpan?cacheDuration = null;

            if (!string.IsNullOrEmpty(request.Tag))
            {
                cacheDuration = TimeSpan.FromDays(365);
            }

            var responseHeaders = new Dictionary <string, string>
            {
                { "transferMode.dlna.org", "Interactive" },
                { "realTimeInfo.dlna.org", "DLNA.ORG_TLAG=*" }
            };

            return(GetImageResult(item,
                                  request,
                                  imageInfo,
                                  cropwhitespace,
                                  outputFormats,
                                  supportedImageEnhancers,
                                  cacheDuration,
                                  responseHeaders,
                                  isHeadRequest));
        }