magosi

magosi

Serving static user uploaded files in Phoenix

I am planning to make a web app that must support user uploads. Think of a quiz type of website where each question can have an associated image. I know how to handle file uploads by the user, but I have no idea where to store the uploaded files and how to serve them statically for download but also for templates to embed the images in Html. I want to do it locally, for now (no S3), and I have heard about Plug.Static, but then again I am unsure if that is the best solution. Imagine I store these user uploaded files in multiple folders and eventually my app will expand to the point where other file types are uploaded than images.

In short, where should I store and how can I serve static user uploaded files so that:

1.) They are available for download by the user (think Plug.Conn.send_file/5)
2.) They are accessible in my Html templates (for example: <image src="/images/img.jpg"> or
<embed src="/docs/file.pdf">) but someone cannot access them if they are unauthorized and tries to steal the user uploaded files. (For instance, someone should not be able to access the static file by going straight to mywebsite.com

As a side note, I often realise in the process of planning a web app project that I lack knowledge about the design and dynamics of web apps and how it all fits together.
If anyone can link me to good resources to inform myself better about these concepts I would really appreciate it.

Thank you all for your time and effort.

Marked As Solved

mindok

mindok

  1. Where you store them is up to you. send_file should work with any path that the server has access to. Maybe “/uploads” under your project folder?

  2. You would create a route to a controller where the parameter allows you to find the file. That route, with appropriate parameters to find the image file, is what you embed dynamically as the image link into your template when it is rendered. The controller can then handle authorisation when the browser follows the image link, and if all good, serves the file via send_file.

If you google “Phoenix Elixir send_file” you should find some samples. If not, I’ll take a look when I get back to the office tomorrow - I have done this recently so can get some code samples.

Also Liked

shanesveller

shanesveller

Note that if you will ever run on more than one server, this technique is not sustainable as you will get irregular 404s based on which backend node you connect to on any given file request. This is why the S3 approach and similar techniques are so desirable, and as a bonus they help pave the way for separate hostnames for static files.

That in turn paves the way for CDN/caching techniques, or other avenues that allow you to take Elixir out of the equation for static content. It’s all a spectrum based on what your time/effort appetite allows for, and if this is an internal/side project you may never need to take it that far.

magosi

magosi

Wow. It is so simple. Thank you very much. I appreciate your time and effort. I would love to see the code snippets as well.

mindok

mindok

I went digging and remembered that I actually used a very good online tutorial to learn most of the tricks I needed to get this work. Here it is: Step-by-Step Tutorial to Build a Phoenix App that Supports User Upload

I didn’t do a lot of what was here (e.g. I didn’t want the ImageMagick dependency), but there’s plenty there to adapt to your situation I think.

Last Post!

magosi

magosi

Sorry my mistake.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement