Skip to content

Commit

Permalink
Limit image resizing for club, members and users (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
baldarn authored Aug 28, 2024
1 parent 1794149 commit e2aabb2
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 19 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ gem 'devise', '~> 4.9'
gem 'devise-i18n'

# Other
gem 'active_storage_validations'
gem 'aws-sdk-s3'
gem 'bcrypt', '~> 3.1.7'
gem 'front_matter_parser'
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ GEM
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
active_storage_validations (1.1.4)
activejob (>= 5.2.0)
activemodel (>= 5.2.0)
activestorage (>= 5.2.0)
activesupport (>= 5.2.0)
activejob (7.2.0)
activesupport (= 7.2.0)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -471,6 +476,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
active_storage_validations
aws-sdk-s3
bcrypt (~> 3.1.7)
bootstrap (~> 5.1)
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ def set_club
def current_user_is_admin?
head :unauthorized unless current_user.admin?
end

def resize_image(picture_param, height, width)
return unless picture_param

begin
ImageProcessing::MiniMagick
.source(picture_param)
.resize_to_fit(width, height)
.call(destination: picture_param.tempfile.path)
rescue StandardError => _e
# Do nothing. If this is catching, it probably means the
# file type is incorrect, which can be caught later by
# model validations.
end
end
end
3 changes: 2 additions & 1 deletion app/controllers/clubs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class ClubsController < BaseController
before_action :current_user_is_admin?
before_action -> { resize_image(club_params[:picture], 300, 300) }, only: [:update]

def edit
@club = current_user.club
Expand All @@ -20,6 +21,6 @@ def update
private

def club_params
params.require(:club).permit(:name, :email, :address, :postal_code, :province, :tax_code, :telephone)
params.require(:club).permit(:name, :email, :address, :postal_code, :province, :tax_code, :telephone, :picture)
end
end
3 changes: 2 additions & 1 deletion app/controllers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

class MembersController < BaseController
before_action :current_user_is_admin?
before_action -> { resize_image(member_params[:picture], 250, 200) }, only: %i[create update]

def index
@group = params[:group_id] ? @club.groups.find(params[:group_id]) : nil
@tag = params[:tag_id] ? @club.tags.find(params[:tag_id]) : nil

@members = @club.members
@members = @club.members.order(:last_name)
@members = @members.joins(:groups).where(groups: { id: @group.id }) if @group
@members = @members.joins(:tags).where(tags: { id: @tag.id }) if @tag
@members = @members.page(params[:page])
Expand Down
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class UsersController < BaseController
before_action :current_user_is_admin?
before_action -> { resize_image(user_params[:picture], 250, 200) }, only: %i[create update]

def index
@users = @club.users.page(params[:page])
Expand Down
1 change: 1 addition & 0 deletions app/models/club.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class Club < ApplicationRecord
has_one_attached :picture
validates :picture, content_type: ['image/png', 'image/jpeg']

has_many :users, dependent: :destroy

Expand Down
1 change: 1 addition & 0 deletions app/models/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class Member < ApplicationRecord
has_one_attached :picture
validates :picture, content_type: ['image/png', 'image/jpeg']

belongs_to :club

Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class User < ApplicationRecord
:confirmable, :lockable, :timeoutable

has_one_attached :picture
validates :picture, content_type: ['image/png', 'image/jpeg']

has_many :user_groups, dependent: :destroy
has_many :groups, through: :user_groups, dependent: :nullify
Expand Down
9 changes: 4 additions & 5 deletions app/views/clubs/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
<%= f.input :province %>
<%= f.input :tax_code %>
<%= f.input :telephone %>
<%=
if club.picture.present?
image_tag club.picture, class: 'img-fluid img-thumbnail rounded mx-auto d-block', style: 'max-width: 200px'
end
%>
<%= f.input :picture, as: :file %>
<% if club.errors[:picture].blank? && club.picture.present? %>
<%= image_tag club.picture, class: 'img-fluid img-thumbnail rounded mx-auto d-block', style: "max-width: 200px" %>
<% end %>
<%= f.submit 'Save', class: 'btn btn-primary' %>
<% end %>
10 changes: 4 additions & 6 deletions app/views/members/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@
<%= f.association :groups, collection: @club.groups, as: :check_boxes %>
<%= f.input :privacy_disclaimer %>
<%= f.input :picture_disclaimer %>
<%= f.file_field :picture %>
<%=
if member.picture.present?
image_tag member.picture, class: 'img-fluid img-thumbnail rounded mx-auto d-block', style: 'max-width: 200px'
end
%>
<%= f.input :picture, as: :file %>
<% if member.errors[:picture].blank? && member.picture.present? %>
<%= image_tag member.picture, class: 'img-fluid img-thumbnail rounded mx-auto d-block', style: "max-width: 200px" %>
<% end %>
<%= f.submit 'Save', class: 'btn btn-primary' %>
<% end %>
10 changes: 4 additions & 6 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
<%= f.input :role, prompt: 'Seleziona ruolo', collection: User.roles.map { |role, v| [User.human_attribute_name("role.#{role}"), role] }, wrapper_html: { class: 'col-sm-6' } %>
<%= f.association :groups, collection: @club.groups, as: :check_boxes %>
<%= f.input :blsd_expires_at, include_blank: true, wrapper_html: { class: 'col-sm-6' } %>
<%= f.file_field :picture %>
<%=
if user.picture.present?
image_tag user.picture, class: 'img-fluid img-thumbnail rounded mx-auto d-block', style: 'max-width: 200px'
end
%>
<%= f.input :picture, as: :file %>
<% if user.errors[:picture].blank? && user.picture.present? %>
<%= image_tag user.picture, class: 'img-fluid img-thumbnail rounded mx-auto d-block', style: "max-width: 200px" %>
<% end %>
<%= f.submit 'Save', class: 'btn btn-primary' %>
<% end %>
3 changes: 3 additions & 0 deletions config/locales/simple_form.it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ it:
default_message: "Rivedere gli errori:"
labels:
user:
picture: Foto
first_name: Nome
last_name: Cognome
email: Email
Expand All @@ -23,6 +24,7 @@ it:
club_province: Provincia Club
club_tax_code: Codice Fiscale/PIva Club
member:
picture: Foto
first_name: Nome
last_name: Cognome
born_at: Nato il
Expand Down Expand Up @@ -56,6 +58,7 @@ it:
group:
name: Nome
club:
picture: Immagine
name: Nome
email: Nome
address: Indirizzo
Expand Down

0 comments on commit e2aabb2

Please sign in to comment.