Skip to content

Commit

Permalink
画像のバリデーションをMimeTypeを用いるものに変更
Browse files Browse the repository at this point in the history
  • Loading branch information
reckyy committed Sep 27, 2024
1 parent e6112b7 commit 770c936
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/controllers/current_user_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def edit
end

def update
@user.uploaded_avatar = user_params[:avatar]
if @user.update(user_params)
redirect_to @user, notice: 'ユーザー情報を更新しました。'
else
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 @@ -65,6 +65,7 @@ def create
@user.course_id = params[:user][:course_id] if params[:user][:course_id].present?
@user.course_id ||= Course.first.id
@user.build_discord_profile
@user.uploaded_avatar = user_params[:avatar]
Newspaper.publish(:user_create, { user: @user })
if @user.staff? || @user.trainee?
create_free_user!
Expand Down
17 changes: 12 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,9 @@ class User < ApplicationRecord

validates :login_name, length: { minimum: 3, message: 'は3文字以上にしてください。' }

validates :avatar, attached: false,
content_type: {
in: %w[image/png image/jpg image/jpeg image/gif image/heic image/heif],
message: 'はPNG, JPG, GIF, HEIC, HEIF形式にしてください'
}
attr_accessor :uploaded_avatar

validate :validate_uploaded_avatar_content_type

validates :country_code, inclusion: { in: ISO3166::Country.codes }, allow_blank: true

Expand Down Expand Up @@ -855,4 +853,13 @@ def category_having_active_practice
def category_having_unstarted_practice
unstarted_practices&.first&.categories&.first
end

def validate_uploaded_avatar_content_type
return unless uploaded_avatar

mime_type = Marcel::Magic.by_magic(uploaded_avatar)&.type
return if mime_type&.start_with?('image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'image/heic', 'image/heif')

errors.add(:avatar, 'は指定された拡張子(PNG, JPG, GIF, HEIC, HEIF形式)になっていないか、あるいは画像が破損している可能性があります')
end
end
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions test/system/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,12 @@ class UsersTest < ApplicationSystemTestCase
filtered_users = all('.users-item__icon .a-user-role')
assert(filtered_users.all? { |user| user[:class].split(' ').include?('is-student') })
end

test 'can not upload broken image as user avatar' do
visit_with_auth '/current_user/edit', 'hajime'
attach_file 'user[avatar]', 'test/fixtures/files/images/broken_image.jpg', make_visible: true
click_button '更新する'

assert_text 'ユーザーアイコンは指定された拡張子(PNG, JPG, GIF, HEIC, HEIF形式)になっていないか、あるいは画像が破損している可能性があります'
end
end

0 comments on commit 770c936

Please sign in to comment.