예제 #1
0
파일: SftpClient.cs 프로젝트: nemec/Blossom
        /// <summary>
        /// Begins an asynchronous uploading the steam into remote file.
        /// </summary>
        /// <param name="input">Data input stream.</param>
        /// <param name="path">Remote file path.</param>
        /// <param name="canOverride">if set to <c>true</c> then existing file will be overwritten.</param>
        /// <param name="handler">The object that handles callbacks for transferred data.</param>
        /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous operation.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="input"/> is <b>null</b>.</exception>
        /// <exception cref="ArgumentException"><paramref name="path"/> is <b>null</b> or contains whitespace characters.</exception>
        /// <exception cref="SshConnectionException">Client is not connected.</exception>
        /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to list the contents of the directory was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
        /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
        /// <remarks>Method calls made by this method to <paramref name="input"/>, may under certain conditions result in exceptions thrown by the stream.</remarks>
        public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, IFileTransferHandler handler, object state)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            if (path.IsNullOrWhiteSpace())
                throw new ArgumentException("path");

            var flags = Flags.Write | Flags.Truncate;

            if (canOverride)
                flags |= Flags.CreateNewOrOpen;
            else
                flags |= Flags.CreateNew;

            //  Ensure that connection is established.
            this.EnsureConnection();

            AsyncCallback callback;
            if (handler != null)
            {
                callback = (r) => handler.TransferCompleted();
            }
            else
            {
                callback = (r) => { };
            }
            var asyncResult = new SftpUploadAsyncResult(callback, state);

            this.ExecuteThread(() =>
            {
                try
                {
                    this.InternalUploadFile(input, path, handler, flags);

                    asyncResult.SetAsCompleted(null, false);
                }
                catch (Exception exp)
                {
                    asyncResult.SetAsCompleted(exp, false);
                }
            });

            return asyncResult;
        }
예제 #2
0
        private static void CopyFile(Stream source, string destination, int bytesPerChunk, IFileTransferHandler handler)
        {
            using (var br = new BinaryReader(source))
            {
                using (var fsDest = new FileStream(destination, FileMode.Create))
                {
                    var bw = new BinaryWriter(fsDest);
                    var buffer = new byte[bytesPerChunk];
                    int bytesRead;

                    for (long i = 0; i < source.Length; i += bytesRead)
                    {
                        bytesRead = br.Read(buffer, 0, bytesPerChunk);
                        bw.Write(buffer, 0, bytesRead);
                        handler.IncrementBytesTransferred((ulong)bytesRead);
                    }
                }
            }
            handler.TransferCompleted();
        }
예제 #3
0
파일: SftpClient.cs 프로젝트: nemec/Blossom
        /// <summary>
        /// Begins an asynchronous file downloading into the stream.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="output">The output.</param>
        /// <param name="handler">The object that handles callbacks for transferred data.</param>
        /// <param name="state">A user-provided object that distinguishes this particular asynchronous write request from other requests.</param>
        /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous operation.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="output"/> is <b>null</b>.</exception>
        /// <exception cref="ArgumentException"><paramref name="path"/> is <b>null</b> or contains whitespace characters.</exception>
        /// <exception cref="SshConnectionException">Client is not connected.</exception>
        /// <exception cref="Renci.SshNet.Common.SftpPermissionDeniedException">Permission to perform the operation was denied by the remote host. <para>-or-</para> A SSH command was denied by the server.</exception>
        /// <exception cref="Renci.SshNet.Common.SshException">A SSH error where <see cref="P:SshException.Message"/> is the message from the remote host.</exception>
        /// <remarks>Method calls made by this method to <paramref name="output"/>, may under certain conditions result in exceptions thrown by the stream.</remarks>
        public IAsyncResult BeginDownloadFile(string path, Stream output, IFileTransferHandler handler, object state)
        {
            if (path.IsNullOrWhiteSpace())
                throw new ArgumentException("path");

            if (output == null)
                throw new ArgumentNullException("output");

            //  Ensure that connection is established.
            this.EnsureConnection();

            AsyncCallback callback;
            if (handler != null)
            {
                callback = (r) => handler.TransferCompleted();
            }
            else
            {
                callback = (r) => { };
            }

            var asyncResult = new SftpDownloadAsyncResult(callback, state);

            this.ExecuteThread(() =>
            {
                try
                {
                    this.InternalDownloadFile(path, output, handler);

                    asyncResult.SetAsCompleted(null, false);
                }
                catch (Exception exp)
                {
                    asyncResult.SetAsCompleted(exp, false);
                }
            });

            return asyncResult;
        }
예제 #4
0
 public bool GetFile(string sourcePath, string destinationPath, IFileTransferHandler handler, bool ifNewer)
 {
     if (ifNewer)
     {
         var modified = File.GetLastWriteTimeUtc(destinationPath);
         var remoteModified = DateTime.MinValue;
         try
         {
             remoteModified = Sftp.GetLastWriteTimeUtc(sourcePath);
         }
         catch (SftpPathNotFoundException)
         {
         }
         if (remoteModified <= modified)
         {
             if (handler != null)
             {
                 handler.FileUpToDate();
             }
             return false;
         }
     }
     using (var file = File.OpenWrite(destinationPath))
     {
         var result = Sftp.BeginDownloadFile(
             sourcePath,
             file,
             r => handler.TransferCompleted(),
             new object(),
             handler.IncrementBytesTransferred);
         Sftp.EndDownloadFile(result);
     }
     return true;
 }