Class: CarrierWave::Storage::NoBrainer::File

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uploader, base, path) ⇒ File

Returns a new instance of File.



154
155
156
# File 'lib/carrierwave/storage/nobrainer.rb', line 154

def initialize(uploader, base, path)
  @uploader, @base, @path, @content_type = uploader, base, path, nil
end

Instance Attribute Details

#pathObject (readonly)

Current local path to file

Returns

String

a path to file



100
101
102
# File 'lib/carrierwave/storage/nobrainer.rb', line 100

def path
  @path
end

Instance Method Details

#content_typeObject

Lookup value for file content-type header

Returns

String

value of content-type



109
110
111
# File 'lib/carrierwave/storage/nobrainer.rb', line 109

def content_type
  @content_type || file.try(:content_type)
end

#deleteObject

Removes the file from the filesystem.



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/carrierwave/storage/nobrainer.rb', line 116

def delete
  criteria = storage_place_from(path).where(path: path).without_ordering

  return unless criteria.present?

  criteria.first.delete

  @content_type = nil
  @file = nil
  @path = nil
end

#exists?Boolean

Check if the file exists on the remote service

Returns

Boolean

true if file exists or false

Returns:

  • (Boolean)


185
186
187
# File 'lib/carrierwave/storage/nobrainer.rb', line 185

def exists?
  !!file
end

#fileObject

lookup file

Returns

NoBrainer::Document

file data from RethinkDB



135
136
137
138
139
# File 'lib/carrierwave/storage/nobrainer.rb', line 135

def file
  return nil unless path

  storage_place_from(path).where(path: path).without_ordering.first
end

#filenameObject

Return file name, if available

Returns

String

file name

or
NilClass

no file name available



150
151
152
# File 'lib/carrierwave/storage/nobrainer.rb', line 150

def filename
  ::File.basename(file.path)
end

#readObject

Read content of file from service

Returns

String

contents of file



164
165
166
# File 'lib/carrierwave/storage/nobrainer.rb', line 164

def read
  file && file.body
end

#sizeObject

Return size of file body

Returns

Integer

size of file body



175
176
177
# File 'lib/carrierwave/storage/nobrainer.rb', line 175

def size
  file.nil? ? 0 : file.body.length
end

#store(new_file) ⇒ Object

Write file to service

Returns

Boolean

true on success or raises error



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/carrierwave/storage/nobrainer.rb', line 195

def store(new_file)
  nobrainer_file = new_file.file.body if new_file.is_a?(self.class)
  nobrainer_file ||= new_file.to_file.read

  @content_type ||= new_file.content_type

  @file = storage_place_for(new_file).where(path: path).first_or_create(
    content_type: new_file.content_type,
    body: nobrainer_file
  )

  @file.save!

  true
end