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

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
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
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
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews