I'm allowing users to upload images. I store image information in a database and relate it to various resources on my site (posts, user avatars, comments etc.) I do not know which strategy to take.
Each image uploaded can only be associated with one resource. This was when the user deletes the resource, the image is gone too. So if their file was called
my-image.jpg
that would be gone from the server.Each image uploaded is checked first (hash file contents) and if the image does not exist it is uploaded and associated with the required resource. If the image already exists, it is not uploaded and just associated with the required resource. If a user deletes the resources that uses the image, the image would not be deleted if it was being used by another resource, the association would be broken, but the original resource would remain. If it is not being used by another resource it would be deleted.
I'm not sure how option 2 would feel for a user if say they deleted their account, and their avatar remains, if they check the file URL cat-avatar.jpg
it would still remain if someone else so happens to use the exact same file.
The implementation of both systems is simple, I'm not concerned about managing associations and deleting images if no associations exist. I'm more concerned with best practices and pitfalls I've yet to consider.
So what would you do?