Class: ActiveStorage::Service::DiskService
- Inherits:
-
ActiveStorage::Service
- Object
- ActiveStorage::Service
- ActiveStorage::Service::DiskService
- Defined in:
- lib/active_storage/service/disk_service.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #download(key) ⇒ Object
- #exist?(key) ⇒ Boolean
-
#initialize(root:) ⇒ DiskService
constructor
A new instance of DiskService.
- #upload(key, io, checksum: nil) ⇒ Object
- #url(key, expires_in:, disposition:, filename:) ⇒ Object
Methods inherited from ActiveStorage::Service
Constructor Details
#initialize(root:) ⇒ DiskService
Returns a new instance of DiskService.
8 9 10 |
# File 'lib/active_storage/service/disk_service.rb', line 8 def initialize(root:) @root = root end |
Instance Attribute Details
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/active_storage/service/disk_service.rb', line 6 def root @root end |
Instance Method Details
#delete(key) ⇒ Object
34 35 36 |
# File 'lib/active_storage/service/disk_service.rb', line 34 def delete(key) File.delete path_for(key) rescue Errno::ENOENT # Ignore files already deleted end |
#download(key) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/active_storage/service/disk_service.rb', line 22 def download(key) if block_given? File.open(path_for(key)) do |file| while data = file.read(64.kilobytes) yield data end end else File.open path_for(key), &:read end end |
#exist?(key) ⇒ Boolean
38 39 40 |
# File 'lib/active_storage/service/disk_service.rb', line 38 def exist?(key) File.exist? path_for(key) end |
#upload(key, io, checksum: nil) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/active_storage/service/disk_service.rb', line 12 def upload(key, io, checksum: nil) File.open(make_path_for(key), "wb") do |file| while chunk = io.read(64.kilobytes) file.write(chunk) end end ensure_integrity_of(key, checksum) if checksum end |
#url(key, expires_in:, disposition:, filename:) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/active_storage/service/disk_service.rb', line 42 def url(key, expires_in:, disposition:, filename:) verified_key_with_expiration = ActiveStorage::VerifiedKeyWithExpiration.encode(key, expires_in: expires_in) if defined?(Rails) && defined?(Rails.application) Rails.application.routes.url_helpers.rails_disk_blob_path(verified_key_with_expiration, disposition: disposition) else "/rails/blobs/#{verified_key_with_expiration}?disposition=#{disposition}" end end |