From 8d352d2f05874dd9283360ed3dfc1fa7fc3e530d Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Mon, 10 Feb 2020 17:18:00 +0100 Subject: Fix the stuffs --- lib/waev/export.ex | 53 ++++++++++++-------------- lib/waev_web/controllers/exports_controller.ex | 19 +++++++-- lib/waev_web/router.ex | 1 + lib/waev_web/templates/exports/show.html.eex | 8 ++-- 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 @@
@@ -20,15 +22,13 @@
<%= case message.attachment do %> - <% %Waev.Export.Message.File{filename: filename, available: true, type: :photo} -> %> + <% %Waev.Export.Message.File{filename: filename, type: :image} -> %> <% _ -> %> <% end %>