From 53911f226dd69fcf26eb1c49c892cd822b393fd5 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Mon, 17 Aug 2020 01:47:59 +0200 Subject: [PATCH] Validation for uploaded image type added --- Gemfile | 4 ++++ Gemfile.lock | 3 +++ app/models/screenshot.rb | 9 ++++----- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 9c41602..c122f39 100644 --- a/Gemfile +++ b/Gemfile @@ -132,3 +132,7 @@ gem 'omniauth-amazon' gem 'omniauth-google-oauth2' gem 'omniauth-github' gem 'omniauth-openid' + +# Validations for ActiveStorage (which replaced Paperclip) are +# not yet available in Rails 6.0. So this is a third-party gem. +gem 'active_storage_validations' diff --git a/Gemfile.lock b/Gemfile.lock index ee06671..d285f2b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -43,6 +43,8 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) + active_storage_validations (0.8.9) + rails (>= 5.2.0) activejob (6.0.2.2) activesupport (= 6.0.2.2) globalid (>= 0.3.6) @@ -368,6 +370,7 @@ PLATFORMS ruby DEPENDENCIES + active_storage_validations better_errors binding_of_caller bootsnap diff --git a/app/models/screenshot.rb b/app/models/screenshot.rb index 5fc4da4..9fc0bd7 100644 --- a/app/models/screenshot.rb +++ b/app/models/screenshot.rb @@ -3,13 +3,12 @@ class Screenshot < ApplicationRecord belongs_to :user, inverse_of: :screenshots # Use paperclip gem to handle image files related to screenshots + # After migration from Paperclip to ActiveStorage: has_one_attached :image - # has_attached_file :image, - # styles: { :large => '800x600>', :thumb => '160x120>' }, - # default_url: '/images/dummy/no-screenshots-available.svg', - # path: ':rails_root/public/screenshots/:id_partition/:style.png', - # url: '/screenshots/:id_partition/:style.png' + # Validate using 'active_storage_validations' gem + validates :image, attached: true, content_type: { in: 'image/png', message: 'is not a valid PNG image file' } + #validates_attachment_content_type :image, :content_type => 'image/png' #validates_with AttachmentSizeValidator, :attributes => :image, :less_than => 5.megabytes # validate :validate_image_is_unique