Module: Eco::API::UseCases::GraphQL::Utils::Sftp
- Includes:
- Helpers::Base::CaseEnv
- Defined in:
- lib/eco/api/usecases/graphql/utils/sftp.rb
Instance Attribute Summary
Attributes included from Helpers::Base::CaseEnv
Attributes included from Language::AuxiliarLogger
Instance Method Summary collapse
- #ensure_remote_empty ⇒ Object
- #remote_folder(subfolder = remote_subfolder) ⇒ Object
- #remote_subfolder ⇒ Object
- #sftp ⇒ Object
- #sftp_config ⇒ Object
- #sftp_group_id ⇒ Object
- #sftp_session ⇒ Object
- #to_remote_path(file, subfolder: nil) ⇒ Object
- #upload(local_file, remote_folder: self.remote_folder, gid: sftp_group_id) ⇒ Object
- #with_remote_files ⇒ Object
Methods included from Helpers::Base::CaseEnv
Methods included from Language::AuxiliarLogger
Instance Method Details
#ensure_remote_empty ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 33 def ensure_remote_empty files = with_remote_files unless files.empty? msg = "There are still files in the remote folder that will be deleted: '#{remote_folder}':\n" msg += " • " + files.map do |file| file.longname end.join("\n • ") + "\n" session.prompt_user("Do you want to proceed to delete? (Y/n):", explanation: msg, default: "Y", timeout: 3) do |response| if response.upcase.start_with?("Y") files.each do |file| remote_full_path = to_remote_path(file.name) res = sftp_session.remove(remote_full_path) logger.info("Deleted remote file: '#{remote_full_path}' (#{res})") end end end end end |
#remote_folder(subfolder = remote_subfolder) ⇒ Object
10 11 12 |
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 10 def remote_folder(subfolder = remote_subfolder) "#{sftp_config.remote_folder}/#{subfolder || ''}" end |
#remote_subfolder ⇒ Object
6 7 8 |
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 6 def remote_subfolder nil end |
#sftp ⇒ Object
70 71 72 |
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 70 def sftp session.sftp end |
#sftp_config ⇒ Object
62 63 64 |
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 62 def sftp_config session.config.sftp end |
#sftp_group_id ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 14 def sftp_group_id if self.class.const_defined?(:SFTP_GROUP) self.class.const_get(:TARGET_STRUCTURE_ID) elsif group_id = .dig(:sftp, :group) group_id end end |
#sftp_session ⇒ Object
66 67 68 |
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 66 def sftp_session sftp.sftp_session end |
#to_remote_path(file, subfolder: nil) ⇒ Object
58 59 60 |
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 58 def to_remote_path(file, subfolder: nil) remote_folder(subfolder) + "/" + file end |
#upload(local_file, remote_folder: self.remote_folder, gid: sftp_group_id) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 22 def upload(local_file, remote_folder: self.remote_folder, gid: sftp_group_id) return false unless local_file && File.exists?(local_file) dest_file = "#{remote_folder}/#{File.basename(local_file)}" res = sftp_session.upload!(local_file, dest_file) attrs = sftp_session.stat!(dest_file) if gid && gid != attrs.gid stat_res = sftp_session.setstat!(dest_file, {permissions: 0660, uid: attrs.uid, gid: gid}) end logger.info("Uploaded '#{local_file}' (#{res})") end |
#with_remote_files ⇒ Object
52 53 54 55 56 |
# File 'lib/eco/api/usecases/graphql/utils/sftp.rb', line 52 def with_remote_files sftp.files(remote_folder).each do |remote_file| yield(remote_file) if block_given? end end |