/// <summary>
        /// Helper function.
        /// </summary>
        public static (EmailForSave result, IEnumerable <(string name, byte[] content)> blobs) ToEntity(EmailToSend emailToSend)
        {
            var blobCount = 1 + emailToSend.Attachments.Count(); // To make sliiightly faster
            var blobs     = new List <(string name, byte[] content)>(blobCount);

            string bodyBlobId = null;

            if (!string.IsNullOrWhiteSpace(emailToSend.Body))
            {
                bodyBlobId = Guid.NewGuid().ToString();
                var bodyBlobName    = EmailUtil.EmailBodyBlobName(bodyBlobId);
                var bodyBlobContent = Encoding.UTF8.GetBytes(emailToSend.Body);

                blobs.Add((bodyBlobName, bodyBlobContent));
            }

            var emailForSave = new EmailForSave
            {
                To          = string.Join(';', emailToSend.To ?? new List <string>()),
                Cc          = string.Join(';', emailToSend.Cc ?? new List <string>()),
                Bcc         = string.Join(';', emailToSend.Bcc ?? new List <string>()),
                Subject     = emailToSend.Subject,
                BodyBlobId  = bodyBlobId,
                Id          = emailToSend.EmailId,
                State       = EmailState.Scheduled,
                Attachments = new List <EmailAttachmentForSave>()
            };

            if (emailToSend.Attachments != null)
            {
                foreach (var att in emailToSend.Attachments)
                {
                    // If there is no content, then don't add the attachment
                    var    contentBlobContent = att.Contents;
                    string contentBlobId      = null;

                    if (contentBlobContent != null && contentBlobContent.Length > 0)
                    {
                        contentBlobId = Guid.NewGuid().ToString();
                        var contentBlobName = EmailUtil.EmailAttachmentBlobName(contentBlobId);
                        blobs.Add((contentBlobName, contentBlobContent));
                    }

                    emailForSave.Attachments.Add(new EmailAttachmentForSave
                    {
                        Name          = att.Name,
                        ContentBlobId = contentBlobId
                    });
                }
            }

            return(emailForSave, blobs);
        }
Exemple #2
0
        protected override async Task <EmailResult> ToEntityResult(EmailForQuery entity, CancellationToken cancellation = default)
        {
            string body = null;

            if (IncludeBody && !string.IsNullOrWhiteSpace(entity.BodyBlobId))
            {
                var blobName = EmailUtil.EmailBodyBlobName(entity.BodyBlobId);
                var bytes    = await _blobService.LoadBlobAsync(_behavior.TenantId, blobName, cancellation);

                body = Encoding.UTF8.GetString(bytes);
            }

            return(new EmailResult(entity, body));
        }
        /// <summary>
        /// Helper function
        /// </summary>
        public static async Task <IEnumerable <EmailToSend> > FromEntities(int tenantId, IEnumerable <EmailForSave> emails, IBlobService blobService, CancellationToken cancellation)
        {
            var result = new List <EmailToSend>();

            foreach (var emailForSave in emails)
            {
                string body = null;
                if (!string.IsNullOrWhiteSpace(emailForSave.BodyBlobId))
                {
                    var bodyBlobName = EmailUtil.EmailBodyBlobName(emailForSave.BodyBlobId);
                    var bodyContent  = bodyBlobName == null ? null : await blobService.LoadBlobAsync(tenantId, bodyBlobName, cancellation);

                    body = Encoding.UTF8.GetString(bodyContent);
                }

                var attachments = new List <EmailAttachmentToSend>();
                result.Add(new EmailToSend()
                {
                    To          = emailForSave.To?.Split(';'),
                    Cc          = emailForSave.Cc?.Split(';'),
                    Bcc         = emailForSave.Bcc?.Split(';'),
                    EmailId     = emailForSave.Id,
                    Subject     = emailForSave.Subject,
                    Body        = body,
                    Attachments = attachments,
                    TenantId    = tenantId
                });

                foreach (var att in emailForSave?.Attachments ?? new List <EmailAttachmentForSave>())
                {
                    if (!string.IsNullOrWhiteSpace(att.ContentBlobId))
                    {
                        var attBlobName = EmailUtil.EmailAttachmentBlobName(att.ContentBlobId);
                        var attContent  = await blobService.LoadBlobAsync(tenantId, attBlobName, cancellation);

                        attachments.Add(new EmailAttachmentToSend
                        {
                            Name     = att.Name,
                            Contents = attContent
                        });
                    }
                }
            }

            return(result);
        }