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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews