diff options
author | Guillermo Ramos | 2020-02-05 14:17:41 +0100 |
---|---|---|
committer | Guillermo Ramos | 2020-02-05 14:17:41 +0100 |
commit | a0eec8eadae94a71974868ac8d48820e0662857b (patch) | |
tree | f85dad657a8f7bea59fbb96e39a7e506986b8ed5 | |
parent | 01143da6e79f26cfacefa0c17f225ccc88d15186 (diff) | |
download | waev-a0eec8eadae94a71974868ac8d48820e0662857b.tar.gz |
Download attachments
-rw-r--r-- | lib/waev/export.ex | 64 | ||||
-rw-r--r-- | lib/waev_web/controllers/exports_controller.ex | 13 | ||||
-rw-r--r-- | lib/waev_web/router.ex | 1 | ||||
-rw-r--r-- | lib/waev_web/templates/exports/show.html.eex | 8 |
4 files changed, 58 insertions, 28 deletions
diff --git a/lib/waev/export.ex b/lib/waev/export.ex index a69fa33..e513fd1 100644 --- a/lib/waev/export.ex +++ b/lib/waev/export.ex @@ -15,16 +15,26 @@ defmodule Waev.Export do end defmodule File do - defstruct filename: nil + defstruct filename: nil, available: nil + + def path(e_id, filename) do + path = "#{Waev.Export.export_path(e_id)}/media/#{filename}" + + if Elixir.File.regular?(path) do + {:ok, path} + else + :error + end + end end defstruct side: nil, date: nil, text: nil, attachment: nil - def parse(side, datetime, text) do + def parse(e, side, datetime, text) do {text, attachment} = - case Regex.run(~r/^([^ ]+) \(archivo adjunto\)$/u, text) do - [^text, filename] -> - {nil, %File{filename: filename}} + case Regex.run(~r/([[:ascii:]]+) \(archivo adjunto\)$/u, text) do + [_, filename] -> + {nil, %File{filename: filename, available: File.path(e.id, filename) != :error}} nil -> {text, nil} @@ -34,7 +44,7 @@ defmodule Waev.Export do end end - defstruct left: nil, right: nil, messages: [] + defstruct id: nil, left: nil, right: nil, messages: [] def path do Application.fetch_env!(:waev, __MODULE__)[:exports_path] @@ -50,31 +60,31 @@ defmodule Waev.Export do end end - def get(e) do - case exists?(e) do + def get(e_id) do + case exists?(e_id) do true -> - export = - File.stream!("#{path()}/#{e}/chat.txt") + e = + File.stream!("#{export_path(e_id)}/chat.txt") |> Enum.take(200) - |> Enum.reduce(%Waev.Export{}, fn line, export -> + |> Enum.reduce(%Waev.Export{id: e_id}, fn line, e -> line = String.trim(line) case Regex.run(~r/^(\d+\/\d+\/\d+ \d+:\d+) - ([^:]+): (.*)$/u, line) do # Match: new message [^line, datetime, name, text] -> - {export, side} = - case {export.left, export.right, name} do + {e, side} = + case {e.left, e.right, name} do {nil, _, _} -> - {%{export | left: Party.lookup(name)}, :left} + {%{e | left: Party.lookup(name)}, :left} {%{name: left}, _, left} -> - {export, :left} + {e, :left} {_, nil, _} -> - {%{export | right: Party.lookup(name)}, :right} + {%{e | right: Party.lookup(name)}, :right} {_, %{name: right}, right} -> - {export, :right} + {e, :right} {left, right, name} -> Logger.error( @@ -83,17 +93,17 @@ defmodule Waev.Export do }" ) - {export, nil} + {e, nil} end - message = Message.parse(side, datetime, text) + message = Message.parse(e, side, datetime, text) - %{export | messages: [message | export.messages]} + %{e | messages: [message | e.messages]} # Otherwise: text continuing from the previous one. We need to # append it to the last message. nil -> - case export.messages do + case e.messages do [first | rest] -> text = case first.text do @@ -102,24 +112,26 @@ defmodule Waev.Export do end first = %{first | text: text} - %{export | messages: [first | rest]} + %{e | messages: [first | rest]} # ... unless we are before the first message. Drop it. _ -> - export + e end end end) - {:ok, export} + {:ok, e} false -> :error end end - defp exists?(e) do - e_path = "#{path()}/#{e}" + def export_path(e_id), do: "#{path()}/#{e_id}" + + defp exists?(e_id) do + e_path = export_path(e_id) File.dir?(e_path) && File.regular?("#{e_path}/chat.txt") end 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 %> <div class="column wa-message column-50 column-offset-25"> <% end %> <%= case message.attachment do %> - <% %Waev.Export.Message.File{filename: filename} -> %> - <a href="#"><%= filename %></a> + <% %Waev.Export.Message.File{filename: filename} = file -> %> + <%= if file.available do %> + <a href="<%= Routes.exports_path(@conn, :get_attachment, @id, filename) %>"><%= filename %></a> + <% else %> + <i><%= filename %></i> (fichero adjunto no disponible) + <% end %> <% nil -> %> <% end %> <%= message.text %> |