private void Connected() { Receive <ListDirectory>((cmd) => { StopIdlePeriod(); IEnumerable <SftpFileInfo> result = null; try { result = _connection.ListDirectory(cmd.RemotePath, null); } catch (Exception) { result = new SftpFileInfo[] { }; } this.Sender.Tell(result, Self); StartIdlePeriod(); }); Receive <UploadFile>((cmd) => { StopIdlePeriod(); Utils.EnsureParentDirectoryExists(_connection, cmd.RemotePath); using (var stream = _fileStreamProvider.OpenRead(cmd.LocalPath)) { _connection.UploadFile(stream, cmd.RemotePath, null); } StartIdlePeriod(); }); Receive <DownloadFile>((cmd) => { StopIdlePeriod(); using (var stream = _fileStreamProvider.OpenWrite(cmd.LocalPath)) { _connection.DownloadFile(cmd.RemotePath, stream, null); } StartIdlePeriod(); }); Receive <ReceiveTimeout>((cmd) => { if (DateTimeOffset.Now - _idleFromTime > TimeSpan.FromSeconds(ConnectionTimeoutInSeconds)) { StopIdlePeriod(); _connection.Disconnect(); _connection.Dispose(); Become(Disconnected); } }); }
/// <summary> /// /// </summary> /// <param name="caminho"></param> /// <param name="destino"></param> /// <returns></returns> public async Task Upload(string caminho, string destino) { try { using (var stream = new FileStream(caminho, FileMode.Open)) { await Task.Run(() => _sftpClient.UploadFile(stream, destino)); } return; } catch (Exception ex) { throw ex; } }
/// <summary> /// /// </summary> /// <param name="writeMode"></param> /// <returns></returns> public override bool Put(DeliveryWriteMode writeMode) { if (SaveFileCopy) { string filename = Path.Combine(Context.BaseDirectory, string.Format("SftpDeliveryFile-{0:yyyyMMdd-HHmmss}.csv", DateTime.Now)); _log.Info(m => m("Saving copy of SFTP upload file to '{0}'", filename)); Source.ToFile(filename, Source.Length); } _log.Trace(m => m("Beginning SFTP communication.")); using (Stream dataStream = new MemoryStream(Source)) { _log.Trace(m => m("Getting SFTP client object...")); _sftp = GetClient(); using (_sftp) { try { _log.Debug(m => m("Connecting to SFTP server ({0}@{1})...", Username, Hostname)); _sftp.Connect(); _log.Trace(m => m("Does file exist at destination (and Overwrite is disabled)?")); if (DeliveryWriteMode.Overwrite != writeMode && _sftp.Exists(Destination)) { if (DeliveryWriteMode.Exception == writeMode) { _log.Info(m => m("Destination file exists and Overwrite flag has not been specified: '{0}'", Destination)); throw new SftpPermissionDeniedException("File already exists and Overwrite is not enabled."); } // DeliverWriteMode.Ignore _log.Info(m => m("Destination file exists and flag is set to Ignore. Skipping.\n{0}", Destination)); return false; } _log.Info(m => m("Uploading file via SFTP ({0}@{1}:{2}) WriteMode: '{3}'", Username, Hostname, Destination, writeMode.ToString("F"))); _sftp.UploadFile(dataStream, Destination, (DeliveryWriteMode.Overwrite == writeMode)); // if nothing blew up we succeeded (?) return true; } catch (SshConnectionException ex) { _log.Warn(m => m("Unable to establish an SFTP connection to '{0}@{1}'\n{2}", Username, Hostname, ex)); throw; } catch(SftpPermissionDeniedException ex) { _log.Warn(m => m("Failed to upload file to '{0}@{1}:{2}'\n{3}", Username, Hostname, Destination, ex)); throw; } catch(SshException ex) { _log.Warn(m => m("SSH server returned the following error '{0}'\n{1}", ex.Message, ex)); throw; } finally { _log.Debug(m => m("Disconnecting from SFTP server...")); if (_sftp != null && _sftp.IsConnected) { _sftp.Disconnect(); } } } } return false; }