Skip to content

Commit

Permalink
Statically serve the generated files
Browse files Browse the repository at this point in the history
See #2350
  • Loading branch information
matiasgarciaisaia committed Sep 26, 2024
1 parent 45976ae commit f0567f4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/ask/survey_results.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions lib/ask_web/controllers/respondent_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/ask_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f0567f4

Please sign in to comment.