[Git][noosfero/noosfero][master] 2 commits: add api end point to get the profile image using size key string
Leandro Nunes
gitlab at mg.gitlab.com
Mon Jun 26 09:39:29 BRT 2017
Leandro Nunes pushed to branch master at Noosfero / noosfero
Commits:
d8de460f by Leonardo Soares at 2017-06-22T13:30:45-03:00
add api end point to get the profile image using size key string
- - - - -
07bcc0a9 by Leandro Nunes at 2017-06-26T12:38:57+00:00
Merge branch 'get_profile_image_end_point' into 'master'
add api end point to get the profile image using size key string
See merge request !1252
- - - - -
4 changed files:
- app/api/v1/profiles.rb
- app/models/image.rb
- test/api/profiles_test.rb
- test/test_helper.rb
Changes:
=====================================
app/api/v1/profiles.rb
=====================================
--- a/app/api/v1/profiles.rb
+++ b/app/api/v1/profiles.rb
@@ -12,11 +12,8 @@ module Api
end
get ':id', requirements: { id: /#{Noosfero.identifier_format}/ } do
- profiles = environment.profiles
- profiles = profiles.visible
key = params[:key].to_s == "identifier" ? :identifier : :id
-
- profile = profiles.find_by key => params[:id]
+ profile = environment.profiles.visible.find_by key => params[:id]
if profile
type_map = {
@@ -30,6 +27,21 @@ module Api
not_found!
end
end
+
+ content_type :binary, "image"
+ ['icon', 'thumb', 'big', 'portrait', 'minor'].map do |thumb_size|
+ get ":id/#{thumb_size}", requirements: { id: /#{Noosfero.identifier_format}/ } do
+ key = params[:key].to_s == "identifier" ? :identifier : :id
+ profile = environment.profiles.visible.find_by key => params[:id]
+ if profile && profile.image && profile.image.data(thumb_size)
+ content_type 'image'
+ present profile.image.data(thumb_size)
+ else
+ not_found!
+ end
+
+ end
+ end
desc "Update profile information"
post ':id' do
=====================================
app/models/image.rb
=====================================
--- a/app/models/image.rb
+++ b/app/models/image.rb
@@ -22,7 +22,6 @@ class Image < ApplicationRecord
:icon => '20x20!' },
:max_size => 5.megabytes, # remember to update validate message below
processor: 'Rmagick'
-
validates_attachment :size => N_("{fn} of uploaded file was larger than the maximum size of 5.0 MB").fix_i18n
extend DelayedAttachmentFu::ClassMethods
@@ -34,4 +33,8 @@ class Image < ApplicationRecord
File.file?(full_filename) ? File.read(full_filename) : nil
end
+ def data(size = nil)
+ File.file?(full_filename(size)) ? File.read(full_filename(size)) : nil
+ end
+
end
=====================================
test/api/profiles_test.rb
=====================================
--- a/test/api/profiles_test.rb
+++ b/test/api/profiles_test.rb
@@ -414,6 +414,17 @@ class ProfilesTest < ActiveSupport::TestCase
assert_equal some_person.id, json['id']
end
+ ['icon', 'thumb', 'big', 'portrait', 'minor'].each do | size |
+ should "get profile image #{size} from identifier" do
+ image = Image.create(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
+ image_data = image.data
+ mock_all_profile_image(size, image, image_data)
+ api_image_data = get_profile_image_from_api(size, 'profile_identifier')
+
+ assert_equal image_data, api_image_data
+ end
+ end
+
should "return profile theme when it is defined" do
some_person = fast_create(Person, theme: 'person-theme')
get "/api/v1/profiles/#{some_person.id}?#{params.to_query}"
=====================================
test/test_helper.rb
=====================================
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -228,6 +228,18 @@ class ActiveSupport::TestCase
profile.save!
end
+ def mock_all_profile_image(size, image, image_data)
+ Profile.any_instance.stubs(:image).returns(image)
+ Image.any_instance.stubs(:data).with(size).returns(image_data)
+ end
+
+ def get_profile_image_from_api (size, profile_identifier)
+ profile = fast_create(Person, identifier: profile_identifier)
+ params[:key] = :identifier
+ get "/api/v1/profiles/#{profile_identifier}/#{size}?#{params.to_query}"
+ data = last_response.body
+ end
+
def create_base64_image
image_path = File.absolute_path(Rails.root + 'public/images/noosfero-network.png')
image_name = File.basename(image_path)
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/5d906b81efd8f22a0309e2bd6519d6cfb25ab749...07bcc0a9a53453fdb4cd95811f436e27811582cc
---
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/5d906b81efd8f22a0309e2bd6519d6cfb25ab749...07bcc0a9a53453fdb4cd95811f436e27811582cc
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20170626/9b931fe1/attachment-0001.html>
More information about the Noosfero-dev
mailing list