示例#1
0
        public async Task Run()
        {
            try
            {
                string[] fields = new string[] { "id", "first_name", "last_name", "email", "picture.type(large)", "link", "locale", "name", "name_format", "updated_time" };

                List <Member> members = await _fbClient.GetAllCommunityMembers(fields);

                if (string.IsNullOrWhiteSpace(_gravatarsConfiguration.DefaultGravatarRelativePath))
                {
                    throw new MissingFieldException($"{nameof(_gravatarsConfiguration.DefaultGravatarRelativePath)} cannot be null, empty or white space");
                }

                if (string.IsNullOrWhiteSpace(_gravatarsConfiguration.DestinationFolder))
                {
                    throw new MissingFieldException($"{nameof(_gravatarsConfiguration.DestinationFolder)} cannot be null, empty or white space");
                }

                if (!Directory.Exists(_gravatarsConfiguration.DestinationFolder))
                {
                    Directory.CreateDirectory(_gravatarsConfiguration.DestinationFolder);
                }
                string defaultGravatarFullPath = Path.Combine(Directory.GetCurrentDirectory(), _gravatarsConfiguration.DefaultGravatarRelativePath);
                if (!File.Exists(defaultGravatarFullPath))
                {
                    throw new FileNotFoundException($"Could not find the file {defaultGravatarFullPath}");
                }
                byte[]        defaultGravatar          = File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), _gravatarsConfiguration.DefaultGravatarRelativePath));
                List <string> userImageFileNames       = new List <string>();
                List <string> usersNotFoundInDirectory = new List <string>();
                foreach (Member member in members)
                {
                    if (member.Picture != null && member.Picture.Data != null && !string.IsNullOrWhiteSpace(member.Picture.Data.Url))
                    {
                        byte[] image;
                        if (!member.Picture.Data.IsSilhouette)
                        {
                            image = await _downloadService.Download(member.Picture.Data.Url);
                        }
                        else
                        {
                            image = defaultGravatar;
                        }

                        string facebookUserEmail          = member.Email.Trim().ToLowerInvariant();
                        IEnumerable <string> emailAliases = await _googleAdminClient.GetAccountAliases(facebookUserEmail);

                        if (emailAliases == null)
                        {
                            usersNotFoundInDirectory.Add(facebookUserEmail);
                        }
                        List <string> emailsList = emailAliases?.ToList() ?? new List <string>();
                        if (!emailsList.Contains(facebookUserEmail))
                        {
                            emailsList.Add(facebookUserEmail);
                        }

                        foreach (string userEmail in emailsList)
                        {
                            string userEmailMD5         = _cryptographyService.CalculateMD5Hash(userEmail);
                            string userEmailMD5FileName = $"{userEmailMD5}.jpg";
                            string fullFileName         = Path.Combine(_gravatarsConfiguration.DestinationFolder, userEmailMD5FileName);
                            File.WriteAllBytes(fullFileName, image);
                            _logger.LogInformation($"User {userEmail} picture has been saved as {fullFileName}");
                            userImageFileNames.Add(userEmailMD5FileName);
                        }
                    }
                    else
                    {
                        _logger.LogWarning($"User {member.Email} picture is null, empty or white space");
                    }
                }
                cleanUpFiles(userImageFileNames);
                if (usersNotFoundInDirectory.Any())
                {
                    _logger.LogWarning($"Could not find the following address in Google directory: {string.Join(", ", usersNotFoundInDirectory)}");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
            }
        }