I have looked more closely at what you are doing with duplicate attachments. I do not find the code to create a second post reference to the same xf_attachment_data. You have added an index for the md5 hash, but there needs to be another entry created in the attachment table using the same xf_attachment_data record that was found to be duplicated. The attach_count of the xf_attachment_data needs to be incremented and the new xf_attachment attachment_id used in the target (post or whatever). There will be two (or more) xf_attachment records for such duplicates, but there will be no second file stored with the image.
Without this, the handling of the images the second or more times they are used on the site get changed either to links or IMG references, which are inherently ugly if one has standardized on the handling of attachments and their display (which we have done).
When this is working properly, I will write a batch or cron job that will perform the above described process on all the existing duplicate attachments in our system. In the meantime, I will turn off the prevent duplicate feature until this is fixed.
Of course if this behavior is already what is used in XF2, someone please tell me and save me a lot of work.