/// <summary>
        /// 
        /// </summary>
        /// <param name="writeMode"></param>
        /// <returns></returns>
        public override bool Put(DeliveryWriteMode writeMode = DeliveryWriteMode.Overwrite)
        {
            if (string.IsNullOrWhiteSpace(Destination))
              {
            throw new NullReferenceException("Delivery Destination cannot be NULL or empty.");
              }

              if (!Directory.Exists(Path.GetDirectoryName(Destination)))
              {
            throw new DirectoryNotFoundException(string.Format("Specified directory does not exist: '{0}'", Destination));
              }

              if (File.Exists(Destination))
              {
            if (DeliveryWriteMode.Overwrite == writeMode)
            {
              File.Delete(Destination);
            }
            else if (DeliveryWriteMode.Exception == writeMode)
            {
              throw new NotSupportedException(string.Format("File already exists, and Overwrite flag has not been specified: '{0}'", Destination));
            }
            else if (DeliveryWriteMode.Ignore == writeMode)
            {
              _log.Warn(m => m("File already exists and flag is set to Ignore. Skipping.\n{0}", Destination));
              return false;
            }
              }

              try
              {
            int length = Source.Length;
            using (FileStream fs = File.Create(Destination, length, FileOptions.None))
            {
              fs.Write(Source, 0, length);
              fs.Flush();
              fs.Close();

              return true;
            }
              }
              catch (Exception ex)
              {
            _log.Error(m => m("Unable to write file '{0}'...\n{1}", Destination, ex));
              }
              return false;
        }
Esempio n. 2
0
        /// <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;
        }
 /// <summary>
 /// </summary>
 /// <param name="writeMode"></param>
 /// <returns></returns>
 public abstract bool Put(DeliveryWriteMode writeMode);