From 1426e7cc639d7ceca5e8921be25f55c3dccb3cf2 Mon Sep 17 00:00:00 2001 From: rebuilt Date: Tue, 27 Dec 2022 14:19:49 -0800 Subject: [PATCH] add proof of concept sftp downloader --- Gemfile | 3 +++ Gemfile.lock | 8 ++++++++ app/services/sftp/downloader.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 app/services/sftp/downloader.rb diff --git a/Gemfile b/Gemfile index bfdf53ca..b76040cc 100644 --- a/Gemfile +++ b/Gemfile @@ -55,6 +55,9 @@ gem 'stimulus-rails' gem 'watir' gem 'selenium-webdriver', '~> 4.4' +gem 'net-sftp' +gem 'ed25519' +gem 'bcrypt_pbkdf' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index 5c0cb79e..58e4ec11 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -82,6 +82,7 @@ GEM ast (2.4.2) backport (1.2.0) bcrypt (3.1.18) + bcrypt_pbkdf (1.1.0) benchmark (0.2.0) better_html (1.0.16) actionview (>= 4.0) @@ -132,6 +133,7 @@ GEM diff-lcs (1.5.0) docile (1.4.0) e2mmap (0.1.0) + ed25519 (1.3.0) erb_lint (0.1.3) activesupport better_html (~> 1.0.7) @@ -204,8 +206,11 @@ GEM net-protocol net-protocol (0.1.3) timeout + net-sftp (4.0.0) + net-ssh (>= 5.0.0, < 8.0.0) net-smtp (0.3.3) net-protocol + net-ssh (7.0.1) newrelic_rpm (8.8.0) nio4r (2.5.8) nokogiri (1.13.10) @@ -408,6 +413,7 @@ PLATFORMS DEPENDENCIES activerecord-import apparition! + bcrypt_pbkdf bootsnap brakeman bullet @@ -417,6 +423,7 @@ DEPENDENCIES database_cleaner debug devise + ed25519 erb_lint erblint-github factory_bot_rails @@ -428,6 +435,7 @@ DEPENDENCIES launchy listen (~> 3.0.5) nested_scaffold + net-sftp newrelic_rpm nokogiri (>= 1.13.4) omniauth diff --git a/app/services/sftp/downloader.rb b/app/services/sftp/downloader.rb new file mode 100644 index 00000000..c1bcd714 --- /dev/null +++ b/app/services/sftp/downloader.rb @@ -0,0 +1,26 @@ +require 'net/sftp' +require 'uri' +require 'csv' + +module Sftp + class Downloader + def initialize + sftptogo_url = ENV['SFTPTOGO_URL'] + uri = URI.parse(sftptogo_url) + Net::SFTP.start(uri.host, uri.user, password: uri.password) do |sftp| + # download a file or directory from the remote host + # open and read from a pseudo-IO for a remote file + # sftp.file.open('/mciea/data/canary.csv', 'r') do |f| + # CSV.parse(f.read) do |row| + # puts row + # end + # end + + # list the entries in a directory + sftp.dir.foreach('/mciea/data/') do |entry| + puts entry.longname + end + end + end + end +end