diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2e9bdb6e..0d5ef819 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ # this file is required for errbit notifier class ApplicationController < ActionController::API + include ActionController::ImplicitRender + include ActionController::Helpers end diff --git a/app/controllers/v0/application_controller.rb b/app/controllers/v0/application_controller.rb index b1af5d0c..4daae564 100644 --- a/app/controllers/v0/application_controller.rb +++ b/app/controllers/v0/application_controller.rb @@ -1,3 +1,4 @@ +require_relative '../../helpers/user_helper' module V0 class ApplicationController < ActionController::API @@ -10,6 +11,8 @@ class ApplicationController < ActionController::API include PrettyJSON include ErrorHandlers + helper ::UserHelper + respond_to :json before_action :prepend_view_paths diff --git a/app/helpers/user_helper.rb b/app/helpers/user_helper.rb new file mode 100644 index 00000000..38bb9ddd --- /dev/null +++ b/app/helpers/user_helper.rb @@ -0,0 +1,9 @@ +module UserHelper + def profile_picture_url(user) + if user.profile_picture.attached? + polymorphic_url(user.profile_picture, only_path: false) + else + '' + end + end +end \ No newline at end of file diff --git a/app/views/v0/devices/_device.jbuilder b/app/views/v0/devices/_device.jbuilder index bd3a6492..224ff593 100644 --- a/app/views/v0/devices/_device.jbuilder +++ b/app/views/v0/devices/_device.jbuilder @@ -29,6 +29,9 @@ if device.owner json.uuid device.owner.uuid json.username device.owner.username json.avatar device.owner.avatar + + json.profile_picture profile_picture_url(device.owner) + json.url device.owner.url json.joined_at device.owner.joined_at json.location device.owner.location diff --git a/app/views/v0/devices/index.jbuilder b/app/views/v0/devices/index.jbuilder index 1c04988e..1b32eaf3 100644 --- a/app/views/v0/devices/index.jbuilder +++ b/app/views/v0/devices/index.jbuilder @@ -1,15 +1 @@ -json.array! @devices, partial: 'device', as: :device - -# json.array! @devices do |device| -# json.id device.id -# json.name device.name -# json.description device.description -# json.status device.status -# json.added_at device.added_at -# json.last_reading_at device.last_reading_at -# json.updated_at device.updated_at - -# json.kit_id device.kit_id - -# json.owner device.owner, :id, :username, :avatar, :url, :joined_at, :location, :device_ids -# end +json.array! @devices, partial: 'device', as: :device \ No newline at end of file diff --git a/app/views/v0/users/_user.jbuilder b/app/views/v0/users/_user.jbuilder index c7bdd141..621144e1 100644 --- a/app/views/v0/users/_user.jbuilder +++ b/app/views/v0/users/_user.jbuilder @@ -11,13 +11,7 @@ json.(user, :updated_at ) -if user.profile_picture.attached? - json.profile_picture polymorphic_url(user.profile_picture, only_path: false) - -else - json.profile_picture '' -end - +json.profile_picture profile_picture_url(user) if current_user and (current_user.is_admin? or current_user == user) json.merge! email: user.email diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index aa5d980f..e207586f 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -135,6 +135,29 @@ namespace :import do end end + desc "Converts old avatar images stored in S3 to use ActiveStorage" + task :old_avatar_images => :environment do + s3 = Fog::Storage.new({ + :provider => 'AWS', + :aws_access_key_id => ENV['aws_access_key'], + :aws_secret_access_key => ENV['aws_secret_key'], + :region => ENV['aws_region'], + }) + bucket = ENV['s3_bucket'] + User.select { |u| !u.avatar_url.nil? && !u.profile_picture.attached? }.each do |user| + puts "Importing avatar for user with id #{user.id}" + begin + avatar_url = user.avatar_url + s3_key = avatar_url.gsub("https://images.smartcitizen.me/s100/", "") + filename = s3_key.split("/")[-1] + object = s3.get_object(bucket, s3_key) + user.profile_picture.attach(io: StringIO.new(object.body), filename: filename) + rescue Exception => e + puts " -- Error #{e.class.name}. Skipping..." + next + end + end + end end