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!
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!
2 Likes
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.
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! 
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.0) and putting the files to send in that.
That was it! thank you guys so much @benwilson512 @sodapopcan
1 Like