Class: Aqueduct::Repositories::Ftp
- Inherits:
-
Object
- Object
- Aqueduct::Repositories::Ftp
- Includes:
- Aqueduct::Repository
- Defined in:
- lib/aqueduct/repositories/ftp.rb
Instance Method Summary collapse
- #count_files(file_locators, file_type) ⇒ Object
- #file_server_available? ⇒ Boolean
- #get_file(file_locator, file_type) ⇒ Object
- #has_repository? ⇒ Boolean
Instance Method Details
#count_files(file_locators, file_type) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/aqueduct/repositories/ftp.rb', line 20 def count_files(file_locators, file_type) error = '' file_paths = [] url_paths = [] begin ftp = Net::FTP.new #(@source.file_server_host, @source.file_server_login, @source.file_server_password) ftp.connect(@source.file_server_host, 21) ftp.login(@source.file_server_login, @source.file_server_password) ftp.chdir(@source.file_server_path) file_locators.each do |file_locator| file_locator = file_locator.to_s.gsub(/[\/*]/, '') # Don't allow wild cards or subfolders file_type = file_type.to_s.gsub(/[\/*]/, '') # Don't allow wild cards or subfolders file_name = file_locator + file_type file_path = File.join(Rails.root, 'tmp', 'symbolic', "source_#{@source.id}", @source.file_server_path) if File.exists?(File.join(file_path, file_name)) # Do nothing # Rails.logger.debug "File already exists" else begin # Rails.logger.debug "Retrieving #{file_name} from FTP" FileUtils.mkpath(file_path) ftp.getbinaryfile(file_name, File.join(file_path, file_name)) rescue # Rails.logger.debug " #{file_name} not found!" File.delete(File.join(file_path, file_name)) if File.exists?(File.join(file_path, file_name)) file_name = nil end end file_paths << File.join(file_path, file_name) unless file_name.blank? url_paths << SITE_URL + "/sources/#{@source.id}/download_file?file_locator=#{file_locator}&file_type=#{file_type}&fn=#{file_name.split('/').last}" unless file_name.blank? end rescue => e error = e.to_s ensure begin ftp.close rescue => e # We don't care. end end { result: url_paths.size, error: error, file_paths: file_paths, urls: url_paths } end |
#file_server_available? ⇒ Boolean
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/aqueduct/repositories/ftp.rb', line 77 def file_server_available? result = false error = '' begin ftp = Net::FTP.new ftp.connect(@source.file_server_host, 21) ftp.login(@source.file_server_login, @source.file_server_password) result = true rescue => e error = e.to_s ensure begin ftp.close rescue => e # We don't care. end end { result: result, error: error } end |
#get_file(file_locator, file_type) ⇒ Object
68 69 70 71 |
# File 'lib/aqueduct/repositories/ftp.rb', line 68 def get_file(file_locator, file_type) result_hash = count_files([file_locator], file_type) { file_path: result_hash[:file_paths].first.to_s, error: result_hash[:error] } end |
#has_repository? ⇒ Boolean
73 74 75 |
# File 'lib/aqueduct/repositories/ftp.rb', line 73 def has_repository? { result: true, error: '' } end |