From f0567f4f808f5140fdfae00b616b5f4e605e9152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Garc=C3=ADa=20Isa=C3=ADa?= Date: Thu, 26 Sep 2024 18:15:00 -0300 Subject: [PATCH] Statically serve the generated files See #2350 --- lib/ask/survey_results.ex | 4 ++-- lib/ask_web/controllers/respondent_controller.ex | 10 ++-------- lib/ask_web/router.ex | 4 ++++ 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/ask/survey_results.ex b/lib/ask/survey_results.ex index 75ce3477f..0f3818b5c 100644 --- a/lib/ask/survey_results.ex +++ b/lib/ask/survey_results.ex @@ -365,8 +365,8 @@ defmodule Ask.SurveyResults do end defp write_to_file(file_type, survey, rows) do - File.mkdir_p!(@target_dir) - target_file = file_path(survey, file_type, @target_dir) + File.mkdir_p!("./priv/static/" <> @target_dir) + target_file = "./priv/static/" <> file_path(survey, file_type, @target_dir) if File.exists?(target_file), do: File.rm!(target_file) # Poor man's mktemp. We only want to avoid having the file living at the stable diff --git a/lib/ask_web/controllers/respondent_controller.ex b/lib/ask_web/controllers/respondent_controller.ex index a4e6f6f56..838b359ef 100644 --- a/lib/ask_web/controllers/respondent_controller.ex +++ b/lib/ask_web/controllers/respondent_controller.ex @@ -729,17 +729,11 @@ defmodule AskWeb.RespondentController do conn end - # FIXME: this function should return the proper file URL/path - defp generated_file_url(%{id: survey_id}, {:results, _filter}), do: "/?some-thingy" - defp generated_file_url(%{id: survey_id}, :disposition_history), do: "#dispositon_history" - defp generated_file_url(%{id: survey_id}, :incentives), do: "#incentives-#{survey_id}" - defp generated_file_url(%{id: survey_id}, :interactions), do: "#interactions-#{survey_id}" - defp file_redirection(conn, survey, file_type) do - file_url = generated_file_url(survey, file_type) + file_url = SurveyResults.file_path(survey, file_type) render(conn, "file-redirect.json", - file_url: file_url + file_url: "/#{file_url}" # TODO: there may be better ways of avoiding relative URLs ) end diff --git a/lib/ask_web/router.ex b/lib/ask_web/router.ex index 5813b3530..b981a19f8 100644 --- a/lib/ask_web/router.ex +++ b/lib/ask_web/router.ex @@ -11,6 +11,10 @@ defmodule AskWeb.Router do plug :protect_from_forgery plug :put_secure_browser_headers plug Plug.Static, at: "files/", from: "assets/static/files/" + + # FIXME: this is temporary. We have to apply access control here: https://elixirforum.com/t/how-can-you-hide-certain-static-assets-behind-a-require-authenticated-user-path/53106/6 + plug Plug.Static, at: "generated_files/", from: "priv/static/generated_files/" + plug Coherence.Authentication.Session, db_model: Ask.User end