Paperclip - duplicating images

I would like not to duplicate the image if posted,

how

user1 upload image
user2 upload same image

2 # images in db

Is there a way to handle this?

Thank,

+2


source to share


3 answers


To do this, you need to split your attachments into your own model.

So now you have

class User < ActiveRecord::Base
   has_attached_file :avatar #plus a bunch of specifications here
end 

      

You would set up a new model and associations like

class User < ActiveRecord::Base
   belongs_to :images
end 

class Image < ActiveRecord::Base
   has_many :users
   has_attached_file :avatar
end 

      



Then in your controller you would need to do

Image.find_or_create_by_avatar_file_name(#filename here)

      

So now you have a user.image_id attribute and can have one uploaded image associated with multiple users.

However, I have created a number of fairly large systems with user photo uploads (icon photos, avatar photos, funny photos, whatever) and the actual overlap between users tends to be pretty small. Plus, you only really save money on what is the cheapest of your resources: storage space. You save nothing on bandwidth cost, processing or program complexity by following this route.

If this is a truly unique set of circumstances (in which case it would be neat to hear what you are doing), then I would advise against taking this route.

0


source


http://www.codeproject.com/Messages/2913691/Comparing-one-image-to-many-others-speeded-up.aspx

I use it in my program and everything is great!

DB related advice: store hashes in a table. and then you just need one hash calculation.



About speed

1) Limit the image size to 100x100, for example

2) When a user tries to log in, a hash of his password is calculated. I think users will log in more often and then update their avatars.

+1


source


Calculate the hash of each image as it is saved. When a new user posts the same image, check the hash of that image and see if it matches anything in the database

0


source







All Articles