OK sure, let me try to get this condensed down.
I’m using the signature plugin to generate md5 hashes for the files and metadata_attributes to store it in a separate column for easy access. Documentation on this is good, I don’t think I need to explain.
I then do a select on the database for the hash of the new upload* and if it exists I remove the attachment (set it to nil again) and copy over the values for _data and _md5. I’m using ActiveRecord with Rails and it looks like this:
existing = Post.where(type: @post.type, attachment_md5: params[f].md5)
@post.attachment_md5 = existing.attachment_md5
@post.attachment_data = existing.attachment_data
@post.video_type = existing.video_type
post_empty = false
@post.attachment = params[f]
@post.video_type = "upload" if f == :video_upload
As I’m using Rails the uploaded files get processed by ActionDispatch before they are handed over to my code and Shrine. I generate the hash there so I can have it before the shrine validations run. For this I created a (probably pretty bad) monkey patch (It’s my first one, be gentle!)
@md5 = Digest::MD5.hexdigest(File.read hash[:tempfile])
Hope this helps!