Class: Service::FtpService
- Inherits:
-
Service
- Object
- Service
- Service::FtpService
- Defined in:
- lib/active_storage/service/ftp_service.rb
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #delete_prefixed(prefix) ⇒ Object
- #download(key) ⇒ Object
- #download_chunk(key, range) ⇒ Object
- #exist?(key) ⇒ Boolean
- #headers_for_direct_upload(key, content_type:) ⇒ Object
-
#initialize(**config) ⇒ FtpService
constructor
A new instance of FtpService.
-
#path_for(key) ⇒ Object
:nodoc:.
- #upload(key, io, checksum: nil) ⇒ Object
- #url(key, expires_in:, filename:, disposition:, content_type:) ⇒ Object
- #url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
Constructor Details
#initialize(**config) ⇒ FtpService
Returns a new instance of FtpService.
11 12 13 |
# File 'lib/active_storage/service/ftp_service.rb', line 11 def initialize(**config) @config = config end |
Instance Method Details
#delete(key) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/active_storage/service/ftp_service.rb', line 58 def delete(key) instrument :delete, key: key do begin connection do |ftp| ftp.chdir(::File.dirname path_for(key)) ftp.delete(::File.basename path_for(key)) end rescue # Ignore files already deleted end end end |
#delete_prefixed(prefix) ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_storage/service/ftp_service.rb', line 71 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do connection do |ftp| ftp.chdir(path_for(prefix)) ftp.list.each do |file| ftp.delete(file.split.last) end end end end |
#download(key) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_storage/service/ftp_service.rb', line 31 def download(key) if block_given? instrument :streaming_download, key: key do open(http_url_for(key)) do |file| while data = file.read(64.kilobytes) yield data end end end else instrument :download, key: key do open(http_url_for(key)) do |file| file.read end end end end |
#download_chunk(key, range) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/active_storage/service/ftp_service.rb', line 49 def download_chunk(key, range) instrument :download_chunk, key: key, range: range do open(http_url_for(key)) do |file| file.seek range.begin file.read range.size end end end |
#exist?(key) ⇒ Boolean
82 83 84 85 86 87 88 89 |
# File 'lib/active_storage/service/ftp_service.rb', line 82 def exist?(key) instrument :exist, key: key do |payload| response = request_head(key) answer = response.code.to_i == 200 payload[:exist] = answer answer end end |
#headers_for_direct_upload(key, content_type:) ⇒ Object
119 120 121 |
# File 'lib/active_storage/service/ftp_service.rb', line 119 def headers_for_direct_upload(key, content_type:, **) {"Content-Type" => content_type} end |
#path_for(key) ⇒ Object
:nodoc:
123 124 125 |
# File 'lib/active_storage/service/ftp_service.rb', line 123 def path_for(key) #:nodoc: File.join ftp_folder, folder_for(key), key end |
#upload(key, io, checksum: nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/active_storage/service/ftp_service.rb', line 15 def upload(key, io, checksum: nil, **) instrument :upload, key: key, checksum: checksum do connection do |ftp| path_for(key).tap do |path| ftp.mkdir_p(::File.dirname path) ftp.chdir(::File.dirname path) ftp.storbinary("STOR #{File.basename(key)}", io, Net::FTP::DEFAULT_BLOCKSIZE) if ftp_chmod ftp.sendcmd("SITE CHMOD #{ftp_chmod.to_s(8)} #{path_for(key)}") end end end ensure_integrity_of(key, checksum) if checksum end end |
#url(key, expires_in:, filename:, disposition:, content_type:) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/active_storage/service/ftp_service.rb', line 91 def url(key, expires_in:, filename:, disposition:, content_type:) instrument :url, key: key do |payload| generated_url = http_url_for(key) payload[:url] = generated_url generated_url end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/active_storage/service/ftp_service.rb', line 99 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) instrument :url, key: key do |payload| verified_token_with_expiration = ActiveStorage.verifier.generate( { key: key, content_type: content_type, content_length: content_length, checksum: checksum }, {expires_in: expires_in, purpose: :blob_token} ) generated_url = url_helpers.update_rails_disk_service_url(verified_token_with_expiration, host: current_host) payload[:url] = generated_url generated_url end end |