Home > Net >  Database design - where to store a default profile picture?
Database design - where to store a default profile picture?

Time:08-10

Consider a ProfilePicture entity such as:

@Entity
public class ProfilePicture {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private byte[] image;

  @OneToOne
  private Employee employee;
}

Each profile picture is associated with an employee - if a profile picture exists, it must be associated with an employee. However, it is totally fine if there is an employee, who does not have a profile picture.

I would now like to create a REST endpoint, which allows for requesting a profile picture to a given employee id. If no profile picture exists to the provided employee id, I want to return a default picture.

The problem is that I don't know where to store the default picture: I would like to store the default picture inside my profile picture database table. That's impossible though, because each picture has to be associated with an employee and the default picture is not. I cannot create a dummy entry representing the default employee inside my employee database table either, because I want to be able to request all employees with another REST call and I don't want to have to filter out this dummy entry every time.

This should be a common issue and I am wondering about the best solution to it. I could create an extra database table holding nothing but the default picture, but really I don't know if that's a good idea.

CodePudding user response:

I would prefer to store image in AWS s3 bucket and use the s3 path something like s3://bucket-name/some-profile-id/picture.jpeg.

(It does not matter for your problem but SCNR) I will also enable cloudfront enable for all images in the s3 path.

for default image, I can assign default path in profile table for those users. Storing image is also possible as you already showed in the entity. But in future it would be difficult to maintain and having cdn - cloudfront like this for faster access would be very difficult.

CodePudding user response:

I would store the image in an s3 bucket, and use some property file to store the the URL for the default pfp.

Then you can include the default pfp url in an initial response when the application is started and just use that if there exists no pfp for the current user.

You can use an application like MinIO for mocking an s3 bucket locally.

Other options might include storing a byte array in some “files” table, or having the file present on your server and just have the API fetch the file directly.

  • Related