示例#1
0
        private async Task<object> GetImageResult(
            BaseItem item,
            Guid itemId,
            ImageRequest request,
            ItemImageInfo image,
            bool cropwhitespace,
            IReadOnlyCollection<ImageFormat> supportedFormats,
            TimeSpan? cacheDuration,
            IDictionary<string, string> headers,
            bool isHeadRequest)
        {
            if (!image.IsLocalFile)
            {
                item ??= _libraryManager.GetItemById(itemId);
                image = await _libraryManager.ConvertImageToLocal(item, image, request.Index ?? 0).ConfigureAwait(false);
            }

            var options = new ImageProcessingOptions
            {
                CropWhiteSpace = cropwhitespace,
                Height = request.Height,
                ImageIndex = request.Index ?? 0,
                Image = image,
                Item = item,
                ItemId = itemId,
                MaxHeight = request.MaxHeight,
                MaxWidth = request.MaxWidth,
                Quality = request.Quality ?? 100,
                Width = request.Width,
                AddPlayedIndicator = request.AddPlayedIndicator,
                PercentPlayed = request.PercentPlayed ?? 0,
                UnplayedCount = request.UnplayedCount,
                Blur = request.Blur,
                BackgroundColor = request.BackgroundColor,
                ForegroundLayer = request.ForegroundLayer,
                SupportedOutputFormats = supportedFormats
            };

            var imageResult = await _imageProcessor.ProcessImage(options).ConfigureAwait(false);

            headers[HeaderNames.Vary] = HeaderNames.Accept;

            return await ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
            {
                CacheDuration = cacheDuration,
                ResponseHeaders = headers,
                ContentType = imageResult.Item2,
                DateLastModified = imageResult.Item3,
                IsHeadRequest = isHeadRequest,
                Path = imageResult.Item1,

                FileShare = FileShare.Read

            }).ConfigureAwait(false);
        }
示例#2
0
        public async Task <object> Get(GetShareImage request)
        {
            var share = _sharingManager.GetShareInfo(request.Id);

            if (share == null)
            {
                throw new ResourceNotFoundException();
            }
            if (share.ExpirationDate <= DateTime.UtcNow)
            {
                throw new ResourceNotFoundException();
            }

            var item = _libraryManager.GetItemById(share.ItemId);

            var image = item.GetImageInfo(ImageType.Primary, 0);

            if (image != null)
            {
                if (image.IsLocalFile)
                {
                    return(await _resultFactory.GetStaticFileResult(Request, image.Path).ConfigureAwait(false));
                }

                try
                {
                    // Don't fail the request over this
                    var updatedImage = await _libraryManager.ConvertImageToLocal(item, image, 0).ConfigureAwait(false);

                    return(await _resultFactory.GetStaticFileResult(Request, updatedImage.Path).ConfigureAwait(false));
                }
                catch
                {
                }
            }

            // Grab a dlna icon if nothing else is available
            using (var response = _dlnaManager.GetIcon("logo240.jpg"))
            {
                using (var ms = new MemoryStream())
                {
                    response.Stream.CopyTo(ms);

                    ms.Position = 0;
                    var bytes = ms.ToArray();
                    return(ResultFactory.GetResult(bytes, "image/" + response.Format.ToString().ToLower()));
                }
            }
        }