Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Carrierwave and SHA1 duplicate image test


  • Please log in to reply
6 replies to this topic

#1 dcastellano1

dcastellano1

    Signalman

  • Members
  • 13 posts
  • LocationColumbus, Ohio

Posted 26 September 2013 - 05:25 PM

Hi All,

I am a novice programmer and would appreciate any suggestionswith the following.
I am using Carrierwave in my Rails 3.x app along with Fog to store
images on S3.  I am trying to prevent uploading of duplicate images.  

This is my approach:

1. Upload file using carrierwave.
class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
   storage :fog
   include CarrierWave::MimeTypes
   process :set_content_type
   def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end

2. In picture model:
 require 'digest/sha1'
 before_validation :update_sha_1_hash
 private
  def update_sha_1_hash
    self.sha_1_hash = Digest::SHA1.hexdigest(self.image)
  end

 3.Check If the hash identifier in #2 is a duplicate of an existing
upload
validates_uniqueness_of :sha_1_hash

Here's the error: can't convert ImageUploader into String
I am not sure how to direct SHA1 to the actual image file before it is
uploaded..

Thanks,
Dave
 

 



#2 dcastellano1

dcastellano1

    Signalman

  • Members
  • 13 posts
  • LocationColumbus, Ohio

Posted 27 September 2013 - 01:01 AM

OK heres a clue leading to another question...

 

I managed to get this error when trying to remove :imageuploaderloader from the path...

 

private
 
  def update_sha_1_hash
    self.sha_1_hash = Digest::SHA1.hexdigest(self.image.slice! ":ImageUploader")
  end
 

undefined method `slice!' for /uploads/tmp/1380243400-416-3987/piechart.gif:ImageUploader

 

Why is slice! an undefined method?  Is this a scope issue?



#3 stevieing

stevieing

    Dispatcher

  • Members
  • 40 posts

Posted 27 September 2013 - 01:56 PM

Slice! is an undefined method for the object.

 

You are trying to use slice! on an ImageUploader object and slice! is only a defined method for Arrays and Strings.

 

You need to remove the text from the path of the image not the image itself so you need to convert the path to a String.

 

Hope that makes sense.

 

Steve.

 



#4 dcastellano1

dcastellano1

    Signalman

  • Members
  • 13 posts
  • LocationColumbus, Ohio

Posted 27 September 2013 - 09:18 PM

You need to remove the text from the path of the image not the image itself so you need to convert the path to a String.

 

That is the conclusion I came to also but then tried...

 

def update_sha_1_hash

    self.sha_1_hash = Digest::SHA1.hexdigest(self.image.to_s.slice ":ImageUploader")
 end
 
and get ERROR:  can't convert nil into String


#5 dcastellano1

dcastellano1

    Signalman

  • Members
  • 13 posts
  • LocationColumbus, Ohio

Posted 29 September 2013 - 09:55 PM

I tried posting this previously and would like to try again as I hope I can explain the question better this time.  I am trying to test for duplicate images in a Picture class using a SHA1 hash to compare the incoming image with all images currently in the database table.

 

This is in my model:

 

mount_uploader :image, ImageUploader  # Carrierwave uploader to Amazon S3

 

before_validation :create_sha_1_hash

validates_uniqueness_of :sha_1_hash

 

Private

    def create_sha_1_hash

      self.sha_1_hash = Digest::SHA1.hexdigest(self.image)

    end

 

In this case, however, self.image = /uploads/tmp/1380243400-416-3987/piechart.gif:ImageUploader

 

I therefore obviously get the error: undefined method `slice!' for /uploads/tmp/1380243400-416-3987/piechart.gif:ImageUploader

 

If I try self.sha_1_hash = Digest::SHA1.hexdigest(self.image.to_s.slice ":ImageUploader")

I get the ERROR:  can't convert nil into String (Not sure why)

 

 I have also tried self.sha_1_hash = Digest::SHA1.hexdigest(self.image_url) which returns the url to the uploaded file and this does give me a digest but it is different for the same file in consecutive uploads so I don’t think it looking at the actual image file.

In this case is it possible it is not evaluating the URL as a pathname to the file?

 

I’m trying to figure out what to use instead of (self.image) to point to the image file itself to create the digest of the image file.



#6 dcastellano1

dcastellano1

    Signalman

  • Members
  • 13 posts
  • LocationColumbus, Ohio

Posted 29 September 2013 - 10:20 PM

Ok, finally solved:

Digest::SHA1.file(image.path).hexdigest

 

Thanks anyway...



#7 raghav.raaz

raghav.raaz

    Passenger

  • Members
  • 6 posts

Posted 22 October 2013 - 08:04 AM

nice post sir thanks for sharing






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users