chris-menz

chris-menz

Hello, I need to set up file download that happens dynamically at runtime. Essentially there are images stored on s3 that need to be downloaded, sorted into a folder and then sent to the clients browser as a zip file. The code for doing all this is set up and working properly. The problem I’m running into is the controller will return without waiting for all the images to download, it returns right away so only 2-5 get downloaded, for my use case there could be 100+ images that need to be downloaded. I’m not sure why the code is not running asynchronously.

If i remove the connection return at the end of the controller, then all images get downloaded as intended, but it throws an error as the controller must return a connection.

lib/router.ex

    get "/orders/:id", OrderController, :order_page
    get "/order-download/:id", OrderController, :download_order

lib/controller/order_controller.ex

  def order_page(conn, %{"id" => id}) do
    order = SnapshotManagement.get_order(id)

    render(conn, "order.html", order: order)
  end

  def download_order(conn, %{"id" => id}) do
## the image downloader function downloads all the images to a 
## folder in priv/static and returns the path to that folder
## this is working as intended so not including the code here
    path = ImageDownloader.download_images_for_order(String.to_integer(id))
    old_path = File.cwd!
    File.cd!(path)
    {:ok, filename} = :zip.create("order.zip", ['./'], [{:cwd, path}])
    File.cd!(old_path)

    send_download(conn, {:file, path <> "/" <> filename})

    File.rm_rf!(path)

    redirect(conn, to: "/order/" <> id)
  end

order.html.eex

<li class="list-group-item"><a href="/order-download/<%= @order.id %>" class="btn btn-primary btn-lg" tabindex="-1" role="button" aria-disabled="true">Download</a></li>

Any help or ideas for other setups are appreciated, thanks!

Showing Posts 1 to 6

sodapopcan

sodapopcan

Try changing:

send_download(conn, {:file, path <> "/" <> filename})

to

conn = send_download(conn, {:file, path <> "/" <> filename})

…which should have the right headers on it (sorry, I did not test this). Remember, conn is an immutable struct!

chris-menz

chris-menz OP

The code doesnt even execute to this point. When I click the button, it starts executing the download function, but then stops and redirects back to the order page.

Heres the console output:

[info] GET /orders/1
[debug] Processing with SnapshotManagerWeb.OrderController.order_page/2
  Parameters: %{"id" => "1"}
  Pipelines: [:browser]
[debug] QUERY OK source="orders" db=5.6ms queue=1.1ms idle=281.8ms
SELECT o0."id", o0."sub_total", o0."notes", o0."user_id", o0."shipping_address", o0."billing_address", o0."order_items", o0."inserted_at", o0."updated_at" FROM "orders" AS o0 WHERE (o0."id" = $1) [1]
[info] Sent 200 in 129ms
[info] GET /order-download/1
[debug] Processing with SnapshotManagerWeb.OrderController.download_order/2
  Parameters: %{"id" => "1"}
  Pipelines: [:browser]
[debug] QUERY OK source="orders" db=1.9ms idle=1894.1ms
SELECT o0."id", o0."sub_total", o0."notes", o0."user_id", o0."shipping_address", o0."billing_address", o0."order_items", o0."inserted_at", o0."updated_at" FROM "orders" AS o0 WHERE (o0."id" = $1) [1]
[debug] Live reload: priv/static/order-1/album-2/3.png
[debug] Live reload: priv/static/order-1/album-2/4.png
[info] GET /orders/1
[debug] Processing with SnapshotManagerWeb.OrderController.order_page/2
  Parameters: %{"id" => "1"}
  Pipelines: [:browser]
[debug] QUERY OK source="orders" db=0.6ms idle=698.7ms
SELECT o0."id", o0."sub_total", o0."notes", o0."user_id", o0."shipping_address", o0."billing_address", o0."order_items", o0."inserted_at", o0."updated_at" FROM "orders" AS o0 WHERE (o0."id" = $1) [1]
[info] Sent 200 in 2ms

You can see that there are two photos downloaded in this instance. There should be many more but you can see that redirect happen immediately which kills the downloading function. That redirect is not coming from the redirect function in the code though which is throwing me off. I can add a pry() or IO.puts directly after this line

path = ImageDownloader.download_images_for_order(String.to_integer(id))

and it doesnt get called. So something is happening to stop the execution of the controller function.

sodapopcan

sodapopcan

It looks like something is triggering Live Reload based on that error! The only think I can think of is that you are downloading to a location that is being watched by the reloader, like in priv/ perhaps?

When it does get figured out, you should still definitely change that send_download line! :slight_smile:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Where are you putting these files that you are downloading? If you’re putting them in priv/static then you’re triggering the live code reloader since you’re changing the files available. This immediately is triggering a refresh on the front end.

I would consider making a temporary directory (consider Briefly Usage Guide — briefly v0.5.1) and putting the files to send in that.

chris-menz

chris-menz OP

That was it! thank you guys so much @benwilson512 @sodapopcan

LostKobrakai

LostKobrakai

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
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
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews