Exemplo n.º 1
0
 public FtpUtil(string host, string username, string password)
 {
     _host = new FtpDirectory();
     Host = host;
     Username = username;
     Password = password;
 }
Exemplo n.º 2
0
        public void CreateFolderTest()
        {
            var root = new FtpDirectory(FTP_BASE_URL, credential);
            var dir  = root.GetDirectory("YC.FTP");

            dir.CreateSubdirectory("test");
        }
Exemplo n.º 3
0
 public FtpUtil(string host, string username, string password)
 {
     _host    = new FtpDirectory();
     Host     = host;
     Username = username;
     Password = password;
 }
Exemplo n.º 4
0
 public void MoveFile()
 {
     var dir   = new FtpDirectory($"{FTP_BASE_URL}/YC.FTP", credential);
     var items = dir?.GetFiles()
                 .FirstOrDefault()
                 ?.MoveTo("/YC.FTP/FILE2");
 }
Exemplo n.º 5
0
        public void GetItemsTest()
        {
            var dir   = new FtpDirectory($"{FTP_BASE_URL}localhost/YC.FTP", credential);
            var items = dir?.GetItems()
                        .Select(p => p.FullName);

            Console.WriteLine(string.Join(",", items));
        }
        public void TestCreation1()
        {
            var ftpDirectory  = FtpDirectory.Create("/foo/");
            var directoryName = ftpDirectory.DirectoryName;

            Assert.AreEqual("foo",
                            directoryName);
        }
Exemplo n.º 7
0
        public void DeleteFolderTest()
        {
            var dir = new FtpDirectory($"{FTP_BASE_URL}/YC.FTP/test", credential);

            if (dir?.Exists == true)
            {
                dir.Delete(true);
            }
        }
        public void TestCreation5()
        {
            var ftpDirectory        = FtpDirectory.Create("/foo1/foo2/foo3");
            var parentFtpDirectory  = ftpDirectory.GetParentFtpDirectory();
            var parentDirectoryName = parentFtpDirectory.DirectoryName;

            Assert.AreEqual("foo2",
                            parentDirectoryName);
        }
        public void TestCreation4()
        {
            var rootFtpDirectory = FtpDirectory.Root;
            var ftpDirectory     = FtpDirectory.Create(rootFtpDirectory,
                                                       "test2");
            var directoryName = ftpDirectory.DirectoryName;

            Assert.AreEqual("test2",
                            directoryName);
        }
Exemplo n.º 10
0
        public void DeleteFileTest()
        {
            var root = new FtpDirectory(FTP_BASE_URL, credential);
            var dir  = root.GetDirectory("YC.FTP");
            var file = dir.GetFile("FILE");

            if (file?.Exists == true)
            {
                file.Delete();
            }
        }
Exemplo n.º 11
0
        public void AppendFileTest()
        {
            var root   = new FtpDirectory(FTP_BASE_URL, credential);
            var dir    = root.GetDirectory("YC.FTP");
            var file   = dir.GetFile("FILE") ?? dir.CreateFile("FILE");
            var stream = file.OpenAppend();
            var writer = new StreamWriter(stream);

            writer.WriteLine("Hello World, " + DateTime.Now.ToString());
            writer.WriteLine(string.Join("", Enumerable.Repeat(" ", 2048)));
            writer.Flush();
            writer.Close();
        }
Exemplo n.º 12
0
        public void TestDirectoryChanges3()
        {
            var sourceFtpDirectory = FtpDirectory.Create("/foo/foo1");
            var targetFtpDirectory = FtpDirectory.Create("/foo/foo2/foo3");
            var directoryChanges   = FtpClientHelper.DirectoryChanges(sourceFtpDirectory,
                                                                      targetFtpDirectory);

            var joinedDirectoryChanges = string.Join(",",
                                                     directoryChanges);

            Assert.AreEqual("..,foo2,foo3",
                            joinedDirectoryChanges);
        }
        public async Task Should_delete_deep_structure()
        {
            await ResourceHelpers.CreateFileWithContentAsync($"folder1/folder2/folder3/{Guid.NewGuid()}.txt", "stuff", Logger);

            using (var ftpClient = ResourceHelpers.GetLocalFtpClient(Logger))
            {
                ftpClient.Logger = Logger;
                var sut = new FtpDirectory(ftpClient, "folder1");
                await sut.DeleteAsync();

                var folders = await sut.GetDirectoriesAsync();

                folders.Any(x => x.Name == "folder1").Should().BeFalse();
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Build the URI to use for the FTP request.
        /// </summary>
        /// <param name="fileName">
        /// The name of the file to transfer, if any. Defaults to null.
        /// </param>
        /// <returns>
        /// The URI to use for the FTP request.
        /// </returns>
        private Uri BuildFtpRequestUri(string fileName = null)
        {
            // Build the fully qualified URI string, ensuring proper number of slashes after each segment.
            StringBuilder stringBuilder = new StringBuilder("ftp://");

            stringBuilder.Append(AddressAuthority.Trim('/'));
//TODO: Need double slash after port?
            stringBuilder.Append("/");
            stringBuilder.Append(FtpDirectory.Trim('/'));
            if (String.IsNullOrWhiteSpace(fileName) == false)
            {
                stringBuilder.Append("/");
                stringBuilder.Append(fileName.Trim('/'));
            }

            return(new Uri(stringBuilder.ToString()));
        }
Exemplo n.º 15
0
        public static IEnumerable <string> GetHierarchy(this FtpDirectory ftpDirectory)
        {
            if (ftpDirectory == null)
            {
                return(Enumerable.Empty <string>());
            }
            var fullName = ftpDirectory.FullName;

            if (string.IsNullOrEmpty(fullName))
            {
                return(Enumerable.Empty <string>());
            }
            var directories = fullName.Split(new[]
            {
                Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar
            },
                                             StringSplitOptions.RemoveEmptyEntries);

            return(directories);
        }
Exemplo n.º 16
0
        internal FtpFileTransferer(FtpDirectory transferStarter, string localFile, string remoteFile, long totalBytes, TransferDirection dir)
        {
            m_transferStarter = transferStarter;
            m_transferDirection = dir;
            m_session = transferStarter.Session;
            m_localFile = localFile;
            m_remoteFile = remoteFile;
            m_totalBytes = totalBytes;

            if (dir == TransferDirection.Upload)
            {
                m_streamCopyRoutine = LocalToRemote;
                m_ftpFileCommandRoutine = m_session.ControlChannel.STOR;
                m_localFileOpenMode = FileMode.Open;
            }
            else
            {
                m_streamCopyRoutine = RemoteToLocal;
                m_ftpFileCommandRoutine = m_session.ControlChannel.RETR;
                m_localFileOpenMode = FileMode.Create;
            }
        }
Exemplo n.º 17
0
        public FileFilter CopyFromFtp(FtpDirectory ftpDir, string destinationDirectory)
        {
            using (FtpConnection ftpConnection = new FtpConnection(ftpDir.Host, ftpDir.Port, ftpDir.Username, ftpDir.Password))
            {
                ftpConnection.Open();
                ftpConnection.Login();
                foreach (string ftpPath in GetFtpFilesAndFolders(ftpDir.BaseDirectory, ftpConnection))
                {
                    if (ftpConnection.DirectoryExists(ftpPath))
                    {
                        var combinedPath = Path.Combine(destinationDirectory, PathWithoutBaseDirectory(ftpPath, ftpDir.BaseDirectory).TrimStart('/'));
                        if (!Directory.Exists(combinedPath))
                        {
                            Directory.CreateDirectory(combinedPath);
                        }
                    }
                    else
                    {
                        if (!Directory.Exists(destinationDirectory))
                        {
                            Directory.CreateDirectory(destinationDirectory);
                        }

                        var combinedPath = Path.Combine(destinationDirectory, PathWithoutBaseDirectory(ftpPath, ftpDir.BaseDirectory).TrimStart('/'));
                        var newPath = Path.GetDirectoryName(combinedPath);
                        if (!Directory.Exists(newPath))
                        {
                            Directory.CreateDirectory(newPath);
                        }

                        ftpConnection.GetFile(ftpPath, combinedPath, false);

                    }
                }
                ftpConnection.Close();
            }

            return this;
        }
Exemplo n.º 18
0
        public IEnumerable <FtpItem> GetDirectoryContents()
        {
            // Intitialize File Transfer
            this.LIST();

            // Read Bytes from Stream, save them to disk
            var result = new StreamReader(Data.Stream).ReadToEnd();

            foreach (string line in result.Split(new [] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (line.StartsWith("d"))
                {
                    yield return(FtpDirectory.Parse(line));
                }
                else
                {
                    yield return(FtpFile.Parse(line));
                }
            }

            // Wait for Confirmation on File Transfer. Stream will be closed by the Server!
            MessageHandler.WaitOne();
        }
Exemplo n.º 19
0
        public FtpFile(FtpDirectory directory,
                       string path)
        {
            if (null == directory)
            {
                throw new ArgumentNullException("directory");
            }

            if (null == path)
            {
                throw new ArgumentNullException("path");
            }

            if (path.Trim().IsEmpty())
            {
                throw new ArgumentOutOfRangeException("path");
            }

            Trace.WriteLineIf(Tracing.Is.TraceVerbose, "[FTP] directory={0} path={1}".FormatWith(directory.Location, path));

            Directory = directory;
            Path = path;
        }
Exemplo n.º 20
0
 public static IFile ParseResponseLine(string line)
 {
     var listDirectoryLine = new ListDirectoryLine(line);
     IFile file;
     switch (listDirectoryLine.Type)
     {
         case ListDirectoryLine.ItemType.Directory:
             file = new FtpDirectory();
             break;
         case ListDirectoryLine.ItemType.File:
             file = new FtpFile
             {
                 Size = listDirectoryLine.Size
             };
             break;
         case ListDirectoryLine.ItemType.FakeDirectory:
             return NullDirectory.Instance;
         default:
             throw new ArgumentException();
     }
     file.Name = listDirectoryLine.Name;
     return file;
 }
Exemplo n.º 21
0
        public FileFilter CopyToFtp(string sourceDirectory, FtpDirectory ftpDir)
        {
            using (FtpConnection ftpConnection = new FtpConnection(ftpDir.Host, ftpDir.Port, ftpDir.Username, ftpDir.Password))
            {
                ftpConnection.Open();
                ftpConnection.Login();
                foreach (WrappedFileSystemInfo fileSystemInfo in GetFilesAndFolders(sourceDirectory)) {
                    if (fileSystemInfo is WrappedDirectoryInfo)
                    {
                        var combinedPath = Path.Combine(ftpDir.BaseDirectory, fileSystemInfo.PathWithoutBaseDirectory);
                        if (!ftpConnection.DirectoryExists(combinedPath))
                            ftpConnection.CreateDirectory(combinedPath);

                    }
                    else
                    {
                        if (!ftpConnection.DirectoryExists(ftpDir.BaseDirectory)) {
                            ftpConnection.CreateDirectory(ftpDir.BaseDirectory);

                        }

                        var combinedPath = Path.Combine(ftpDir.BaseDirectory, fileSystemInfo.PathWithoutBaseDirectory);
                        var newPath = Path.GetDirectoryName(combinedPath);
                        if (!ftpConnection.DirectoryExists(newPath))
                        {
                            ftpConnection.CreateDirectory(newPath);
                        }

                        ftpConnection.SetCurrentDirectory(newPath);
                        ftpConnection.PutFile(fileSystemInfo.FullName);
                    }
                }
            }

            return this;
        }
Exemplo n.º 22
0
 internal void InitRootDirectory()
 {
     m_root = new FtpDirectory(this, m_caseInsensitive, m_ctrlChannel.PWD());
     m_current = m_root;
 }
Exemplo n.º 23
0
        // Construction

        #region C'tors

        public FtpUtil()
        {
            _host = new FtpDirectory();
        }
Exemplo n.º 24
0
 public FcChangeDirectory(FtpDirectory CurrentDir, FtpDirectory TargetDir)
 {
     this.CurrentDir = TargetDir;
     this.TargetDir  = TargetDir;
 }
Exemplo n.º 25
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="FtpSessionConnected"/> object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                try
                {
                    // This will be done regardless of whether the object is finalized or disposed.

                    if (disposing)
                    {
                        m_host = null;
                        m_root = null;
                        m_current = null;

                        if ((object)m_ctrlChannel != null)
                            m_ctrlChannel.Close();

                        m_ctrlChannel = null;

                        if ((object)m_dataStream != null)
                            m_dataStream.Dispose();

                        m_dataStream = null;
                    }
                }
                finally
                {
                    m_disposed = true;  // Prevent duplicate dispose.
                }
            }
        }
Exemplo n.º 26
0
 /// <summary>
 /// Creates a new <see cref="FtpFile"/>.
 /// </summary>
 /// <param name="parent">Parent directory of file.</param>
 protected FtpFile(FtpDirectory parent)
 {
     Parent = parent;
 }
Exemplo n.º 27
0
        public void DeleteFromFtp(FtpDirectory ftpDir)
        {
            using (FtpConnection ftpConnection = new FtpConnection(ftpDir.Host, ftpDir.Port, ftpDir.Username, ftpDir.Password))
            {
                ftpConnection.Open();
                ftpConnection.Login();

                var paths = GetFtpFilesAndFolders(ftpDir.BaseDirectory, ftpConnection);
                paths.Reverse();
                foreach (var path in paths)
                {
                    var fullPath = ftpDir.BaseDirectory + path;
                    if (ftpConnection.DirectoryExists(fullPath))
                        RemoveDirectoryEvenIfNotEmpty(ftpConnection, fullPath);
                    else {
                        ftpConnection.RemoveFile(fullPath);
                    }
                }

                ftpConnection.Close();
            }
        }
Exemplo n.º 28
0
 internal NativeFtpFile(FtpDirectory parent, Ftp.FtpFile file) : base(parent)
 {
     m_file = file;
 }
Exemplo n.º 29
0
        // Construction

        #region C'tors

        public FtpUtil()
        {
            _host = new FtpDirectory();
        }