Class: CarrierWave::Storage::Azure::File

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/storage/azure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, connection, path) ⇒ File

Returns a new instance of File.



28
29
30
31
32
# File 'lib/carrierwave/storage/azure.rb', line 28

def initialize(uploader, connection, path)
  @uploader = uploader
  @connection = connection
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/carrierwave/storage/azure.rb', line 26

def path
  @path
end

Instance Method Details

#content_typeObject



67
68
69
70
# File 'lib/carrierwave/storage/azure.rb', line 67

def content_type
  @content_type = blob.properties[:content_type] if @content_type.nil? && !blob.nil?
  @content_type
end

#content_type=(new_content_type) ⇒ Object



72
73
74
# File 'lib/carrierwave/storage/azure.rb', line 72

def content_type=(new_content_type)
  @content_type = new_content_type
end

#deleteObject



92
93
94
95
96
97
98
99
# File 'lib/carrierwave/storage/azure.rb', line 92

def delete
  begin
    @connection.delete_blob @uploader.azure_container, @path
    true
  rescue ::Azure::Core::Http::HTTPError
    false
  end
end

#exist?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/carrierwave/storage/azure.rb', line 76

def exist?
  blob.nil?
end

#extensionObject



88
89
90
# File 'lib/carrierwave/storage/azure.rb', line 88

def extension
  @path.split('.').last
end

#filenameObject



84
85
86
# File 'lib/carrierwave/storage/azure.rb', line 84

def filename
  URI.decode(url).gsub(/.*\/(.*?$)/, '\1')
end

#readObject



63
64
65
# File 'lib/carrierwave/storage/azure.rb', line 63

def read
  content
end

#sizeObject



80
81
82
# File 'lib/carrierwave/storage/azure.rb', line 80

def size
  blob.properties[:content_length] unless blob.nil?
end

#store!(file) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/carrierwave/storage/azure.rb', line 34

def store!(file)
  @content_type = file.content_type
  file_to_send = ::File.open(file.file, 'rb')
  block_id = 0
  blocks = []

  until file_to_send.eof?
    block_id += 1

    @content = file_to_send.read 4194304 # Send 4MB chunk
    @connection.create_blob_block @uploader.azure_container, @path, block_id.to_s, @content
    blocks << block_id.to_s
  end

  # Commit block blobs
  @connection.commit_blob_blocks @uploader.azure_container, @path, blocks, :blob_content_type => @content_type

  true
end

#url(options = {}) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/carrierwave/storage/azure.rb', line 54

def url(options = {})
  path = ::File.join @uploader.azure_container, @path
  if @uploader.asset_host
    "#{@uploader.asset_host}/#{path}"
  else
    @connection.generate_uri(path).to_s
  end
end