Class: Shrine::Storage::Fog

Inherits:
Object
  • Object
show all
Defined in:
lib/shrine/storage/fog.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory:, prefix: nil, public: true, expires: 3600, connection: nil, **options) ⇒ Fog

Returns a new instance of Fog.



10
11
12
13
14
15
16
# File 'lib/shrine/storage/fog.rb', line 10

def initialize(directory:, prefix: nil, public: true, expires: 3600, connection: nil, **options)
  @connection = connection || ::Fog::Storage.new(options)
  @directory = @connection.directories.new(key: directory)
  @prefix = prefix
  @public = public
  @expires = expires
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/shrine/storage/fog.rb', line 8

def connection
  @connection
end

#directoryObject (readonly)

Returns the value of attribute directory.



8
9
10
# File 'lib/shrine/storage/fog.rb', line 8

def directory
  @directory
end

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/shrine/storage/fog.rb', line 8

def prefix
  @prefix
end

Instance Method Details

#clear!(confirm = nil) ⇒ Object

Raises:

  • (Shrine::Confirm)


63
64
65
66
# File 'lib/shrine/storage/fog.rb', line 63

def clear!(confirm = nil)
  raise Shrine::Confirm unless confirm == :confirm
  list.each(&:destroy)
end

#delete(id) ⇒ Object



48
49
50
# File 'lib/shrine/storage/fog.rb', line 48

def delete(id)
  file(id).destroy
end

#download(id) ⇒ Object



26
27
28
# File 'lib/shrine/storage/fog.rb', line 26

def download(id)
  Down.download(url(id))
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/shrine/storage/fog.rb', line 44

def exists?(id)
  !!head(id)
end

#open(id) ⇒ Object



36
37
38
# File 'lib/shrine/storage/fog.rb', line 36

def open(id)
  download(id)
end

#read(id) ⇒ Object



40
41
42
# File 'lib/shrine/storage/fog.rb', line 40

def read(id)
  get(id).body
end

#stream(id) ⇒ Object



30
31
32
33
34
# File 'lib/shrine/storage/fog.rb', line 30

def stream(id)
  get(id) do |chunk, _, content_length|
    yield chunk, content_length
  end
end

#upload(io, id, metadata = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/shrine/storage/fog.rb', line 18

def upload(io, id,  = {})
  if copyable?(io)
    copy(io, id, )
  else
    put(io, id, )
  end
end

#url(id, **options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/shrine/storage/fog.rb', line 52

def url(id, **options)
  signed_url = file(id).url(Time.now + @expires, **options)
  if @public
    uri = URI(signed_url)
    uri.query = nil
    uri.to_s
  else
    signed_url
  end
end