Exemplo n.º 1
0
        public void Download(
            string sourcePath,
            string destPath,
            string searchPattern   = "*",
            bool deleteAfter       = false,
            bool maintainStructure = true
            )
        {
            Check.NotNullOrWhiteSpace(
                () => sourcePath,
                () => destPath
                );

            var logger        = new Logger();
            var transferWatch = new Stopwatch();
            var sourceFtp     = new Ftp(this._host, this._userName, this._password);

            if (!Directory.Exists(destPath))
            {
                Directory.CreateDirectory(destPath);
            }

            logger.Log("    > Verify if Has Extension...\r\n");
            if (Path.HasExtension(sourcePath))
            {
                transferWatch.Start();

                logger.Log(
                    string.Format("      > Host: {0}\r\nFile: {1} ...\r\n", this._host, sourcePath),
                    Enumerators.LogColor.Success
                    );
                logger.Log("      > Dowloading File...\r\n");

                sourceFtp.Download(sourcePath, destPath);

                if (deleteAfter)
                {
                    logger.Log("      > Deleting File...\r\n");
                    sourceFtp.DeleteFile(sourcePath);
                }

                logger.Log(
                    string.Format(
                        "      > Transfer File Duration: {0}...\r\n",
                        transferWatch.Elapsed
                        )
                    );
                logger.Log("      > Transfer File Complete...\r\n", Enumerators.LogColor.Success);
            }
            else
            {
                logger.Log(
                    string.Format("    > Getting Files ({0})...\r\n", sourcePath)
                    );

                var files = sourceFtp.GetFiles(sourcePath, searchPattern, SearchOption.AllDirectories);

                transferWatch.Start();

                logger.Log("    > Start Dowloading Files...\r\n");
                foreach (var file in files)
                {
                    logger.Log(
                        string.Format("      > File: {0} ...\r\n", file.FullName),
                        Enumerators.LogColor.Success
                        );

                    var path = destPath;

                    if (maintainStructure && !Path.HasExtension(path))
                    {
                        logger.Log("      > Creating the Structure...\r\n");
                        var relativePath = Transfer.GetRelativePath(sourcePath, file.DirectoryName);
                        if (!string.IsNullOrWhiteSpace(relativePath))
                        {
                            path = string.Concat(
                                path,
                                Path.DirectorySeparatorChar,
                                relativePath
                                );
                            Directory.CreateDirectory(path);
                        }
                    }

                    logger.Log("      > Dowloading File...\r\n");
                    sourceFtp.Download(file.RelativePath, path);

                    if (deleteAfter)
                    {
                        logger.Log("      > Deleting File...\r\n");
                        file.Delete();
                    }

                    logger.Log(
                        string.Format(
                            "      > Transfer File Duration: {0}...\r\n",
                            transferWatch.Elapsed
                            )
                        );
                    logger.Log("      > Transfer File Complete...\r\n", Enumerators.LogColor.Success);
                    transferWatch.Restart();
                }
            }

            transferWatch.Stop();
        }
Exemplo n.º 2
0
        private Ftp Connect()
        {
            var ftpConnect = new Ftp(this._host, this._userName, this._password);

            return(ftpConnect);
        }