Class: ArchiveStorage::StoredFile
- Inherits:
-
Object
- Object
- ArchiveStorage::StoredFile
- Defined in:
- lib/archive_storage/stored_file.rb
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#storage_key ⇒ Object
readonly
Returns the value of attribute storage_key.
-
#uploader ⇒ Object
readonly
Returns the value of attribute uploader.
Instance Method Summary collapse
- #candidate_storages ⇒ Object
- #content_type ⇒ Object
- #current_storage ⇒ Object
- #delete ⇒ Object
- #exists? ⇒ Boolean
- #filename ⇒ Object
-
#initialize(uploader, identifier, storage_key: nil) ⇒ StoredFile
constructor
A new instance of StoredFile.
- #path ⇒ Object
- #read ⇒ Object
- #size ⇒ Object
- #url(*args, **options) ⇒ Object
Constructor Details
#initialize(uploader, identifier, storage_key: nil) ⇒ StoredFile
Returns a new instance of StoredFile.
9 10 11 12 13 |
# File 'lib/archive_storage/stored_file.rb', line 9 def initialize(uploader, identifier, storage_key: nil) @uploader = uploader @identifier = identifier.to_s @storage_key = storage_key || uploader.store_path(identifier) end |
Instance Attribute Details
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
7 8 9 |
# File 'lib/archive_storage/stored_file.rb', line 7 def identifier @identifier end |
#storage_key ⇒ Object (readonly)
Returns the value of attribute storage_key.
7 8 9 |
# File 'lib/archive_storage/stored_file.rb', line 7 def storage_key @storage_key end |
#uploader ⇒ Object (readonly)
Returns the value of attribute uploader.
7 8 9 |
# File 'lib/archive_storage/stored_file.rb', line 7 def uploader @uploader end |
Instance Method Details
#candidate_storages ⇒ Object
63 64 65 |
# File 'lib/archive_storage/stored_file.rb', line 63 def candidate_storages ([current_storage] + policy.read_fallbacks + [policy.primary_storage_key]).compact.uniq end |
#content_type ⇒ Object
38 39 40 |
# File 'lib/archive_storage/stored_file.rb', line 38 def content_type with_read_adapter { |adapter| adapter.head(storage_key).content_type } end |
#current_storage ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/archive_storage/stored_file.rb', line 54 def current_storage ArchiveStorage.registry.current_storage_for( uploader, identifier: identifier, storage_key: storage_key, default: policy.primary_storage_key ) end |
#delete ⇒ Object
50 51 52 |
# File 'lib/archive_storage/stored_file.rb', line 50 def delete adapter_for(current_storage).delete(storage_key) end |
#exists? ⇒ Boolean
42 43 44 45 46 47 48 |
# File 'lib/archive_storage/stored_file.rb', line 42 def exists? candidate_storages.any? do |storage| ArchiveStorage.adapter(storage).exists?(storage_key) rescue *fallback_errors false end end |
#filename ⇒ Object
19 20 21 |
# File 'lib/archive_storage/stored_file.rb', line 19 def filename ::File.basename(identifier) end |
#path ⇒ Object
15 16 17 |
# File 'lib/archive_storage/stored_file.rb', line 15 def path storage_key end |
#read ⇒ Object
30 31 32 |
# File 'lib/archive_storage/stored_file.rb', line 30 def read with_read_adapter { |adapter| adapter.read(storage_key) } end |
#size ⇒ Object
34 35 36 |
# File 'lib/archive_storage/stored_file.rb', line 34 def size with_read_adapter { |adapter| adapter.head(storage_key).byte_size } end |
#url(*args, **options) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/archive_storage/stored_file.rb', line 23 def url(*args, **) = args.last.is_a?(Hash) ? args.pop : {} = .merge() with_read_adapter { |adapter| adapter.url(storage_key, **) } end |