Added unique validator that checks for duplicate image uploads

This commit is contained in:
Christoph Haas 2015-04-27 00:20:25 +02:00
parent 9377af7795
commit 3a17af689e

View file

@ -12,6 +12,15 @@ class Screenshot < ActiveRecord::Base
url: '/screenshots/:id_partition/:style.png'
validates_attachment_content_type :image, :content_type => 'image/png'
validates_with AttachmentSizeValidator, :attributes => :image, :less_than => 5.megabytes
validate :validate_image_is_unique
# Validator that checks if the image has already been uploaded
def validate_image_is_unique
# Look for images with the same checksum / image_fingerprint
if Screenshot.find_by(image_fingerprint: image_fingerprint)
errors.add(:image, "has already been uploaded")
end
end
# Return caption for full-screen screenshots.
# Takes the description of a screenshot if available.