diff --git a/app/controllers/images_controller.rb b/app/controllers/images_controller.rb index 9acc50244a..700809444a 100644 --- a/app/controllers/images_controller.rb +++ b/app/controllers/images_controller.rb @@ -16,9 +16,10 @@ def create uid: current_user.uid) flash[:error] = 'The image could not be saved.' unless @image.save! elsif params[:data] + filetype = params[:data].split(';').first.split('/').last @image = Image.new(uid: current_user.uid, photo: params[:data], - photo_file_name: 'dataurl') + photo_file_name: 'dataurl.' + filetype) @image.save! else @image = Image.new(uid: current_user.uid, diff --git a/test/functional/images_controller_test.rb b/test/functional/images_controller_test.rb index b9117b0f68..2c19feaa30 100644 --- a/test/functional/images_controller_test.rb +++ b/test/functional/images_controller_test.rb @@ -1,9 +1,6 @@ require 'test_helper' class ImagesControllerTest < ActionController::TestCase - # def create - # def new - # def update def setup activate_authlogic @@ -35,13 +32,23 @@ def setup test 'image creation success should render the details about the image in the form of json' do user = UserSession.create(users(:jeff)) upload_photo = fixture_file_upload('rails.png', 'image/png') - post :create, - params: { + post :create, + params: { image: { photo: upload_photo, title: 'Rails image', }, - } + } assert_equal 'application/json', @response.content_type end + + test 'creation via daturl' do + user = UserSession.create(users(:jeff)) + data = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAQABADASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAABgj/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwCdABykX//Z" + post :create, + params: { + data: data + } + assert "dataurl.jpeg", Image.last.filename + end end