Skip to content

Commit

Permalink
Support Base64 upload
Browse files Browse the repository at this point in the history
  • Loading branch information
SammyLin committed Mar 23, 2016
1 parent 23a004e commit e3d2961
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/attache/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@ def _call(env, config)
params = request.params
return config.unauthorized unless config.authorized?(params)

relpath = generate_relpath(Attache::Upload.sanitize params['file'])
cachekey = File.join(request_hostname(env), relpath)
if params.has_key? 'data'

base_64_encoded_data = params['data']
data_index = base_64_encoded_data.index('base64') + 7
data_format = base_64_encoded_data.match(/image\/(\w+)/)[1]
filedata = base_64_encoded_data[data_index..-1]
decoded_image = Base64.decode64(filedata)

filename = "#{Time.now.to_i}.#{data_format}"

relpath = generate_relpath(Attache::Upload.sanitize filename)
cachekey = File.join(request_hostname(env), relpath)

bytes_wrote = Attache.cache.write(cachekey, StringIO.new(decoded_image))
else
relpath = generate_relpath(Attache::Upload.sanitize params['file'])
cachekey = File.join(request_hostname(env), relpath)
bytes_wrote = Attache.cache.write(cachekey, request.body)
end

bytes_wrote = Attache.cache.write(cachekey, request.body)
if bytes_wrote == 0
return [500, config.headers_with_cors.merge('X-Exception' => 'Local file failed'), []]
else
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/attache/upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
let(:params) { {} }
let(:filename) { "Exãmple %#{rand} %20.gif" }
let(:file) { StringIO.new(IO.binread("spec/fixtures/landscape.jpg"), 'rb') }
let(:base64_data) { "data:image/jpeg;base64,/9j/4QBiRXhpZgAATU0AKgAAAAgABQESAAMAAAABAAUAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAAITAAMAAAABAAEAAAAAAAAAAABIAAAAAQAAAEgAAAAB/9sAQwACAgICAgECAgICAwICAwMGBAMDAwMHBQUEBggHCQgIBwgICQoNCwkKDAoICAsPCwwNDg4PDgkLEBEQDhENDg4O/9sAQwECAwMDAwMHBAQHDgkICQ4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4O/8AAEQgABAADAwEiAAIRAQMRAf/EABUAAQEAAAAAAAAAAAAAAAAAAAAJ/8QAHxAAAQMEAwEAAAAAAAAAAAAAAQIDBAAFBhEHEjEh/8QAFQEBAQAAAAAAAAAAAAAAAAAABAX/xAAaEQACAgMAAAAAAAAAAAAAAAABAgARMdHh/9oADAMBAAIRAxEAPwCZefZjIj815UzGsNgjR0XN4NsosrJS2nudJGwToeD75SlKjI7FBzUSC1Zn/9k=" }
let(:hostname) { "example.com" }

before do
Expand All @@ -23,6 +24,24 @@
expect(code).to eq 200
end

context "uploading with bash64" do
let(:params) { Hash(data: base64_data) }

subject { proc { middleware.call Rack::MockRequest.env_for('http://' + hostname + '/upload?' + params.collect {|k,v| "#{CGI.escape k.to_s}=#{CGI.escape v.to_s}"}.join('&'), method: 'PUT', "HTTP_HOST" => hostname) } }

it 'should respond successfully with json' do
code, headers, body = subject.call
expect(code).to eq(200)
expect(headers['Content-Type']).to eq('text/json')

JSON.parse(body.join('')).tap do |json|
expect(json).to be_has_key('path')
expect(json['geometry']).to eq('4x3')
end
end

end

context "uploading" do
let(:params) { Hash(file: filename) }

Expand Down

0 comments on commit e3d2961

Please sign in to comment.