Search code examples
haskellyesod

Yesod - Should user profile pictures be stored in the static directory?


Currently profile pictures that the users upload are saved to /static/avatars/{upload-date}. I then generate the StaticR route for the image via information stored in the database (they were added at runtime so the StaticR route doesn't already exist).

The problem I've run into is that when a user updates their profile picture they still get the old one due to the caching of static files. Is there some way around this or should I be storing the images somewhere else? If so then how do I go about accessing the images from say /avatars/{upload-date}/{userid}.png?

I know I could create a route along the lines of /avatars/#Day/#UserId but I'm not sure how I would get the ".png" or ".jpg" appended to that. Or what I would write for the handler function.


Solution

  • I know I could create a route along the lines of /avatars/#Day/#UserId but I'm not sure how I would get the ".png" or ".jpg" appended to th

    There is no need that your url should end up with a png or jpeg extension. All you need to make sure is that your content type header is set properly.

    Or what I would write for the handler function.

    That's quite simple. If you are serving png images, then all you have to do is use the functions sendFile and typePng to serve them:

    myAvatarHandler = sendFile typePng "/home/yesod/static/avatars/day/userid.png"
    

    Also in your example, make sure to store timestamp of the upload and change your url according to that. Or else two profile images updated in the same day will be cached.