diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index 8dce8a4..acdd42c 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -1,9 +1,11 @@ require 'grape' +require 'base64' +require 'tempfile' require_relative '../../find_by_contents' - module Noosfero; - module API - module APIHelpers +module Noosfero; + module API + module APIHelpers PRIVATE_TOKEN_PARAM = :private_token DEFAULT_ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type, :author_id, :identifier, :archived] @@ -231,7 +233,7 @@ require_relative '../../find_by_contents' else created_at = scope.find(reference_id).created_at scope.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at) - end + end end def by_categories(scope, params) @@ -368,6 +370,30 @@ require_relative '../../find_by_contents' not_found! if Noosfero::API::API.endpoint_unavailable?(self, @environment) end + def asset_with_image params + if params.has_key? :image_builder + asset_api_params = params + asset_api_params[:image_builder] = base64_to_uploadedfile(asset_api_params[:image_builder]) + return asset_api_params + end + params + end + + def base64_to_uploadedfile(base64_image) + tempfile = base64_to_tempfile base64_image + converted_image = base64_image + converted_image[:tempfile] = tempfile + return {uploaded_data: ActionDispatch::Http::UploadedFile.new(converted_image)} + end + + def base64_to_tempfile base64_image + base64_img_str = base64_image[:tempfile] + decoded_base64_str = Base64.decode64(base64_img_str) + tempfile = Tempfile.new(base64_image[:filename]) + tempfile.write(decoded_base64_str.encode("ascii-8bit").force_encoding("utf-8")) + tempfile.rewind + tempfile + end private def parser_params(params) @@ -394,7 +420,6 @@ require_relative '../../find_by_contents' end_period = until_date.nil? ? DateTime.now : until_date begin_period..end_period end - end end end diff --git a/lib/noosfero/api/v1/articles.rb b/lib/noosfero/api/v1/articles.rb index 67392cb..b39f41f 100644 --- a/lib/noosfero/api/v1/articles.rb +++ b/lib/noosfero/api/v1/articles.rb @@ -51,7 +51,7 @@ module Noosfero post ':id' do article = environment.articles.find(params[:id]) return forbidden! unless article.allow_edit?(current_person) - article.update_attributes!(params[:article]) + article.update_attributes!(asset_with_image(params[:article])) present_partial article, :with => Entities::Article end diff --git a/lib/noosfero/api/v1/people.rb b/lib/noosfero/api/v1/people.rb index 316b8f1..22c69ca 100644 --- a/lib/noosfero/api/v1/people.rb +++ b/lib/noosfero/api/v1/people.rb @@ -55,11 +55,10 @@ module Noosfero post ':id' do authenticate! return forbidden! if current_person.id.to_s != params[:id] - current_person.update_attributes!(params[:person]) + current_person.update_attributes!(asset_with_image(params[:person])) present current_person, :with => Entities::Person, :current_person => current_person end - - # Example Request: + # POST api/v1/people?person[login]=some_login&person[password]=some_password&person[name]=Jack # for each custom field for person, add &person[field_name]=field_value to the request desc "Create person" @@ -76,7 +75,7 @@ module Noosfero params[:person][:custom_values][key]=params[:person].delete(key) if Person.custom_fields(environment).any?{|cf| cf.name==key} end - user = User.build(user_data, params[:person], environment) + user = User.build(user_data, asset_with_image(params[:person]), environment) begin user.signup! diff --git a/test/api/helpers_test.rb b/test/api/helpers_test.rb index 14ad270..bbe50e5 100644 --- a/test/api/helpers_test.rb +++ b/test/api/helpers_test.rb @@ -1,4 +1,5 @@ require_relative 'test_helper' +require "base64" require 'noosfero/api/helpers' class APIHelpersTest < ActiveSupport::TestCase @@ -238,6 +239,24 @@ class APIHelpersTest < ActiveSupport::TestCase present_partial(model, {}) end + should 'create a :uploaded_data hash, expected by image_builder ' do + base64_image = create_base64_image + uploadedfile = base64_to_uploadedfile base64_image + assert uploadedfile.has_key? :uploaded_data + assert_equal uploadedfile[:uploaded_data].original_filename, base64_image[:filename] + assert_equal uploadedfile[:uploaded_data].content_type, base64_image[:type] + assert uploadedfile[:uploaded_data].tempfile + end + + should 'return a params copy with a UploadedFile object' do + base64_image = create_base64_image + params = {} + params.merge!({image_builder: base64_image}) + asset_params = asset_with_image params + assert !asset_params[:image_builder][:uploaded_data].nil? + assert asset_params[:image_builder][:uploaded_data].is_a? ActionDispatch::Http::UploadedFile + end + protected def error!(info, status) diff --git a/test/api/people_test.rb b/test/api/people_test.rb index 9e4cd9b..01d7f08 100644 --- a/test/api/people_test.rb +++ b/test/api/people_test.rb @@ -386,4 +386,15 @@ class PeopleTest < ActiveSupport::TestCase assert_not_nil json['person'][attribute] end end + + should 'update person image' do + login_api + base64_image = create_base64_image + params.merge!({person: {image_builder: base64_image}}) + assert_nil person.image + post "/api/v1/people/#{person.id}?#{params.to_query}" + person.reload + assert_not_nil person.image + assert_equal person.image.filename, base64_image[:filename] + end end diff --git a/test/api/test_helper.rb b/test/api/test_helper.rb index baa51df..a0a94fc 100644 --- a/test/api/test_helper.rb +++ b/test/api/test_helper.rb @@ -33,6 +33,18 @@ class ActiveSupport::TestCase attr_accessor :private_token, :user, :person, :params, :environment + def create_base64_image + image_path = File.absolute_path(Rails.root + 'public/images/noosfero-network.png') + image_name = File.basename(image_path) + image_type = "image/#{File.extname(image_name).delete "."}" + encoded_base64_img = Base64.encode64(File.open(image_path) {|io| io.read }) + base64_image = {} + base64_image[:tempfile] = encoded_base64_img + base64_image[:filename] = image_name + base64_image[:type] = image_type + base64_image + end + private def json_response_ids(kind) -- libgit2 0.21.2