Skip to content

Commit

Permalink
GetMessageAttachments() - Remove null execution path
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavminhas committed Jul 27, 2024
1 parent 167716b commit 97e87e5
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions GmailAPIHelper/GmailHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,22 +368,25 @@ public static int GetMessageAttachments(this GmailService gmailService, string q
if (messages.Count > 0)
{
var latestMessage = messages.OrderByDescending(item => item.InternalDate).FirstOrDefault();
var messageRequest = service.Users.Messages.Get(userId, latestMessage.Id);
messageRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full;
var latestMessageDetails = messageRequest.Execute();
if (latestMessageDetails.Payload != null)
if (latestMessage != null)
{
if (latestMessageDetails.Payload.Parts.Count > 0)
var messageRequest = service.Users.Messages.Get(userId, latestMessage.Id);
messageRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full;
var latestMessageDetails = messageRequest.Execute();
if (latestMessageDetails.Payload != null)
{
foreach (var part in latestMessageDetails.Payload.Parts)
if (latestMessageDetails.Payload.Parts.Count > 0)
{
if (part.Filename != "")
foreach (var part in latestMessageDetails.Payload.Parts)
{
var messageAttachmentRequest = service.Users.Messages.Attachments.Get(userId, latestMessageDetails.Id, part.Body.AttachmentId);
var messageAttachmentResponse = messageAttachmentRequest.Execute();
var messageAttachmentData = Convert.FromBase64String(messageAttachmentResponse.Data.Replace('-', '+').Replace('_', '/').Replace(" ", "+"));
File.WriteAllBytes(Path.Combine(directoryPath, part.Filename), messageAttachmentData);
count++;
if (part.Filename != "")
{
var messageAttachmentRequest = service.Users.Messages.Attachments.Get(userId, latestMessageDetails.Id, part.Body.AttachmentId);
var messageAttachmentResponse = messageAttachmentRequest.Execute();
var messageAttachmentData = Convert.FromBase64String(messageAttachmentResponse.Data.Replace('-', '+').Replace('_', '/').Replace(" ", "+"));
File.WriteAllBytes(Path.Combine(directoryPath, part.Filename), messageAttachmentData);
count++;
}
}
}
}
Expand Down

0 comments on commit 97e87e5

Please sign in to comment.