Esempio n. 1
0
        public async Task <DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
        {
            var liveTvItem = (LiveTvProgram)item;

            var imageResponse = new DynamicImageResponse();

            var service = _liveTvManager.Services.FirstOrDefault(i => string.Equals(i.Name, liveTvItem.ServiceName, StringComparison.OrdinalIgnoreCase));

            if (service != null)
            {
                try
                {
                    var channel = _liveTvManager.GetInternalChannel(liveTvItem.ChannelId);

                    var response = await service.GetProgramImageAsync(liveTvItem.ExternalId, channel.ExternalId, cancellationToken).ConfigureAwait(false);

                    if (response != null)
                    {
                        imageResponse.HasImage = true;
                        imageResponse.Stream   = response.Stream;
                        imageResponse.Format   = response.Format;
                    }
                }
                catch (NotImplementedException)
                {
                }
            }

            return(imageResponse);
        }
Esempio n. 2
0
        public async Task <DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
        {
            var channelItem = (IChannelItem)item;

            var imageResponse = new DynamicImageResponse();

            if (!string.IsNullOrEmpty(channelItem.OriginalImageUrl))
            {
                var options = new HttpRequestOptions
                {
                    CancellationToken = cancellationToken,
                    Url = channelItem.OriginalImageUrl
                };

                var response = await _httpClient.GetResponse(options).ConfigureAwait(false);

                if (response.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
                {
                    imageResponse.HasImage = true;
                    imageResponse.Stream   = response.Content;
                    imageResponse.SetFormatFromMimeType(response.ContentType);
                }
                else
                {
                    _logger.Error("Provider did not return an image content type.");
                }
            }

            return(imageResponse);
        }
        public async Task <DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
        {
            var liveTvItem = (LiveTvProgram)item;

            var imageResponse = new DynamicImageResponse();

            if (!string.IsNullOrEmpty(liveTvItem.ProviderImagePath))
            {
                imageResponse.Path     = liveTvItem.ProviderImagePath;
                imageResponse.HasImage = true;
            }
            else if (!string.IsNullOrEmpty(liveTvItem.ProviderImageUrl))
            {
                var options = new HttpRequestOptions
                {
                    CancellationToken = cancellationToken,
                    Url = liveTvItem.ProviderImageUrl
                };

                var response = await _httpClient.GetResponse(options).ConfigureAwait(false);

                if (response.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
                {
                    imageResponse.HasImage = true;
                    imageResponse.Stream   = response.Content;
                    imageResponse.SetFormatFromMimeType(response.ContentType);
                }
                else
                {
                    _logger.Error("Provider did not return an image content type.");
                }
            }
            else if (liveTvItem.HasProviderImage ?? true)
            {
                var service = _liveTvManager.Services.FirstOrDefault(i => string.Equals(i.Name, liveTvItem.ServiceName, StringComparison.OrdinalIgnoreCase));

                if (service != null)
                {
                    try
                    {
                        var channel = _liveTvManager.GetInternalChannel(liveTvItem.ChannelId);

                        var response = await service.GetProgramImageAsync(liveTvItem.ExternalId, channel.ExternalId, cancellationToken).ConfigureAwait(false);

                        if (response != null)
                        {
                            imageResponse.HasImage = true;
                            imageResponse.Stream   = response.Stream;
                            imageResponse.Format   = response.Format;
                        }
                    }
                    catch (NotImplementedException)
                    {
                    }
                }
            }

            return(imageResponse);
        }
Esempio n. 4
0
        public async void RefreshImages_PopulatedItemPopulatedProviderDynamic_UpdatesImagesIfForced(ImageType imageType, int imageCount, bool forceRefresh)
        {
            var item = GetItemWithImages(imageType, imageCount, false);

            var libraryOptions = GetLibraryOptions(item, imageType, imageCount);

            var imageResponse = new DynamicImageResponse
            {
                HasImage = true,
                Format   = ImageFormat.Jpg,
                Path     = "url path",
                Protocol = MediaProtocol.Http
            };

            var dynamicProvider = new Mock <IDynamicImageProvider>(MockBehavior.Strict);

            dynamicProvider.Setup(rp => rp.Name).Returns("MockDynamicProvider");
            dynamicProvider.Setup(rp => rp.GetSupportedImages(item))
            .Returns(new[] { imageType });
            dynamicProvider.Setup(rp => rp.GetImage(item, imageType, It.IsAny <CancellationToken>()))
            .ReturnsAsync(imageResponse);

            var refreshOptions = forceRefresh
                ? new ImageRefreshOptions(Mock.Of <IDirectoryService>())
            {
                ImageRefreshMode = MetadataRefreshMode.FullRefresh,
                ReplaceAllImages = true
            }
                : new ImageRefreshOptions(Mock.Of <IDirectoryService>());

            var itemImageProvider = GetItemImageProvider(null, new Mock <IFileSystem>());
            var result            = await itemImageProvider.RefreshImages(item, libraryOptions, new List <IImageProvider> {
                dynamicProvider.Object
            }, refreshOptions, CancellationToken.None);

            Assert.Equal(forceRefresh, result.UpdateType.HasFlag(ItemUpdateType.ImageUpdate));
            if (forceRefresh)
            {
                // replaces multi-types
                Assert.Single(item.GetImages(imageType));
            }
            else
            {
                // adds to multi-types if room
                Assert.Equal(imageCount, item.GetImages(imageType).Count());
            }
        }
Esempio n. 5
0
        public async void RefreshImages_EmptyItemPopulatedProviderDynamic_AddsImages(ImageType imageType, int imageCount, bool responseHasPath, MediaProtocol protocol)
        {
            // Has to exist for querying DateModified time on file, results stored but not checked so not populating
            BaseItem.FileSystem = Mock.Of <IFileSystem>();

            var item = new Video();

            var libraryOptions = GetLibraryOptions(item, imageType, imageCount);

            // Path must exist if set: is read in as a stream by AsyncFile.OpenRead
            var imageResponse = new DynamicImageResponse
            {
                HasImage = true,
                Format   = ImageFormat.Jpg,
                Path     = responseHasPath ? string.Format(CultureInfo.InvariantCulture, TestDataImagePath, 0) : null,
                Protocol = protocol
            };

            var dynamicProvider = new Mock <IDynamicImageProvider>(MockBehavior.Strict);

            dynamicProvider.Setup(rp => rp.Name).Returns("MockDynamicProvider");
            dynamicProvider.Setup(rp => rp.GetSupportedImages(item))
            .Returns(new[] { imageType });
            dynamicProvider.Setup(rp => rp.GetImage(item, imageType, It.IsAny <CancellationToken>()))
            .ReturnsAsync(imageResponse);

            var refreshOptions = new ImageRefreshOptions(Mock.Of <IDirectoryService>());

            var providerManager = new Mock <IProviderManager>(MockBehavior.Strict);

            providerManager.Setup(pm => pm.SaveImage(item, It.IsAny <Stream>(), It.IsAny <string>(), imageType, null, It.IsAny <CancellationToken>()))
            .Callback <BaseItem, Stream, string, ImageType, int?, CancellationToken>((callbackItem, _, _, callbackType, _, _) => callbackItem.SetImagePath(callbackType, 0, new FileSystemMetadata()))
            .Returns(Task.CompletedTask);
            var itemImageProvider = GetItemImageProvider(providerManager.Object, null);
            var result            = await itemImageProvider.RefreshImages(item, libraryOptions, new List <IImageProvider> {
                dynamicProvider.Object
            }, refreshOptions, CancellationToken.None);

            Assert.True(result.UpdateType.HasFlag(ItemUpdateType.ImageUpdate));
            // dynamic provider unable to return multiple images
            Assert.Single(item.GetImages(imageType));
            if (protocol == MediaProtocol.Http)
            {
                Assert.Equal(imageResponse.Path, item.GetImagePath(imageType, 0));
            }
        }
Esempio n. 6
0
        public async Task <DynamicImageResponse> GetChannelImage(ImageType type, CancellationToken cancellationToken)
        {
            logger.LogInformation(nameof(GetChannelImage));
            if (type == ImageType.Thumb || type == ImageType.Primary || type == ImageType.Backdrop || type == ImageType.Menu)
            {
                var name     = "n0tFlix.Channel.NRK.Images.logo.png";
                var response = new DynamicImageResponse
                {
                    Format   = ImageFormat.Png,
                    HasImage = true,
                    Stream   = GetType().Assembly.GetManifestResourceStream(name),
                };

                return(response);
            }
            return(await Task.FromResult <DynamicImageResponse>(null));
        }
Esempio n. 7
0
        private async Task <DynamicImageResponse> LoadCover(ZipArchive epub, XmlDocument opf, string opfRootDirectory)
        {
            var utilities = new OpfReader <EpubMetadataImageProvider>(opf, _logger);
            var coverRef  = utilities.ReadCoverPath(opfRootDirectory);

            if (coverRef == null)
            {
                return(new DynamicImageResponse {
                    HasImage = false
                });
            }

            var cover = coverRef.Value;

            var coverFile = epub.GetEntry(cover.Path);

            if (coverFile == null)
            {
                return(new DynamicImageResponse {
                    HasImage = false
                });
            }

            var memoryStream = new MemoryStream();

            using (var coverStream = coverFile.Open())
            {
                await coverStream.CopyToAsync(memoryStream)
                .ConfigureAwait(false);
            }

            memoryStream.Position = 0;

            var response = new DynamicImageResponse
            {
                HasImage = true,
                Stream   = memoryStream
            };

            response.SetFormatFromMimeType(cover.MimeType);

            return(response);
        }
Esempio n. 8
0
        private Task <DynamicImageResponse> LoadCover(ZipArchive epub, XmlDocument opf, string opfRootDirectory)
        {
            var coverRef = ReadCoverPath(opf, opfRootDirectory);

            if (coverRef == null)
            {
                return(Task.FromResult(new DynamicImageResponse {
                    HasImage = false
                }));
            }

            var cover = coverRef.Value;

            var coverFile = epub.GetEntry(cover.Path);

            if (coverFile == null)
            {
                return(Task.FromResult(new DynamicImageResponse {
                    HasImage = false
                }));
            }

            var memoryStream = new MemoryStream();

            using (var coverStream = coverFile.Open())
            {
                coverStream.CopyTo(memoryStream);
            }

            memoryStream.Position = 0;

            var response = new DynamicImageResponse
            {
                HasImage = true,
                Stream   = memoryStream
            };

            response.SetFormatFromMimeType(cover.MimeType);

            return(Task.FromResult(response));
        }