How can I render an image file that's not in assets?

I’m trying to build something like Imgur, so going to "/1" would show a page with the image/comments/etc. but "/1.jpg" would just render the image (for hotlinking). My images are held in "/uploads/DATE_UPLOADED/FILENAME", not in "/assets". If a user visits "/1.jpg", how could I render the file that’s sitting in "/uploads/1_6_2018/hawaii.jpg"? (already have the code to access the filepath/file from the ID, just not sure how to serve it so it can be hotlinked).

A easy way I can think of is copy Plug.Static and change how path was build.

I would just look it up in the DB (probably this is a good case for CacheX to be in front) to see which one to send out and then send_file it out.

You can also build a static list that nginx can use to dispatch directly to the right file instantly though, that is what I’d do for performance. Or maybe even symlinks!

1 Like