From b8d557a1ea3e0ab1536d1bbef69e96ab2aad5ea5 Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Wed, 16 Jun 2021 01:36:27 +0200 Subject: [PATCH 1/2] Add avatar file size validation (max 5 MB) --- app/assets/javascripts/application/avatar.js | 8 +++--- .../stylesheets/application/avatar.scss | 8 +++--- app/controllers/users_controller.rb | 20 +++++++++++---- app/models/user.rb | 2 ++ app/views/layouts/_messages.html.erb | 2 +- app/views/users/_avatar.html.erb | 17 ++++++------- config/locales/ca.yml | 1 + config/locales/en.yml | 7 +++--- config/locales/es.yml | 1 + config/locales/eu.yml | 1 + config/locales/gl.yml | 1 + config/locales/pt-BR.yml | 1 + spec/controllers/users_controller_spec.rb | 25 ++++++++++++++----- 13 files changed, 62 insertions(+), 32 deletions(-) diff --git a/app/assets/javascripts/application/avatar.js b/app/assets/javascripts/application/avatar.js index 3f4b127bf..c6034c88c 100644 --- a/app/assets/javascripts/application/avatar.js +++ b/app/assets/javascripts/application/avatar.js @@ -38,14 +38,14 @@ $(function () { // on submit take the parameters of the box to crop the avatar $('#form_photo').on("submit", () => { - let total_width = parseInt(getComputedStyle(document.getElementById("containerCrop")).width); - let photo_width = parseInt(getComputedStyle(document.getElementById("foto")).width); + let total_width = parseInt(getComputedStyle(document.getElementById("container_crop")).width); + let photo_width = parseInt(getComputedStyle(document.getElementById("original_img")).width); let left_displacement = total_width - photo_width; $('#height_offset').val(parseInt(panel.css('top')) - 15); $('#width_offset').val(parseInt(panel.css('left')) - 15 - (left_displacement/2)); $('#height_width').val(parseInt(panel.css('width'))); - $('#original_width').val($('#foto').width()); + $('#original_width').val($('#original_img').width()); }); function resize(e) { @@ -93,7 +93,7 @@ $(function () { } function preview_image_modal() { - var preview = document.querySelector('#foto'); + var preview = document.querySelector('#original_img'); var file = document.querySelector('#avatar-js').files[0]; var reader = new FileReader(); diff --git a/app/assets/stylesheets/application/avatar.scss b/app/assets/stylesheets/application/avatar.scss index d87ab1d6b..7ed2d7c8d 100644 --- a/app/assets/stylesheets/application/avatar.scss +++ b/app/assets/stylesheets/application/avatar.scss @@ -6,8 +6,8 @@ #crop_panel { position: absolute; - width: 140px; - height: 140px; + width: 200px; + height: 200px; border-left: 4px dashed black; cursor: move; touch-action: none; @@ -21,7 +21,7 @@ right: 0; width: 16px; height: 100%; - cursor: w-resize; + cursor: ew-resize; } #crop_panel::after { @@ -32,5 +32,5 @@ bottom: 0; width: 100%; height: 16px; - cursor: n-resize; + cursor: ns-resize; } diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index eded58ee7..44529a9b2 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -67,13 +67,13 @@ def update def update_avatar avatar = params[:avatar] + errors = validate_avatar(avatar) - if avatar && content_type_permitted(avatar.content_type) + if errors.blank? current_user.avatar.purge if current_user.avatar.attached? - crop_image_and_save(current_user, avatar) else - flash[:error] = t 'users.show.invalid_format' + flash[:error] = errors.join("
") end redirect_to current_user @@ -151,7 +151,17 @@ def crop_image_and_save(user, avatar) ) end - def content_type_permitted(avatar_content_type) - %w[image/jpeg image/pjpeg image/png image/x-png].include? avatar_content_type + def validate_avatar(file) + errors = [] + + if User::AVATAR_CONTENT_TYPES.exclude?(file.content_type) + errors << t("users.show.invalid_format") + end + + if file.size.to_f > User::AVATAR_MAX_SIZE.megabytes + errors << t("users.avatar.max_size_warning", size: User::AVATAR_MAX_SIZE) + end + + errors end end diff --git a/app/models/user.rb b/app/models/user.rb index 4f3e006c8..a0eb4bc9a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,6 +9,8 @@ class User < ApplicationRecord :timeoutable ] + AVATAR_MAX_SIZE = 5 + AVATAR_CONTENT_TYPES = %w[image/jpeg image/pjpeg image/png image/x-png] GENDERS = %w( female male diff --git a/app/views/layouts/_messages.html.erb b/app/views/layouts/_messages.html.erb index 0222a2c35..d1db057d0 100644 --- a/app/views/layouts/_messages.html.erb +++ b/app/views/layouts/_messages.html.erb @@ -5,7 +5,7 @@ diff --git a/app/views/users/_avatar.html.erb b/app/views/users/_avatar.html.erb index bd792a6be..df80232cf 100644 --- a/app/views/users/_avatar.html.erb +++ b/app/views/users/_avatar.html.erb @@ -16,25 +16,24 @@