Hi, 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. I am a novice programmer and would appreciate any suggestions. 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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/76e4554c4b97466b0bf44ea6cb6af25b%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Dear Dave, That looks like it''s very nearly there. I suspect that the error you are encountering is because `self.image` in Step 2 is the ImageUploader itself, rather than the path to the file. I don''t think you need to worry about trying to compute the hash prior to upload; instead, you could let the image upload but check the hash prior to saving -- exactly like you''re trying. Perhaps you might like to try if changing Digest::SHA1.hexdigest(self.image) to Digest::SHA1.file(image.path).hexdigest works for you. (It''s not necessary to use `self.image` opposed to `image` as it''s a ''get''/read context, rather than a ''set''/write context.) You might want to also ensure you''re validating presence of `:sha_1_hash`. Peace, tiredpixel On 26/09/2013 13:44, Dave Castellano wrote:> Hi, > 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. I > am a novice programmer and would appreciate any suggestions. > > 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 >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/52448195.6090907%40tiredpixel.com. For more options, visit https://groups.google.com/groups/opt_out.
Dear Tiredpixel, Thank you so much. I have been working on this for several days now and this worked beautifully. Thank you for taking the time to help! Dave C. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e30622b11eed5a1b359e3c4531bf3831%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Dear Dave, Delighted to hear it; you are most welcome! Peace, tiredpixel On 29/09/2013 23:08, Dave Castellano wrote:> Dear Tiredpixel, > > Thank you so much. I have been working on this for several days now and > this worked beautifully. > > Thank you for taking the time to help! > > Dave C. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5248A71E.404%40tiredpixel.com. For more options, visit https://groups.google.com/groups/opt_out.