aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/waev/export.ex53
-rw-r--r--lib/waev_web/controllers/exports_controller.ex19
-rw-r--r--lib/waev_web/router.ex1
-rw-r--r--lib/waev_web/templates/exports/show.html.eex8
4 files changed, 46 insertions, 35 deletions
diff --git a/lib/waev/export.ex b/lib/waev/export.ex
index 2efebb3..eabb96b 100644
--- a/lib/waev/export.ex
+++ b/lib/waev/export.ex
@@ -2,61 +2,59 @@ defmodule Waev.Export do
require Logger
defmodule Party do
- defstruct name: nil, photo: nil
+ defstruct name: nil
- def lookup(name) do
- %Party{name: name, photo: nil}
+ def avatar_path(e_id, filename) do
+ path = "#{Waev.Export.export_path(e_id)}/avatars/#{filename}"
+ if File.regular?(path), do: path, else: nil
+ end
+
+ def new(name) do
+ %Party{name: name}
end
end
defmodule Message do
defmodule File do
- defstruct filename: nil, available: nil, type: nil
+ # type is either :image or :file
+ defstruct filename: nil, type: nil
- def exists?(e_id, filename) do
- Elixir.File.regular?(Waev.Export.media_path(e_id, filename))
+ def media_path(e_id, filename) do
+ path = "#{Waev.Export.export_path(e_id)}/media/#{filename}"
+ if Elixir.File.regular?(path), do: path, else: nil
end
- def photo_extension?(filename) do
+
+ def is_image?(filename) do
valid_extensions = ["png", "jpeg", "jpg"]
- extension =
- filename |> String.split(".") |> Enum.at(-1) |> String.downcase()
+ extension = filename |> String.split(".") |> Enum.at(-1) |> String.downcase()
Enum.member?(valid_extensions, extension)
end
- def path(e_id, filename) do
- path = Waev.Export.media_path(e_id, filename)
- if Elixir.File.regular?(path) do
- {:ok, path}
- else
- :error
- end
+ def new(filename) do
+ type = if is_image?(filename), do: :image, else: :file
+ %File{filename: filename, type: type}
end
end
defstruct side: nil, date: nil, text: nil, attachment: nil
- def parse(e, side, datetime, text) do
+ def parse(side, datetime, text) do
{text, attachment} =
case Regex.run(~r/([[:ascii:]]+) \(archivo adjunto\)$/u, text) do
[_, filename] ->
- available = File.path(e.id, filename) != :error
- if Message.File.photo_extension?(filename) do
- {nil, %File{filename: filename, available: available, type: :photo}}
- else
- {nil, %File{filename: filename, available: available, type: :file}}
- end
+ {nil, File.new(filename)}
nil ->
{text, nil}
end
+ IO.puts("Attachment: #{inspect(attachment)}")
%Message{side: side, date: datetime, text: text, attachment: attachment}
end
end
defstruct id: nil, left: nil, right: nil, messages: []
-
def list do
case File.ls(path()) do
{:error, _reason} ->
@@ -82,13 +80,13 @@ defmodule Waev.Export do
{e, side} =
case {e.left, e.right, name} do
{nil, _, _} ->
- {%{e | left: Party.lookup(name)}, :left}
+ {%{e | left: Party.new(name)}, :left}
{%{name: left}, _, left} ->
{e, :left}
{_, nil, _} ->
- {%{e | right: Party.lookup(name)}, :right}
+ {%{e | right: Party.new(name)}, :right}
{_, %{name: right}, right} ->
{e, :right}
@@ -103,7 +101,7 @@ defmodule Waev.Export do
{e, nil}
end
- message = Message.parse(e, side, datetime, text)
+ message = Message.parse(side, datetime, text)
%{e | messages: [message | e.messages]}
@@ -138,7 +136,6 @@ defmodule Waev.Export do
def path, do: Application.fetch_env!(:waev, __MODULE__)[:exports_path]
def export_path(e_id), do: "#{path()}/#{e_id}"
def chat_path(e_id), do: "#{export_path(e_id)}/chat.txt"
- def media_path(e_id, media_id), do: "#{export_path(e_id)}/media/#{media_id}"
defp exists?(e_id) do
File.dir?(export_path(e_id)) && File.regular?(chat_path(e_id))
diff --git a/lib/waev_web/controllers/exports_controller.ex b/lib/waev_web/controllers/exports_controller.ex
index 764accf..f2b6fb4 100644
--- a/lib/waev_web/controllers/exports_controller.ex
+++ b/lib/waev_web/controllers/exports_controller.ex
@@ -15,15 +15,28 @@ defmodule WaevWeb.ExportsController do
end
def get_media(conn, %{"id" => id, "at_id" => at_id}) do
- case Waev.Export.Message.File.path(id, at_id) do
- {:ok, path} ->
+ case Waev.Export.Message.File.media_path(id, at_id) do
+ nil ->
+ conn
+ |> put_status(:not_found)
+ |> put_view(WaevWeb.ErrorView)
+ |> render("404.html")
+
+ path ->
send_download(conn, {:file, path}, filename: at_id)
+ end
+ end
- :error ->
+ def get_avatar(conn, %{"id" => id, "av_id" => av_id}) do
+ case Waev.Export.Party.avatar_path(id, av_id) do
+ nil ->
conn
|> put_status(:not_found)
|> put_view(WaevWeb.ErrorView)
|> render("404.html")
+
+ path ->
+ send_download(conn, {:file, path}, filename: av_id)
end
end
end
diff --git a/lib/waev_web/router.ex b/lib/waev_web/router.ex
index 1e9ed0f..29d1b20 100644
--- a/lib/waev_web/router.ex
+++ b/lib/waev_web/router.ex
@@ -20,6 +20,7 @@ defmodule WaevWeb.Router do
# index,edit,new,show,create,update
resources "/exports", ExportsController, only: [:show]
get "/exports/:id/media/:at_id", ExportsController, :get_media
+ get "/exports/:id/avatars/:av_id", ExportsController, :get_avatar
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 265374a..4d7de74 100644
--- a/lib/waev_web/templates/exports/show.html.eex
+++ b/lib/waev_web/templates/exports/show.html.eex
@@ -1,9 +1,11 @@
<div class="container">
<div class="row">
<div class="column column-50 wa-avatar">
+ <img alt="<%= @export.left.name %>" src="<%= Routes.exports_path(@conn, :get_avatar, @id, @export.left.name) %>" />
<%= @export.left.name %>
</div>
<div class="column column-50 wa-avatar">
+ <img alt="<%= @export.right.name %>" src="<%= Routes.exports_path(@conn, :get_avatar, @id, @export.right.name) %>" />
<%= @export.right.name %>
</div>
</div>
@@ -20,15 +22,13 @@
<div class="wa-message">
<%= case message.attachment do %>
- <% %Waev.Export.Message.File{filename: filename, available: true, type: :photo} -> %>
+ <% %Waev.Export.Message.File{filename: filename, type: :image} -> %>
<img class="wa-message-photo" alt="<%= filename %>" src="<%= Routes.exports_path(@conn, :get_media, @id, filename) %>" />
<% _ -> %>
<% end %>
<div class="wa-message-box">
<%= case message.attachment do %>
- <% %Waev.Export.Message.File{filename: filename, available: false} -> %>
- <i class="wa-message-text"><%= filename %></i> (fichero adjunto no disponible)
- <% %Waev.Export.Message.File{filename: filename, available: true, type: :file} -> %>
+ <% %Waev.Export.Message.File{filename: filename, type: :file} -> %>
<a class="wa-message-text" href="<%= Routes.exports_path(@conn, :get_media, @id, filename) %>"><%= filename %></a>
<% _ -> %>
<% end %>