Пример #1
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.Info("{0} file(s) successfully uploaded via HTTP ({1}).".FormatInvariant(succeededFiles, deployment.HttpTransmissionType.ToString()));
        }
Пример #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.DeploymentFiles.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)System.Net.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
                    {
                        var msg = "The FTP transfer might fail. {0} ({1}), {2}. File {3}".FormatInvariant(
                            response.StatusCode.ToString(), statusCode, response.StatusDescription.NaIfEmpty(), path);

                        context.Log.Error(msg);
                    }
                }
            }

            context.Log.Information("{0} file(s) successfully uploaded via FTP.".FormatInvariant(succeededFiles));
        }