public async Task PutAsync(string remotename, Stream input, CancellationToken cancelToken)
        {
            string remotePath = remotename;
            long   streamLen;

            try
            {
                var ftpClient = CreateClient();

                try
                {
                    streamLen = input.Length;
                }
                catch (NotSupportedException) { streamLen = -1; }

                // Get the remote path
                remotePath = "";

                if (!string.IsNullOrEmpty(remotename))
                {
                    // Append the filename
                    remotePath += remotename;
                }

                var success = await ftpClient.UploadAsync(input, remotePath, FtpExists.Overwrite, createRemoteDir : false, token : cancelToken, progress : null).ConfigureAwait(false);

                if (!success)
                {
                    throw new UserInformationException(string.Format(Strings.ErrorWriteFile, remotename), "AftpPutFailure");
                }

                // check remote file size; matching file size indicates completion
                var  sleepTime             = 250;
                var  maxVerifyMilliseconds = 5000;
                var  m          = maxVerifyMilliseconds;
                long remoteSize = 0;
                while (m > 0)
                {
                    remoteSize = ftpClient.GetFileSize(remotePath);
                    if (streamLen == remoteSize)
                    {
                        return;
                    }
                    m -= sleepTime;
                    Thread.Sleep(sleepTime);
                }

                throw new UserInformationException(Strings.ListVerifySizeFailure(remotename, remoteSize, streamLen), "AftpListVerifySizeFailure");
            }
            catch (FtpCommandException ex)
            {
                if (ex.Message == "Directory not found.")
                {
                    throw new FolderMissingException(Strings.MissingFolderError(remotePath, ex.Message), ex);
                }

                throw;
            }
        }
Example #2
0
        public void Put(string remotename, System.IO.Stream input)
        {
            string remotePath = remotename;
            long   streamLen  = -1;

            try
            {
                var ftpClient = CreateClient();

                try
                {
                    streamLen = input.Length;
                }
                // ReSharper disable once EmptyGeneralCatchClause
                catch
                {
                }

                // Get the remote path
                remotePath = "";

                if (!string.IsNullOrEmpty(remotename))
                {
                    // Append the filename
                    remotePath += remotename;
                }

                using (var outputStream = ftpClient.OpenWrite(remotePath))
                {
                    try
                    {
                        CoreUtility.CopyStream(input, outputStream, true, _copybuffer);
                    }
                    finally
                    {
                        outputStream.Close();
                    }
                }


                if (_listVerify)
                {
                    var fileEntries = List(remotename, true);

                    foreach (var fileEntry in fileEntries)
                    {
                        if (fileEntry.Name.Equals(remotename) || fileEntry.Name.EndsWith("/" + remotename, StringComparison.Ordinal) || fileEntry.Name.EndsWith("\\" + remotename, StringComparison.Ordinal))
                        {
                            if (fileEntry.Size < 0 || streamLen < 0 || fileEntry.Size == streamLen)
                            {
                                return;
                            }

                            throw new UserInformationException(Strings.ListVerifySizeFailure(remotename, fileEntry.Size, streamLen), "AftpListVerifySizeFailure");
                        }
                    }

                    throw new UserInformationException(Strings.ListVerifyFailure(remotename, fileEntries.Select(n => n.Name)), "AftpListVerifySizeFailure");
                }
            }
            catch (FtpCommandException ex)
            {
                if (ex.Message == "Directory not found.")
                {
                    throw new FolderMissingException(Strings.MissingFolderError(remotePath, ex.Message), ex);
                }

                throw;
            }
        }
        public async Task PutAsync(string remotename, Stream input, CancellationToken cancelToken)
        {
            string remotePath = remotename;
            long   streamLen;

            try
            {
                var ftpClient = CreateClient();

                try
                {
                    streamLen = input.Length;
                }
                catch (NotSupportedException) { streamLen = -1; }

                // Get the remote path
                remotePath = "";

                if (!string.IsNullOrEmpty(remotename))
                {
                    // Append the filename
                    remotePath += remotename;
                }

                var success = await ftpClient.UploadAsync(input, remotePath, FtpExists.Overwrite, createRemoteDir : false, token : cancelToken, progress : null).ConfigureAwait(false);

                if (!success)
                {
                    throw new UserInformationException(string.Format(Strings.ErrorWriteFile, remotename), "AftpPutFailure");
                }

                if (_listVerify)
                {
                    var fileEntries = List(remotename, true);

                    foreach (var fileEntry in fileEntries)
                    {
                        if (fileEntry.Name.Equals(remotename) || fileEntry.Name.EndsWith("/" + remotename, StringComparison.Ordinal) || fileEntry.Name.EndsWith("\\" + remotename, StringComparison.Ordinal))
                        {
                            if (fileEntry.Size < 0 || streamLen < 0 || fileEntry.Size == streamLen)
                            {
                                return;
                            }

                            throw new UserInformationException(Strings.ListVerifySizeFailure(remotename, fileEntry.Size, streamLen), "AftpListVerifySizeFailure");
                        }
                    }

                    throw new UserInformationException(Strings.ListVerifyFailure(remotename, fileEntries.Select(n => n.Name)), "AftpListVerifySizeFailure");
                }
            }
            catch (FtpCommandException ex)
            {
                if (ex.Message == "Directory not found.")
                {
                    throw new FolderMissingException(Strings.MissingFolderError(remotePath, ex.Message), ex);
                }

                throw;
            }
        }