Class: CarrierWave::Storage::GoogleDrive::File

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

Instance Method Summary collapse

Constructor Details

#initialize(uploader, storage, resource_id = nil) ⇒ File

Returns a new instance of File.



26
27
28
29
30
31
# File 'lib/carrierwave/storage/google_drive.rb', line 26

def initialize(uploader, storage, resource_id = nil)
  @uploader   = uploader
  @storage    = storage
  @version_name = (uploader.version_name||'default').to_sym
  @resource_id = YAML.load(resource_id.to_s)[@version_name] rescue ""
end

Instance Method Details

#connectionObject



47
48
49
# File 'lib/carrierwave/storage/google_drive.rb', line 47

def connection
  @storage.connection
end

#content_typeObject



51
52
53
54
# File 'lib/carrierwave/storage/google_drive.rb', line 51

def content_type
  @content_type ||= file.available_content_types.first
  @content_type
end

#deleteObject



41
42
43
44
45
# File 'lib/carrierwave/storage/google_drive.rb', line 41

def delete
  if @resource_id.present?
    file.delete
  end
end

#extensionObject



55
56
57
# File 'lib/carrierwave/storage/google_drive.rb', line 55

def extension
  content_type.to_s
end

#readObject



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

def read
  open(url).read
end

#store(file) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/carrierwave/storage/google_drive.rb', line 33

def store(file)
  @remote_file = connection.upload_from_file(file.path, nil, :convert => false)
  @remote_file.acl.push( {:scope_type => "default", :role => "reader"})
  @resource_id = @remote_file.resource_id.split(':').last
  @uploader.key = @remote_file.resource_id.split(':').last
  @resource_id
end

#url(options = { }) ⇒ Object



59
60
61
# File 'lib/carrierwave/storage/google_drive.rb', line 59

def url(options = { })
  "https://docs.google.com/uc?&id=#{@resource_id}"
end