Esempio n. 1
0
        private async Task <Image> LoadFromGitHubAsync(string imageFileName, string email, int imageSize)
        {
            string imageUrl = null;

            try
            {
                int    suffixPosition = email.IndexOf(GitHubPrivateEmailSuffix, StringComparison.OrdinalIgnoreCase);
                string username       = email.Substring(0, suffixPosition);
                var    client         = new Git.hub.Client();
                var    user           = client.GetUser(username);
                if (!string.IsNullOrEmpty(user?.AvatarUrl))
                {
                    var builder = new UriBuilder(user.AvatarUrl);
                    var query   = new StringBuilder(builder.Query.TrimStart('?'));
                    query.Append(query.Length == 0 ? "?" : "&");
                    query.AppendFormat("s={0}", imageSize);
                    builder.Query = query.ToString();
                    imageUrl      = builder.Uri.AbsoluteUri;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            if (string.IsNullOrEmpty(imageUrl))
            {
                return(null);
            }

            return(await DownloadImage(new Uri(imageUrl), imageFileName));
        }
        private async Task <Uri> BuildAvatarUri([NotNull] string email, string name, int imageSize)
        {
            var match = _gitHubEmailRegex.Match(email);

            if (match.Success)
            {
                // email is an @users.noreply.github.com address

                var username = match.Groups["username"].Value;

                // For real users we can directly access the avatar by using
                // https://avatars.githubusercontent.com/{encodedUsername}?s={imageSize}
                // But for bots this doesn't work. To get the avatar url we can make use of the
                // GitHub API to get the profile (which includes the avatar url) but for unauthenticated
                // requests the rate limits are pretty low (60 requests per hour)

                // To mitigate the issue of possibly hitting the rate limit, we directly load the avatars
                // for all "normal" users and only users that can't be resolved that way (like bots)
                // query the GitHub profile first.

                // GitHub user names can't contain square brackets but bots use them.
                var isBot = username.IndexOf('[') >= 0;

                if (isBot)
                {
                    var client      = new Git.hub.Client();
                    var userProfile = await client.GetUserAsync(username);

                    if (string.IsNullOrEmpty(userProfile?.AvatarUrl))
                    {
                        return(null);
                    }

                    var builder = new UriBuilder(userProfile.AvatarUrl);
                    var query   = new StringBuilder(builder.Query.TrimStart('?'));
                    query.Append(query.Length == 0 ? "?" : "&");
                    query.Append("s=").Append(imageSize);
                    builder.Query = query.ToString();
                    return(builder.Uri);
                }
                else
                {
                    var encodedUsername = HttpUtility.UrlEncode(match.Groups["username"].Value);
                    return(new Uri($"https://avatars.githubusercontent.com/{encodedUsername}?s={imageSize}"));
                }
            }
            else
            {
                // regular email address

                if (_onlySupplyNoReply)
                {
                    return(null);
                }

                var encodedEmail = HttpUtility.UrlEncode(email);
                return(new Uri($"https://avatars.githubusercontent.com/u/e?email={encodedEmail}&s={imageSize}"));
            }
        }