magosi

magosi

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.

First 7 of 7 Posts Switch mode

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.

magosi

magosi OP

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.

magosi

magosi OP

Ah thank you. I have stumbled upon that tutorial as well. I will experiment a bit. I think I should look for production elixir apps and study them.

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.

JeyHey

JeyHey

Why is this the solution?

magosi

magosi OP

Sorry my mistake.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement