public async Task PublishAsync(ExportDeployment deployment, ExportDeploymentContext context, CancellationToken cancellationToken)
        {
            var emailAddresses = deployment.EmailAddresses
                                 .SplitSafe(",")
                                 .Where(x => x.IsEmail())
                                 .ToArray();

            if (!emailAddresses.Any())
            {
                return;
            }

            var emailAccount = await _db.EmailAccounts.FindByIdAsync(deployment.EmailAccountId, false, cancellationToken);

            var fromEmailAddress = emailAccount.ToMailAddress();
            var files            = await context.GetDeploymentFilesAsync(cancellationToken);

            var canStreamBlob = _db.DataProvider.CanStreamBlob;
            var num           = 0;

            foreach (var emailAddress in emailAddresses)
            {
                var queuedEmail = new QueuedEmail
                {
                    From           = fromEmailAddress,
                    SendManually   = false,
                    To             = emailAddress,
                    Subject        = deployment.EmailSubject.NaIfEmpty(),
                    Body           = deployment.EmailSubject.NaIfEmpty(),
                    CreatedOnUtc   = DateTime.UtcNow,
                    EmailAccountId = deployment.EmailAccountId
                };

                foreach (var file in files)
                {
                    var name       = file.Name;
                    var attachment = new QueuedEmailAttachment
                    {
                        StorageLocation = EmailAttachmentStorageLocation.Blob,
                        Name            = name,
                        MimeType        = MimeTypes.MapNameToMimeType(name)
                    };

                    using var item = MediaStorageItem.FromFile(file);
                    await _dbMediaStorageProvider.ApplyBlobAsync(attachment, item, false);

                    queuedEmail.Attachments.Add(attachment);
                }

                _db.QueuedEmails.Add(queuedEmail);

                // Blob data could be large, so better not bulk commit here.
                num += await _db.SaveChangesAsync(cancellationToken);
            }

            context.Log.Info($"{num} email(s) created and queued for deployment.");
        }
Beispiel #2
0
        public async Task PublishAsync(ExportDeployment deployment, ExportDeploymentContext context, CancellationToken cancelToken)
        {
            var succeededFiles = 0;
            var url            = deployment.Url;
            var files          = await context.GetDeploymentFilesAsync(cancelToken);

            if (!url.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) &&
                !url.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
            {
                url = "http://" + url;
            }

            var uri = new Uri(url);

            if (deployment.HttpTransmissionType == ExportHttpTransmissionType.MultipartFormDataPost)
            {
                var num         = 0;
                var credentials = deployment.Username.HasValue()
                    ? new NetworkCredential(deployment.Username, deployment.Password)
                    : null;

                using var handler  = new HttpClientHandler { Credentials = credentials };
                using var client   = new HttpClient(handler);
                using var formData = new MultipartFormDataContent();

                foreach (var file in files)
                {
                    var bytes = await file.ReadAllBytesAsync();

                    formData.Add(new ByteArrayContent(bytes), "file {0}".FormatInvariant(++num), file.Name);
                }

                var response = await client.PostAsync(uri, formData, cancelToken);

                if (response.IsSuccessStatusCode)
                {
                    succeededFiles = num;
                }
                else if (response.Content != null)
                {
                    context.Result.LastError = context.T("Admin.Common.HttpStatus", (int)response.StatusCode, response.StatusCode.ToString());

                    var content = await response.Content.ReadAsStringAsync(cancelToken);

                    var msg = "Multipart form data upload failed. HTTP status {0} ({1}). Response: {2}".FormatInvariant(
                        (int)response.StatusCode, response.StatusCode.ToString(), content.NaIfEmpty().Truncate(2000, "..."));

                    context.Log.Error(msg);
                }
            }
            else
            {
                using var webClient = new WebClient();

                if (deployment.Username.HasValue())
                {
                    webClient.Credentials = new NetworkCredential(deployment.Username, deployment.Password);
                }

                foreach (var file in files)
                {
                    await webClient.UploadFileTaskAsync(uri, file.PhysicalPath);

                    ++succeededFiles;
                }
            }

            context.Log.Info($"{succeededFiles} file(s) successfully uploaded via HTTP ({deployment.HttpTransmissionType}).");
        }