1

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.

  1. 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.

  2. 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?

1
  • I've attempted an answer, but I'm not sure this question is a good fit here as you have not provided details as to the purpose of storing images and and who uploads them and any answer is likely to be too opinion based.
    – davidgo
    Commented Nov 21 at 8:14

2 Answers 2

1

If two users have uploaded the exact same image, then I'd assume that at least one user doesn't own the copyright.

It seems more likey that a third party would want an image removed, when one or more of your users have uploaded their copyrighted image. Ultimately, a complaint from an ex-user about their image is going to require the same action as a complaint from such a third party - presumably to delete it.

Your approach only seems to consider saving memory, when (if your site is public and open to all) you need to presume that some people will upload adult and copyrighted content, and have a moderation approach ready to go.

0

I think the second option (ie 1 image for multiple posts) is more error prone - especially if you need to keep track of the links yourself in a high level language.

A hybrid model may be to have a 1:1 mapping per your option 1 but to use an underlying filesystem that supports deduplication. In this way you would save disk space but would miss out on possible bandwidth savings/potential caching savings of some of the images.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.