From a0eec8eadae94a71974868ac8d48820e0662857b Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Wed, 5 Feb 2020 14:17:41 +0100 Subject: Download attachments --- lib/waev_web/controllers/exports_controller.ex | 13 +++++++++++++ lib/waev_web/router.ex | 1 + lib/waev_web/templates/exports/show.html.eex | 8 ++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) (limited to 'lib/waev_web') diff --git a/lib/waev_web/controllers/exports_controller.ex b/lib/waev_web/controllers/exports_controller.ex index a3dde48..fd776a9 100644 --- a/lib/waev_web/controllers/exports_controller.ex +++ b/lib/waev_web/controllers/exports_controller.ex @@ -13,4 +13,17 @@ defmodule WaevWeb.ExportsController do |> render("404.html") end end + + def get_attachment(conn, %{"id" => id, "at_id" => at_id}) do + case Waev.Export.Message.File.path(id, at_id) do + {:ok, path} -> + send_download(conn, {:file, path}, filename: at_id) + + :error -> + conn + |> put_status(:not_found) + |> put_view(WaevWeb.ErrorView) + |> render("404.html") + end + end end diff --git a/lib/waev_web/router.ex b/lib/waev_web/router.ex index edd2c69..54667a8 100644 --- a/lib/waev_web/router.ex +++ b/lib/waev_web/router.ex @@ -19,6 +19,7 @@ defmodule WaevWeb.Router do get "/", PageController, :index # index,edit,new,show,create,update resources "/exports", ExportsController, only: [:show] + get "/exports/:id/attachments/:at_id", ExportsController, :get_attachment end # Other scopes may use custom stacks. diff --git a/lib/waev_web/templates/exports/show.html.eex b/lib/waev_web/templates/exports/show.html.eex index 2d72e2e..77727a8 100644 --- a/lib/waev_web/templates/exports/show.html.eex +++ b/lib/waev_web/templates/exports/show.html.eex @@ -20,8 +20,12 @@ ID: <%= @id %>