Exemple #1
0
        public virtual void Publish(ExportDeploymentContext context, ExportDeployment deployment)
        {
            var destinationFolder = deployment.GetDeploymentFolder(true);

            if (destinationFolder.IsEmpty())
            {
                return;
            }

            if (!System.IO.Directory.Exists(destinationFolder))
            {
                System.IO.Directory.CreateDirectory(destinationFolder);
            }

            if (context.CreateZipArchive)
            {
                if (File.Exists(context.ZipPath))
                {
                    var destinationFile = Path.Combine(destinationFolder, Path.GetFileName(context.ZipPath));

                    File.Copy(context.ZipPath, destinationFile, true);

                    context.Log.Information("Copied zipped export data to " + destinationFile);
                }
            }
            else
            {
                if (!FileSystemHelper.CopyDirectory(new DirectoryInfo(context.FolderContent), new DirectoryInfo(destinationFolder)))
                {
                    context.Result.LastError = context.T("Admin.DataExchange.Export.Deployment.CopyFileFailed");
                }

                context.Log.Information("Copied export data files to " + destinationFolder);
            }
        }
        public virtual void Publish(ExportDeploymentContext context, ExportDeployment deployment)
        {
            var targetFolder = deployment.GetDeploymentFolder(true);

            if (!FileSystemHelper.CopyDirectory(new DirectoryInfo(context.FolderContent), new DirectoryInfo(targetFolder)))
            {
                context.Result.LastError = context.T("Admin.DataExchange.Export.Deployment.CopyFileFailed");
            }

            context.Log.Information("Copied export data files to " + targetFolder);
        }
Exemple #3
0
        private bool UploadFile(string path, string fileUrl, bool keepAlive = true)
        {
            var succeeded  = false;
            var bytesRead  = 0;
            var buffLength = 32768;

            byte[] buff = new byte[buffLength];

            var request = CreateRequest(fileUrl, keepAlive, (new FileInfo(path)).Length);

            request.Method = WebRequestMethods.Ftp.UploadFile;

            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;
                succeeded = statusCode >= 200 && statusCode <= 299;

                if (succeeded)
                {
                    ++_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));
                }
            }

            return(succeeded);
        }
        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));
        }
        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()));
        }