Skip to content

Commit c71403f

Browse files
committed
limit image uploads by type
1 parent e982236 commit c71403f

File tree

6 files changed

+13
-0
lines changed

6 files changed

+13
-0
lines changed

app/models/brochure.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ class Brochure < ApplicationRecord
1212

1313
has_one_attached :image, dependent: :destroy
1414
has_one_attached :pdf, dependent: :destroy
15+
validates :image, presence: false,
16+
blob: { content_type: Imageable::ALLOWED_IMAGE_TYPES, size_range: Imageable::IMAGE_SIZE_RANGE }
1517
end

app/models/catalog.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Catalog < ApplicationRecord
1414
has_many :books, primary_key: :code, class_name: "Book", dependent: :nullify
1515
has_one_attached :pdf, dependent: :destroy
1616
has_one_attached :image, dependent: :destroy
17+
validates :image, presence: false,
18+
blob: { content_type: Imageable::ALLOWED_IMAGE_TYPES, size_range: Imageable::IMAGE_SIZE_RANGE }
1719

1820
def set_title
1921
if code.present?

app/models/concerns/imageable.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
module Imageable
44
extend ActiveSupport::Concern
55

6+
ALLOWED_IMAGE_TYPES = %w[image/png image/jpg image/jpeg image/gif].freeze
7+
IMAGE_SIZE_RANGE = 1..5.megabytes
8+
69
def thumb_image(image_field)
710
custom_image(image_field, 160, 220)
811
end

app/models/event.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class Event < ApplicationRecord
55
include Imageable
66

77
validates :title, :start_date, :end_date, presence: true
8+
validates :image, presence: false,
9+
blob: { content_type: Imageable::ALLOWED_IMAGE_TYPES, size_range: Imageable::IMAGE_SIZE_RANGE }
810

911
has_rich_text :description
1012
has_rich_text :news_text

app/models/highlight.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class Highlight < ApplicationRecord
55
include Friendable
66

77
validates :title, :image, :link, :alt_text, presence: true
8+
validates :image, presence: false,
9+
blob: { content_type: Imageable::ALLOWED_IMAGE_TYPES, size_range: Imageable::IMAGE_SIZE_RANGE }
810

911
has_one_attached :image
1012
end

app/models/news_item.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ class NewsItem < ApplicationRecord
55
include Friendable
66

77
validates :title, :image, presence: true
8+
validates :image, presence: false,
9+
blob: { content_type: Imageable::ALLOWED_IMAGE_TYPES, size_range: Imageable::IMAGE_SIZE_RANGE }
810

911
has_one_attached :image
1012
has_rich_text :description

0 commit comments

Comments
 (0)