Class: ArchiveStorage::StoredFile

Inherits:
Object
  • Object
show all
Defined in:
lib/archive_storage/stored_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#identifierObject (readonly)

Returns the value of attribute identifier.



7
8
9
# File 'lib/archive_storage/stored_file.rb', line 7

def identifier
  @identifier
end

#storage_keyObject (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

#uploaderObject (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_storagesObject



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_typeObject



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_storageObject



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

#deleteObject



50
51
52
# File 'lib/archive_storage/stored_file.rb', line 50

def delete
  adapter_for(current_storage).delete(storage_key)
end

#exists?Boolean

Returns:

  • (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

#filenameObject



19
20
21
# File 'lib/archive_storage/stored_file.rb', line 19

def filename
  ::File.basename(identifier)
end

#pathObject



15
16
17
# File 'lib/archive_storage/stored_file.rb', line 15

def path
  storage_key
end

#readObject



30
31
32
# File 'lib/archive_storage/stored_file.rb', line 30

def read
  with_read_adapter { |adapter| adapter.read(storage_key) }
end

#sizeObject



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, **options)
  positional_options = args.last.is_a?(Hash) ? args.pop : {}
  url_options = positional_options.merge(options)

  with_read_adapter { |adapter| adapter.url(storage_key, **url_options) }
end