Exemple #1
0
 /// <summary>
 /// 创建文件夹
 /// </summary>
 /// <param name="dirName"></param>
 public void MakeDir(string dirName)
 {
     try
     {
         ftpConnection.CreateDirectory(dirName);
     }
     catch
     {
         throw;
     }
 }
Exemple #2
0
        /// <summary>
        /// Creates a new Ftp directory on the ftp server.
        /// </summary>
        private void CreateDirectory()
        {
            if (string.IsNullOrEmpty(this.RemoteDirectoryName))
            {
                this.Log.LogError("The required RemoteDirectoryName attribute has not been set for FTP.");
                return;
            }

            using (FtpConnection ftpConnection = this.CreateFtpConnection())
            {
                ftpConnection.LogOn();
                this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Creating Directory: {0}", this.RemoteDirectoryName));
                try
                {
                    ftpConnection.CreateDirectory(this.RemoteDirectoryName);
                }
                catch (FtpException ex)
                {
                    if (ex.Message.Contains("550"))
                    {
                        return;
                    }

                    this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "There was an error creating ftp directory: {0}. The Error Details are \"{1}\" and error code is {2} ", this.RemoteDirectoryName, ex.Message, ex.ErrorCode));
                }
            }
        }
Exemple #3
0
        private void SendAddOnToServer(List <string> files, String folderPath)
        {
            Console.WriteLine("Sending to server");
            TextReader tr   = new StreamReader(@"C:\Users\Pilus\Documents\Cloud Backup\PrologueServer.txt");
            string     addr = tr.ReadLine();
            string     user = tr.ReadLine();
            string     pass = tr.ReadLine();


            using (FtpConnection ftp = new FtpConnection(addr, user, pass)) {
                ftp.Open();  /* Open the FTP connection */
                ftp.Login(); /* Login using previously provided credentials */

                int c          = 0;
                int c2         = 0;
                int transfered = 0;

                foreach (String file in files)
                {
                    try {
                        // Check if the directory exists
                        String relativeFilePath = file.Replace(folderPath, "").Replace(@"\", "/");
                        String totalDirPath     = relativeFilePath.Substring(0, relativeFilePath.LastIndexOf("/"));
                        String folder           = totalDirPath.Substring(totalDirPath.LastIndexOf("/") + 1);
                        String topDir           = "/" + totalDirPath.Replace("/" + folder, "");
                        if (!ftp.GetCurrentDirectory().Equals("/" + totalDirPath))
                        {
                            ftp.SetCurrentDirectory(topDir);
                            if (!ftp.DirectoryExists(folder))
                            {
                                ftp.CreateDirectory(folder);
                            }
                        }



                        if (PutFile(ftp, file) == true)
                        {
                            transfered++;
                        }

                        c++;
                        int c3 = (c / (files.Count / 20));
                        if (c3 > c2)
                        {
                            Console.Write("{0}% ", c3 * 5);
                            c2 = c3;
                        }
                    }
                    catch (FtpException e) {
                        Console.WriteLine(String.Format("FTP Error: {0} {1}", e.ErrorCode, e.Message));
                    }
                }

                Console.WriteLine("{0} files created/updated.", transfered);
            }
        }
Exemple #4
0
        public void PublishToGitFTP(DeploymentModel model)
        {
            if (model.AzureDeployment)
            {
                var remoteProcess =
                    Process.Start("\"" + gitLocation + "\"", " --git-dir=\"" + fullRepoPath + "\" remote add blog " + model.AzureRepo);
                if (remoteProcess != null)
                {
                    remoteProcess.WaitForExit();
                }

                var pushProcess = Process.Start("\"" + gitLocation + "\"", " --git-dir=\"" + fullRepoPath + "\" push -f blog master");
                if (pushProcess != null)
                {
                    pushProcess.WaitForExit();
                }
            }
            else
            {
                using (ftp = new FtpConnection(model.FTPServer, model.FTPUsername, model.FTPPassword))
                {
                    try
                    {
                        ftp.Open();
                        ftp.Login();

                        if (!string.IsNullOrWhiteSpace(model.FTPPath))
                        {
                            if (!ftp.DirectoryExists(model.FTPPath))
                            {
                                ftp.CreateDirectory(model.FTPPath);
                            }

                            ftp.SetCurrentDirectory(model.FTPPath);
                        }

                        FtpBlogFiles(snowPublishPath, model.FTPPath);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
Exemple #5
0
        private void FtpBlogFiles(string dirPath, string uploadPath)
        {
            string[] files   = Directory.GetFiles(dirPath, "*.*");
            string[] subDirs = Directory.GetDirectories(dirPath);


            foreach (string file in files)
            {
                ftp.PutFile(file, Path.GetFileName(file));
            }

            foreach (string subDir in subDirs)
            {
                if (!ftp.DirectoryExists(uploadPath + "/" + Path.GetFileName(subDir)))
                {
                    ftp.CreateDirectory(uploadPath + "/" + Path.GetFileName(subDir));
                }

                ftp.SetCurrentDirectory(uploadPath + "/" + Path.GetFileName(subDir));

                FtpBlogFiles(subDir, uploadPath + "/" + Path.GetFileName(subDir));
            }
        }
Exemple #6
0
        public void PublishToGitFTP(DeploymentModel model)
        {
            if (model.GitDeployment)
            {
                Logger.Debug("Executing git add");

                var addProcess          = new Process();
                var addProcessStartInfo = new ProcessStartInfo("\"" + gitLocation + "\"")
                {
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    UseShellExecute        = false,
                    Arguments = " --git-dir=\"" + fullPublishGitPath + "\" --work-tree=\"" + publishGitPath + "\" add -A"
                };
                addProcess.StartInfo           = addProcessStartInfo;
                addProcess.OutputDataReceived += (sender, args) => Logger.Debug(args.Data);
                addProcess.ErrorDataReceived  += (sender, args) => Logger.Debug(args.Data);
                addProcess.Start();
                addProcess.BeginOutputReadLine();
                addProcess.BeginErrorReadLine();
                addProcess.WaitForExit();

                Logger.Debug("git add process to exited");

                Logger.Debug("Executing git email config process");

                var emailProcess          = new Process();
                var emailProcessStartInfo = new ProcessStartInfo("\"" + gitLocation + "\"")
                {
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    UseShellExecute        = false,
                    Arguments = " --git-dir=\"" + fullPublishGitPath + "\" --work-tree=\"" + publishGitPath + "\" config user.email \"[email protected]\""
                };
                emailProcess.StartInfo           = emailProcessStartInfo;
                emailProcess.OutputDataReceived += (sender, args) => Logger.Debug(args.Data);
                emailProcess.ErrorDataReceived  += (sender, args) => Logger.Debug(args.Data);
                emailProcess.Start();
                emailProcess.BeginOutputReadLine();
                emailProcess.BeginErrorReadLine();
                emailProcess.WaitForExit();
                Logger.Debug("git email config process to exited");

                Logger.Debug("Executing git name config process");

                var userProcess          = new Process();
                var userProcessStartInfo = new ProcessStartInfo("\"" + gitLocation + "\"")
                {
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    UseShellExecute        = false,
                    Arguments = " --git-dir=\"" + fullPublishGitPath + "\" --work-tree=\"" + publishGitPath + "\" config user.name \"barbato\""
                };
                userProcess.StartInfo           = userProcessStartInfo;
                userProcess.OutputDataReceived += (sender, args) => Logger.Debug(args.Data);
                userProcess.ErrorDataReceived  += (sender, args) => Logger.Debug(args.Data);
                userProcess.Start();
                userProcess.BeginOutputReadLine();
                userProcess.BeginErrorReadLine();
                userProcess.WaitForExit();

                Logger.Debug("git name config process to exited");

                Logger.Debug("Executing git commit");

                var commitProcess          = new Process();
                var commitProcessStartInfo = new ProcessStartInfo("\"" + gitLocation + "\"")
                {
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    UseShellExecute        = false,
                    Arguments = " --git-dir=\"" + fullPublishGitPath + "\" --work-tree=\"" + publishGitPath +
                                "\" commit -a -m \"Static Content Regenerated\""
                };
                commitProcess.StartInfo           = commitProcessStartInfo;
                commitProcess.OutputDataReceived += (sender, args) => Logger.Debug(args.Data);
                commitProcess.ErrorDataReceived  += (sender, args) => Logger.Debug(args.Data);
                commitProcess.Start();
                commitProcess.BeginOutputReadLine();
                commitProcess.BeginErrorReadLine();
                commitProcess.WaitForExit();

                Logger.Debug("git commit process to exited");

                Logger.Debug("Executing git push");

                var pushProcess          = new Process();
                var pushProcessStartInfo = new ProcessStartInfo("\"" + gitLocation + "\"")
                {
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    UseShellExecute        = false,
                    Arguments = " --git-dir=\"" + fullPublishGitPath + "\" push -f origin master"
                };
                pushProcess.OutputDataReceived += (sender, args) => Logger.Debug(args.Data);
                pushProcess.ErrorDataReceived  += (sender, args) => Logger.Debug(args.Data);
                pushProcess.StartInfo           = pushProcessStartInfo;
                pushProcess.Start();
                pushProcess.BeginOutputReadLine();
                pushProcess.BeginErrorReadLine();
                pushProcess.WaitForExit();

                Logger.Debug("git push process to exited");
            }
            else
            {
                using (ftp = new FtpConnection(model.FTPServer, model.FTPUsername, model.FTPPassword))
                {
                    try
                    {
                        ftp.Open();
                        ftp.Login();

                        if (!string.IsNullOrWhiteSpace(model.FTPPath))
                        {
                            var parrentDirectory = String.Format("/{0}", Path.GetDirectoryName(model.FTPPath).Replace(Path.DirectorySeparatorChar, '/'));
                            /* Get name of the directory */
                            var checkingDirectory = String.Format("{0}", Path.GetFileName(model.FTPPath)).ToLower();
                            /* Get all child directories info of the parent directory */
                            var ftpDirectories = ftp.GetDirectories(parrentDirectory);
                            /* check if the given directory exists in the returned result */
                            var exists = ftpDirectories.Any(d => d.Name.ToLower() == checkingDirectory);

                            if (!exists)
                            {
                                ftp.CreateDirectory(model.FTPPath);
                            }

                            ftp.SetCurrentDirectory(model.FTPPath);
                        }

                        FtpBlogFiles(publishGitPath, model.FTPPath);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
Exemple #7
0
        private ActionResult FtpUpload(IJob job)
        {
            var actionResult = Check(job.Profile);

            if (!actionResult)
            {
                Logger.Error("Canceled FTP upload action.");
                return(actionResult);
            }

            if (string.IsNullOrEmpty(job.Passwords.FtpPassword))
            {
                Logger.Error("No ftp password specified in action");
                return(new ActionResult(ActionId, 102));
            }

            Logger.Debug("Creating ftp connection.\r\nServer: " + job.Profile.Ftp.Server + "\r\nUsername: "******"Can not connect to the internet for login to ftp. Win32Exception Message:\r\n" +
                                 ex.Message);
                    ftp.Close();
                    return(new ActionResult(ActionId, 108));
                }

                Logger.Error("Win32Exception while login to ftp server:\r\n" + ex.Message);
                ftp.Close();
                return(new ActionResult(ActionId, 104));
            }
            catch (Exception ex)
            {
                Logger.Error("Exception while login to ftp server:\r\n" + ex.Message);
                ftp.Close();
                return(new ActionResult(ActionId, 104));
            }

            var fullDirectory = job.TokenReplacer.ReplaceTokens(job.Profile.Ftp.Directory).Trim();

            if (!IsValidPath(fullDirectory))
            {
                Logger.Warn("Directory contains invalid characters \"" + fullDirectory + "\"");
                fullDirectory = MakeValidPath(fullDirectory);
            }

            Logger.Debug("Directory on ftp server: " + fullDirectory);

            var directories = fullDirectory.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);

            try
            {
                foreach (var directory in directories)
                {
                    if (!ftp.DirectoryExists(directory))
                    {
                        Logger.Debug("Create folder: " + directory);
                        ftp.CreateDirectory(directory);
                    }

                    Logger.Debug("Move to: " + directory);
                    ftp.SetCurrentDirectory(directory);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception while setting directory on ftp server\r\n:" + ex.Message);
                ftp.Close();
                return(new ActionResult(ActionId, 105));
            }

            var addendum = "";

            if (job.Profile.Ftp.EnsureUniqueFilenames)
            {
                Logger.Debug("Generate addendum for unique filename");
                try
                {
                    addendum = AddendumForUniqueFilename(Path.GetFileName(job.OutputFiles[0]), ftp);
                    Logger.Debug("The addendum for unique filename is \"" + addendum +
                                 "\" If empty, the file was already unique.");
                }
                catch (Exception ex)
                {
                    Logger.Error("Exception while generating unique filename\r\n:" + ex.Message);
                    ftp.Close();
                    return(new ActionResult(ActionId, 106));
                }
            }

            foreach (var file in job.OutputFiles)
            {
                try
                {
                    var targetFile = Path.GetFileNameWithoutExtension(file) + addendum + Path.GetExtension(file);
                    ftp.PutFile(file, MakeValidPath(targetFile));
                }
                catch (Exception ex)
                {
                    Logger.Error("Exception while uploading the file \"" + file + "\": \r\n" + ex.Message);
                    ftp.Close();
                    return(new ActionResult(ActionId, 107));
                }
            }

            ftp.Close();
            return(new ActionResult());
        }
 public void CreateDirectory(string path)
 {
     _ftpConnection.CreateDirectory(path);
 }