From 3a17af689e4aa3e1a41eeb9960eb6d73e6cf6141 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Mon, 27 Apr 2015 00:20:25 +0200 Subject: [PATCH] Added unique validator that checks for duplicate image uploads --- app/models/screenshot.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/models/screenshot.rb b/app/models/screenshot.rb index e74d21c..9121090 100644 --- a/app/models/screenshot.rb +++ b/app/models/screenshot.rb @@ -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.