Exemplo n.º 1
0
        /// <summary>
        /// Gets the images.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="provider">The provider.</param>
        /// <param name="preferredLanguages">The preferred languages.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="type">The type.</param>
        /// <returns>Task{IEnumerable{RemoteImageInfo}}.</returns>
        private async Task <IEnumerable <RemoteImageInfo> > GetImages(
            BaseItem item,
            IRemoteImageProvider provider,
            IReadOnlyCollection <string> preferredLanguages,
            CancellationToken cancellationToken,
            ImageType?type = null)
        {
            try
            {
                var result = await provider.GetImages(item, cancellationToken).ConfigureAwait(false);

                if (type.HasValue)
                {
                    result = result.Where(i => i.Type == type.Value);
                }

                if (preferredLanguages.Count > 0)
                {
                    result = result.Where(i => string.IsNullOrEmpty(i.Language) ||
                                          preferredLanguages.Contains(i.Language, StringComparer.OrdinalIgnoreCase) ||
                                          string.Equals(i.Language, "en", StringComparison.OrdinalIgnoreCase));
                }

                return(result);
            }
            catch (OperationCanceledException)
            {
                return(new List <RemoteImageInfo>());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "{ProviderName} failed in GetImageInfos for type {ItemType} at {ItemPath}", provider.GetType().Name, item.GetType().Name, item.Path);
                return(new List <RemoteImageInfo>());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the images.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="provider">The provider.</param>
        /// <param name="preferredLanguage">The preferred language.</param>
        /// <param name="type">The type.</param>
        /// <returns>Task{IEnumerable{RemoteImageInfo}}.</returns>
        private async Task <IEnumerable <RemoteImageInfo> > GetImages(IHasImages item, CancellationToken cancellationToken, IRemoteImageProvider provider, string preferredLanguage, ImageType?type = null)
        {
            try
            {
                var result = await provider.GetImages(item, cancellationToken).ConfigureAwait(false);

                if (type.HasValue)
                {
                    result = result.Where(i => i.Type == type.Value);
                }

                if (string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase))
                {
                    result = result.Where(i => string.IsNullOrEmpty(i.Language) ||
                                          string.Equals(i.Language, "en", StringComparison.OrdinalIgnoreCase));
                }

                return(result);
            }
            catch (OperationCanceledException)
            {
                return(new List <RemoteImageInfo>());
            }
            catch (Exception ex)
            {
                _logger.ErrorException("{0} failed in GetImageInfos for type {1}", ex, provider.GetType().Name, item.GetType().Name);
                return(new List <RemoteImageInfo>());
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the images.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="provider">The provider.</param>
        /// <param name="preferredLanguage">The preferred language.</param>
        /// <param name="includeAllLanguages">Whether to include all languages in results.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="type">The type.</param>
        /// <returns>Task{IEnumerable{RemoteImageInfo}}.</returns>
        private async Task <IEnumerable <RemoteImageInfo> > GetImages(
            BaseItem item,
            IRemoteImageProvider provider,
            string preferredLanguage,
            bool includeAllLanguages,
            CancellationToken cancellationToken,
            ImageType?type = null)
        {
            bool hasPreferredLanguage = !string.IsNullOrWhiteSpace(preferredLanguage);

            try
            {
                var result = await provider.GetImages(item, cancellationToken).ConfigureAwait(false);

                if (type.HasValue)
                {
                    result = result.Where(i => i.Type == type.Value);
                }

                if (!includeAllLanguages && hasPreferredLanguage)
                {
                    // Filter out languages that do not match the preferred languages.
                    //
                    // TODO: should exception case of "en" (English) eventually be removed?
                    result = result.Where(i => string.IsNullOrWhiteSpace(i.Language) ||
                                          string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase) ||
                                          string.Equals(i.Language, "en", StringComparison.OrdinalIgnoreCase));
                }

                return(result.OrderByLanguageDescending(preferredLanguage));
            }
            catch (OperationCanceledException)
            {
                return(new List <RemoteImageInfo>());
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "{ProviderName} failed in GetImageInfos for type {ItemType} at {ItemPath}", provider.GetType().Name, item.GetType().Name, item.Path);
                return(new List <RemoteImageInfo>());
            }
        }