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, &block) ⇒ Object
- #download_chunk(key, range) ⇒ Object
- #exist?(key) ⇒ Boolean
- #headers_for_direct_upload(key, checksum:) ⇒ Object
-
#initialize(**config) ⇒ FTPService
constructor
A new instance of FTPService.
- #update_metadata(key, content_type:, disposition: nil, filename: nil) ⇒ Object
- #upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename: nil) ⇒ Object
- #url(key, expires_in:, filename:, content_type:, disposition:) ⇒ Object
- #url_for_direct_upload(key, expires_in:, checksum:) ⇒ Object
Constructor Details
#initialize(**config) ⇒ FTPService
Returns a new instance of FTPService.
5 6 7 |
# File 'lib/active_storage/service/ftp_service.rb', line 5 def initialize(**config) @config = config end |
Instance Method Details
#delete(key) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/active_storage/service/ftp_service.rb', line 50 def delete(key) instrument :delete, key: key do connection do |ftp| ftp.delete(key) end end end |
#delete_prefixed(prefix) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/active_storage/service/ftp_service.rb', line 58 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do connection do |ftp| files = ftp.list("#{prefix}*") files.each { |f| f.delete(f) } end end end |
#download(key, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/active_storage/service/ftp_service.rb', line 17 def download(key, &block) if block_given? instrument :streaming_download, key: key do stream(key, &block) end else instrument :download, key: key do stream(data) do |data| result += data end result end end end |
#download_chunk(key, range) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_storage/service/ftp_service.rb', line 38 def download_chunk(key, range) instrument :download_chunk, key: key, range: range do connection do |ftp| offset = range.begin ftp.retrbinary("RETR #{key}", range.begin) do |data| yield data offset += data end end end end |
#exist?(key) ⇒ Boolean
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/active_storage/service/ftp_service.rb', line 67 def exist?(key) instrument :exist, key: key do |payload| connection do |ftp| data = ftp.retrbinary("RETR #{key}") answer = !data.nil? payload[:exist] = answer answer end end end |
#headers_for_direct_upload(key, checksum:) ⇒ Object
97 98 99 |
# File 'lib/active_storage/service/ftp_service.rb', line 97 def headers_for_direct_upload(key, checksum:, **) { 'Content-MD5' => checksum } end |
#update_metadata(key, content_type:, disposition: nil, filename: nil) ⇒ Object
32 33 34 35 36 |
# File 'lib/active_storage/service/ftp_service.rb', line 32 def (key, content_type:, disposition: nil, filename: nil) instrument :update_metadata, key: key, content_type: content_type, disposition: disposition do # Nothing need to do end end |
#upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename: nil) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/active_storage/service/ftp_service.rb', line 9 def upload(key, io, checksum: nil, content_type: nil, disposition: nil, filename: nil) instrument :upload, key: key, checksum: checksum do connection do |ftp| ftp.put(io, key) end end end |
#url(key, expires_in:, filename:, content_type:, disposition:) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/active_storage/service/ftp_service.rb', line 78 def url(key, expires_in:, filename:, content_type:, disposition:) instrument :url, key: key do |payload| generated_url = url_for(key) payload[:url] generated_url end end |
#url_for_direct_upload(key, expires_in:, checksum:) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/active_storage/service/ftp_service.rb', line 86 def url_for_direct_upload(key, expires_in:, checksum:, **) instrument :url, key: key do |payload| raise 'not support direct upload' # generated_url = bucket.signed_url key, method: "PUT", expires: expires_in, content_md5: checksum # payload[:url] = generated_url # generated_url end end |