示例#1
0
        /// <inheritdoc/>
        public Task <Image?> GetAvatarAsync(string email, string?name, int imageSize)
        {
            var fallback = SerializeFallbackType(_fallback) ?? "404";
            var hash     = ComputeHash(email);

            UriBuilder uri = new("https", "www.gravatar.com")
            {
                Path  = $"/avatar/{hash}",
                Query = $"s={imageSize}&r={_rating}&d={fallback}"
            };

            if (_forceFallback)
            {
                uri.Query += "&f=y";
            }

            var avatarUri = uri.Uri;

            // TODO NULLABLE UriBuilder.Uri doesn't appear to be nullable
            if (avatarUri is null)
            {
                return(Task.FromResult <Image?>(null));
            }

            return(_downloader.DownloadImageAsync(avatarUri));
        }
        public async Task <Image> GetAvatarAsync([NotNull] string email, string name, int imageSize)
        {
            var uri = await BuildAvatarUri(email, name, imageSize);

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

            var image = await _downloader.DownloadImageAsync(uri);

            // Sadly GitHub doesn't provide an option to return a 404 error for non-custom avatars
            // and always provides a fallback image (identicon). Using GitHubs fallback image would
            // render the user defined fallback useless so we have to filter out the identicons.

            // We do this by checking the size of the returned image, because identicons provided by
            // GitHub are never scaled and always 420 x 420 - even if a different size was requested.

            // We exploit that fact to filter out identicons.
            var isIdenticon = imageSize != 420 && image.Size.Width == 420;

            if (isIdenticon)
            {
                return(null);
            }

            return(image);
        }
        /// <inheritdoc/>
        public Task <Image> GetAvatarAsync([NotNull] string email, string name, int imageSize)
        {
            var fallback = SerializeFallbackType(_fallback) ?? "404";
            var hash     = ComputeHash(email);

            var uri = new UriBuilder("https", "www.gravatar.com")
            {
                Path  = $"/avatar/{hash}",
                Query = $"s={imageSize}&r={_rating}&d={fallback}"
            };

            if (_forceFallback)
            {
                uri.Query += "&f=y";
            }

            var avatarUri = uri.Uri;

            if (avatarUri == null)
            {
                return(Task.FromResult <Image>(null));
            }

            return(_downloader.DownloadImageAsync(avatarUri));
        }
        /// <inheritdoc/>
        public async Task <Image?> GetAvatarAsync(string email, string?name, int imageSize)
        {
            var templateData = new UriTemplateData(email, name, imageSize);

            foreach (var provider in _subProvider)
            {
                var avatar = provider switch
                {
                    UriTemplateResolver r => await _downloader.DownloadImageAsync(r.ResolveTemplate(templateData)),
                    _ => await provider.GetAvatarAsync(email, name, imageSize),
                };

                if (avatar is not null)
                {
                    return(avatar);
                }
            }

            return(null);
        }