Skip to content

Instantly share code, notes, and snippets.

@fender21
Created February 22, 2012 20:04
Show Gist options
  • Save fender21/1886923 to your computer and use it in GitHub Desktop.
Save fender21/1886923 to your computer and use it in GitHub Desktop.
Carrierwave
class UploadedFile < CarrierWave::Uploader::Base
SEPARATOR = "\r\n".freeze
attr_reader :body
include CarrierWave::MimeTypes
process :set_content_type
def filename
@name = key
end
def extension_white_list
%w(pdf tiff tif)
end
def initialize(body)
@body = body
end
# return boundary data
def boundary
@body.lines.first.strip.gsub(/^-*/, '')
end
# return file content
def headers
headers = {}
@body.lines.each_with_index do |line, index|
next if index == 0
break if line.strip == ''
key, value = *line.split(':')
headers[key.strip] = value.strip
end
headers
end
# stripts boundaty data and headers from the file content
def content
first_line_index = 0
@body.lines.each do |line|
first_line_index += 1
break if line.strip == ''
end
@body.lines.to_a[first_line_index..-2].join()[0..-3]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment