Module: Pushfile::Data
- Included in:
- Upload
- Defined in:
- lib/pushfile/data.rb
Instance Method Summary collapse
-
#ajax_upload ⇒ Object
Ajax upload.
-
#file_upload ⇒ Object
File upload.
-
#setup_data ⇒ Object
Set up data.
-
#url_upload ⇒ Object
URL upload.
Instance Method Details
#ajax_upload ⇒ Object
Ajax upload
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pushfile/data.rb', line 23 def ajax_upload filename = @options[:filename] type = @options[:mimetype] || mimetype(filename) file = @options[:tempfile] || "/tmp/upload-#{filename}" # Pass stream (typically request.body) to read chunks if @options[:stream] File.open(file, 'w') do |f| f.binmode while buffer = @options[:stream].read(51200) f << buffer end end end {:filename => filename, :tempfile => File.new(file), :type => type} end |
#file_upload ⇒ Object
File upload
42 43 44 45 46 47 48 |
# File 'lib/pushfile/data.rb', line 42 def file_upload o = @options[:file] || @options[:datafile] tmpfile, filename = o[:tempfile], o[:filename] type = o[:type] || mimetype(filename) {:filename => filename, :tempfile => tmpfile, :type => type} end |
#setup_data ⇒ Object
Set up data
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/pushfile/data.rb', line 8 def setup_data # Fetch the image into a tempfile, and store if @options[:url] url_upload elsif @options[:filename] ajax_upload # Do Froala or Dropzone file uploads elsif @options[:file] || @options[:datafile] file_upload end end |
#url_upload ⇒ Object
URL upload
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pushfile/data.rb', line 51 def url_upload url = @options[:url].strip content = RestClient.get(url) rescue nil file = Tempfile.new('tmp').tap do |file| file.binmode # must be in binary mode file.write(content) file.rewind end if content # Extract the file name from the URL filename = url.split('/').last # Mime type type = @options[:mimetype] || mimetype(filename) {:filename => filename, :type => type, :tempfile => file} end |