Exemple #1
0
 public async Task <bool> DownloadAsync(FtpFile ftpFile,
                                        Stream stream)
 {
     return(await this.DownloadAsync(ftpFile,
                                     stream,
                                     CancellationToken.None));
 }
Exemple #2
0
 public bool Delete(FtpFile ftpFile,
                    CancellationToken cancellationToken)
 {
     return(this.DeleteAsync(ftpFile,
                             cancellationToken)
            .Result);
 }
Exemple #3
0
 public bool Download(FtpFile ftpFile,
                      Stream stream)
 {
     return(this.Download(ftpFile,
                          stream,
                          CancellationToken.None));
 }
Exemple #4
0
 public bool Download(FtpFile ftpFile,
                      Stream stream,
                      CancellationToken cancellationToken)
 {
     return(this.DownloadAsync(ftpFile,
                               stream,
                               cancellationToken)
            .Result);
 }
Exemple #5
0
 public async Task <bool> UploadAsync(Stream stream,
                                      FtpFile ftpFile,
                                      bool createDirectoryIfNotExists = true)
 {
     return(await this.UploadAsync(stream,
                                   ftpFile,
                                   CancellationToken.None,
                                   createDirectoryIfNotExists));
 }
Exemple #6
0
 public bool Upload(Stream stream,
                    FtpFile ftpFile,
                    bool createDirectoryIfNotExists = true)
 {
     return(this.Upload(stream,
                        ftpFile,
                        CancellationToken.None,
                        createDirectoryIfNotExists));
 }
Exemple #7
0
 public bool Upload(Stream stream,
                    FtpFile ftpFile,
                    CancellationToken cancellationToken,
                    bool createDirectoryIfNotExists = true)
 {
     return(this.UploadAsync(stream,
                             ftpFile,
                             cancellationToken,
                             createDirectoryIfNotExists)
            .Result);
 }
Exemple #8
0
        public async Task <bool> DeleteAsync(FtpFile ftpFile,
                                             CancellationToken cancellationToken)
        {
            using (await this._mutex.LockAsync(cancellationToken))
            {
                var controlComplexSocket = await this.EnsureConnectionAndAuthenticationAsync(cancellationToken);

                if (controlComplexSocket == null)
                {
                    return(false);
                }

                var success = await this.GotoParentDirectoryAsync(controlComplexSocket,
                                                                  ftpFile,
                                                                  cancellationToken);

                if (!success)
                {
                    return(false);
                }

                success = await this.SendAndLogAsync(controlComplexSocket,
                                                     cancellationToken,
                                                     "DELE {0}",
                                                     ftpFile.FileName);

                if (!success)
                {
                    return(false);
                }

                var ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                             cancellationToken);

                if (!ftpReply.Success)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #9
0
 public async Task <bool> DeleteAsync(FtpFile ftpFile)
 {
     return(await this.DeleteAsync(ftpFile,
                                   CancellationToken.None));
 }
Exemple #10
0
 public bool Delete(FtpFile ftpFile)
 {
     return(this.Delete(ftpFile,
                        CancellationToken.None));
 }
Exemple #11
0
        public async Task <bool> DownloadAsync(FtpFile ftpFile,
                                               Stream stream,
                                               CancellationToken cancellationToken)
        {
            using (await this._mutex.LockAsync(cancellationToken))
            {
                var controlComplexSocket = await this.EnsureConnectionAndAuthenticationAsync(cancellationToken);

                if (controlComplexSocket == null)
                {
                    return(false);
                }

                {
                    var success = await this.GotoParentDirectoryAsync(controlComplexSocket,
                                                                      ftpFile,
                                                                      cancellationToken);

                    if (!success)
                    {
                        return(false);
                    }
                }

                long bytesTotal;

                // sending SIZE
                // reading SIZE
                {
                    var success = await this.SendAndLogAsync(controlComplexSocket,
                                                             cancellationToken,
                                                             "SIZE {0}",
                                                             ftpFile.FileName);

                    if (!success)
                    {
                        return(false);
                    }

                    var ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                                 cancellationToken);

                    if (!ftpReply.Success)
                    {
                        return(false);
                    }

                    if (!long.TryParse(ftpReply.ResponseMessage,
                                       out bytesTotal))
                    {
                        return(false);
                    }
                }

                // sending PASV
                // reading PASV
                var transferComplexSocket = await this.GetPassiveComplexSocketAsync(controlComplexSocket,
                                                                                    cancellationToken);

                if (transferComplexSocket == null)
                {
                    return(false);
                }

                {
                    FtpReply ftpReply;

                    using (transferComplexSocket)
                    {
                        // sending RETR
                        // open transfer socket
                        // reading RETR (150 Opening BINARY mode data connection...)
                        var success = await this.SendAndLogAsync(controlComplexSocket,
                                                                 cancellationToken,
                                                                 "RETR {0}",
                                                                 ftpFile.FileName);

                        if (!success)
                        {
                            return(false);
                        }

                        success = await transferComplexSocket.ConnectAsync(cancellationToken);

                        if (!success)
                        {
                            return(false);
                        }

                        ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                                 cancellationToken);

                        if (!ftpReply.Success)
                        {
                            return(false);
                        }

                        {
                            this.WaitBeforeReceive();

                            // reading transfer socket
                            var rawFtpResponse = await transferComplexSocket.Socket.ReceiveAsync(this.ChunkReceiveBufferSize,
                                                                                                 transferComplexSocket.GetSocketAsyncEventArgs,
                                                                                                 cancellationToken,
                                                                                                 bytesTotal,
                                                                                                 bytesReceived =>
                            {
                                var downloadProgressEventArgs = new DownloadProgressEventArgs(bytesReceived,
                                                                                              bytesTotal);
                                this.OnDownloadProgressAsync(downloadProgressEventArgs);
                            });

                            if (!rawFtpResponse.Success)
                            {
                                return(false);
                            }

                            stream.Write(rawFtpResponse.Buffer,
                                         0,
                                         rawFtpResponse.Buffer.Length);
                        }
                    }

                    if (!ftpReply.Completed)
                    {
                        // reading RETR (226 Transfer complete)
                        ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                                 cancellationToken);

                        if (!ftpReply.Success)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Exemple #12
0
        public async Task <bool> UploadAsync(Stream stream,
                                             FtpFile ftpFile,
                                             CancellationToken cancellationToken,
                                             bool createDirectoryIfNotExists = true)
        {
            using (await this._mutex.LockAsync(cancellationToken))
            {
                var controlComplexSocket = await this.EnsureConnectionAndAuthenticationAsync(cancellationToken);

                if (controlComplexSocket == null)
                {
                    return(false);
                }

                {
                    var success = await this.GotoParentDirectoryAsync(controlComplexSocket,
                                                                      ftpFile,
                                                                      cancellationToken,
                                                                      createDirectoryIfNotExists);

                    if (!success)
                    {
                        return(false);
                    }
                }

                // sending PASV
                // reading PASV
                var transferComplexSocket = await this.GetPassiveComplexSocketAsync(controlComplexSocket,
                                                                                    cancellationToken);

                if (transferComplexSocket == null)
                {
                    return(false);
                }

                FtpReply ftpReply;
                using (transferComplexSocket)
                {
                    // sending STOR
                    // open transfer socket
                    // reading STOR (150 ...)
                    var success = await this.SendAndLogAsync(controlComplexSocket,
                                                             cancellationToken,
                                                             "STOR {0}",
                                                             ftpFile.FileName);

                    if (!success)
                    {
                        return(false);
                    }

                    success = await transferComplexSocket.ConnectAsync(cancellationToken);

                    if (!success)
                    {
                        return(false);
                    }

                    ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                             cancellationToken);

                    if (!ftpReply.Success)
                    {
                        return(false);
                    }

                    {
                        // sending transfer socket
                        success = await transferComplexSocket.Socket.SendAsync(this.ChunkSendBufferSize,
                                                                               controlComplexSocket.GetSocketAsyncEventArgs,
                                                                               stream,
                                                                               cancellationToken,
                                                                               (bytesSent,
                                                                                bytesTotal) =>
                        {
                            var uploadProgressEventArgs = new UploadProgressEventArgs(bytesSent,
                                                                                      bytesTotal);
                            this.OnUploadProgressAsync(uploadProgressEventArgs);
                        });

                        if (!success)
                        {
                            return(false);
                        }
                    }
                }

                if (!ftpReply.Completed)
                {
                    // reading STOR (226 ...)
                    ftpReply = await this.ReceiveAndLogAsync(controlComplexSocket,
                                                             cancellationToken);

                    if (!ftpReply.Success)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemple #13
0
        public bool Upload(Stream stream,
                           FtpFile ftpFile)
        {
            Contract.Requires(stream != null);
            Contract.Requires(stream.CanRead);
            Contract.Requires(ftpFile != null);

            lock (this._lockControlComplexSocket)
            {
                {
                    var success = this.EnsureConnection();
                    if (!success)
                    {
                        return(false);
                    }
                }

                var controlComplexSocket = this._controlComplexSocket;
                {
                    var hierarchy = ftpFile.GetHierarchy()
                                    .Reverse();

                    foreach (var element in hierarchy)
                    {
                        var name = element.Name;
                        {
                            var success = controlComplexSocket.Send(string.Format("CWD {0}",
                                                                                  name),
                                                                    this.Encoding,
                                                                    this.SendTimeout);
                            if (!success)
                            {
                                return(false);
                            }
                        }
                        var complexResult = controlComplexSocket.Receive(this.Encoding,
                                                                         this.ReceiveTimeout);
                        var ftpResponseType = complexResult.FtpResponseType;
                        switch (ftpResponseType)
                        {
                        case FtpResponseType.PermanentNegativeCompletion:
                            // TODO some parsing of the actual FtpReply.ResponseCode should be done in here. i assume 5xx-state means "directory does not exist" all the time, which might be wrong
                            var success = this.TryCreateDirectoryInternal(name,
                                                                          out complexResult);
                            if (!success)
                            {
                                return(false);
                            }
                            goto case FtpResponseType.PositiveCompletion;

                        case FtpResponseType.PositiveCompletion:
                            continue;

                        default:
                            return(false);
                        }
                    }
                }

                var transferComplexSocket = this.GetPassiveComplexSocket();
                if (transferComplexSocket == null)
                {
                    return(false);
                }

                using (transferComplexSocket)
                {
                    {
                        // sending STOR-command via control socket
                        var fileName = ftpFile.Name;
                        var success  = controlComplexSocket.Send(string.Format("STOR {0}",
                                                                               fileName),
                                                                 this.Encoding,
                                                                 this.SendTimeout);
                        if (!success)
                        {
                            return(false);
                        }
                    }
                    {
                        // connect to transfer socket
                        var connected = transferComplexSocket.Connect(this.ConnectTimeout);
                        if (!connected)
                        {
                            return(false);
                        }
                    }
                    {
                        // receiving STOR-response via control socket (should be 150/125)
                        var complexResult = controlComplexSocket.Receive(this.Encoding,
                                                                         this.ReceiveTimeout);
                        var success = complexResult.Success;
                        if (!success)
                        {
                            return(false);
                        }
                    }
                    {
                        // sending content via transfer socket
                        var success = transferComplexSocket.Send(stream,
                                                                 this.SendTimeout);
                        if (!success)
                        {
                            return(false);
                        }
                    }
                }

                {
                    FtpResponseType ftpResponseType;
                    do
                    {
                        var complexResult = controlComplexSocket.Receive(this.Encoding,
                                                                         this.ReceiveTimeout);
                        var success = complexResult.Success;
                        if (!success)
                        {
                            return(false);
                        }

                        ftpResponseType = complexResult.FtpResponseType;
                    } while (ftpResponseType != FtpResponseType.PositiveCompletion);

                    return(true);
                }
            }
        }