Class: EchoUploads::Mapper
- Inherits:
-
Object
- Object
- EchoUploads::Mapper
- Defined in:
- lib/echo_uploads/mapper.rb
Instance Attribute Summary collapse
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
Instance Method Summary collapse
-
#initialize(file) ⇒ Mapper
constructor
A new instance of Mapper.
- #write(ext) {|path| ... } ⇒ Object
Constructor Details
#initialize(file) ⇒ Mapper
Returns a new instance of Mapper.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/echo_uploads/mapper.rb', line 5 def initialize(file) unless( file.is_a?(ActionDispatch::Http::UploadedFile) or file.is_a?(Rack::Test::UploadedFile) ) raise( "Expected file to be a ActionDispatch::Http::UploadedFile "+ "or Rack::Test::UploadedFile, but was #{file.inspect}" ) end @uploaded_file = file @outputs = [] end |
Instance Attribute Details
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
20 21 22 |
# File 'lib/echo_uploads/mapper.rb', line 20 def outputs @outputs end |
Instance Method Details
#write(ext) {|path| ... } ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/echo_uploads/mapper.rb', line 22 def write(ext) folder = ::File.join Rails.root, 'tmp/echo_uploads' FileUtils.mkdir_p folder path = ::File.join(folder, SecureRandom.hex(15) + ext) yield path # The map callback might not write a file. That could happen if, for example, the # input file is an invalid image. The best thing to do here is to fail silently. # The application author should write code to handle the failure case, e.g. by # appending to the ActiveRecord errors hash. if ::File.exists? path file = ::File.open path, 'rb' mapped_file = ::EchoUploads::MappedFile.new( tempfile: file, filename: @uploaded_file.original_filename ) mapped_file.mapped_filename = ::File.basename path outputs << mapped_file end end |