Ejemplo n.º 1
0
        public virtual void Publish(ExportDeploymentContext context, ExportDeployment deployment)
        {
            var emailAccount = _emailAccountService.GetEmailAccountById(deployment.EmailAccountId);
            var smtpContext  = new SmtpContext(emailAccount);
            var count        = 0;

            foreach (var email in deployment.EmailAddresses.SplitSafe(",").Where(x => x.IsEmail()))
            {
                var queuedEmail = new QueuedEmail
                {
                    From           = emailAccount.ToEmailAddress(),
                    SendManually   = false,
                    To             = email,
                    Subject        = deployment.EmailSubject.NaIfEmpty(),
                    Body           = deployment.EmailSubject.NaIfEmpty(),
                    CreatedOnUtc   = DateTime.UtcNow,
                    EmailAccountId = deployment.EmailAccountId
                };

                foreach (var path in context.GetDeploymentFiles())
                {
                    var name = Path.GetFileName(path);

                    queuedEmail.Attachments.Add(new QueuedEmailAttachment
                    {
                        StorageLocation = EmailAttachmentStorageLocation.Blob,
                        MediaStorage    = new MediaStorage {
                            Data = File.ReadAllBytes(path)
                        },
                        Name     = name,
                        MimeType = MimeTypes.MapNameToMimeType(name)
                    });
                }

                _queuedEmailService.InsertQueuedEmail(queuedEmail);
                ++count;
            }

            context.Log.Info("{0} email(s) created and queued for deployment.".FormatInvariant(count));
        }
Ejemplo n.º 2
0
        public virtual void Publish(ExportDeploymentContext context, ExportDeployment deployment)
        {
            var bytesRead      = 0;
            var succeededFiles = 0;
            var url            = deployment.Url;
            var buffLength     = 32768;

            byte[] buff            = new byte[buffLength];
            var    deploymentFiles = context.GetDeploymentFiles().ToList();
            var    lastIndex       = (deploymentFiles.Count - 1);

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

            foreach (var path in deploymentFiles)
            {
                var fileUrl = url.EnsureEndsWith("/") + Path.GetFileName(path);

                var request = (FtpWebRequest)WebRequest.Create(fileUrl);
                request.Method     = WebRequestMethods.Ftp.UploadFile;
                request.KeepAlive  = (deploymentFiles.IndexOf(path) != lastIndex);
                request.UseBinary  = true;
                request.Proxy      = null;
                request.UsePassive = deployment.PassiveMode;
                request.EnableSsl  = deployment.UseSsl;

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

                request.ContentLength = (new FileInfo(path)).Length;

                var requestStream = request.GetRequestStream();

                using (var stream = new FileStream(path, FileMode.Open))
                {
                    while ((bytesRead = stream.Read(buff, 0, buffLength)) != 0)
                    {
                        requestStream.Write(buff, 0, bytesRead);
                    }
                }

                requestStream.Close();

                using (var response = (FtpWebResponse)request.GetResponse())
                {
                    var statusCode = (int)response.StatusCode;

                    if (statusCode >= 200 && statusCode <= 299)
                    {
                        ++succeededFiles;
                    }
                    else
                    {
                        context.Result.LastError = context.T("Admin.Common.FtpStatus", statusCode, response.StatusCode.ToString());

                        context.Log.Error("The FTP transfer failed. FTP status {0} ({1}). File {3}".FormatInvariant(statusCode, response.StatusCode.ToString(), path));
                    }
                }
            }

            context.Log.Info("{0} file(s) successfully uploaded via FTP.".FormatInvariant(succeededFiles));
        }
Ejemplo n.º 3
0
        public virtual void Publish(ExportDeploymentContext context, ExportDeployment deployment)
        {
            var succeededFiles = 0;
            var url            = deployment.Url;

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

            if (deployment.HttpTransmissionType == ExportHttpTransmissionType.MultipartFormDataPost)
            {
                var          countFiles  = 0;
                ICredentials credentials = null;

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

                using (var handler = new HttpClientHandler {
                    Credentials = credentials
                })
                    using (var client = new HttpClient(handler))
                        using (var formData = new MultipartFormDataContent())
                        {
                            foreach (var path in context.GetDeploymentFiles())
                            {
                                byte[] fileData = File.ReadAllBytes(path);
                                formData.Add(new ByteArrayContent(fileData), "file {0}".FormatInvariant(++countFiles), Path.GetFileName(path));
                            }

                            var response = client.PostAsync(url, formData).Result;

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

                                var content = response.Content.ReadAsStringAsync().Result;

                                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 path in context.GetDeploymentFiles())
                    {
                        webClient.UploadFile(url, path);

                        ++succeededFiles;
                    }
                }
            }

            context.Log.Information("{0} file(s) successfully uploaded via HTTP ({1}).".FormatInvariant(succeededFiles, deployment.HttpTransmissionType.ToString()));
        }