43 lines
1.2 KiB
Elixir
43 lines
1.2 KiB
Elixir
defmodule ApiWeb.AttachmentController do
|
|
use ApiWeb, :controller
|
|
require Logger
|
|
import Ecto.Query
|
|
alias Api.Repo
|
|
|
|
def index(conn, params) do
|
|
decode = """
|
|
SELECT convert_from(decrypt(decode('#{params["idstudyattachment"]}', 'hex')::bytea, '1nf0rm3'::bytea, 'aes'), 'UTF8');
|
|
"""
|
|
idstudyattachment = Repo.query!(decode).rows |> hd() |> hd()
|
|
idstudyattachment = String.to_integer(idstudyattachment)
|
|
|
|
[attachment | _] = get_attachment(idstudyattachment)
|
|
Logger.info("Adjunto: #{inspect(attachment)}")
|
|
|
|
file_path = attachment[:path]
|
|
if File.exists?(file_path) do
|
|
conn
|
|
|> put_resp_content_type("application/#{attachment[:format]}")
|
|
|> put_resp_header("content-disposition", "inline; filename=#{attachment[:fullname]}")
|
|
|> send_file(200, file_path)
|
|
else
|
|
conn
|
|
|> send_resp(404, "File not found")
|
|
end
|
|
end
|
|
|
|
def get_attachment(idstudyattachment) do
|
|
query =
|
|
from sa in "studyattachments",
|
|
where: sa.idstudyattachment == ^idstudyattachment,
|
|
select: %{
|
|
attachment: sa.attachment,
|
|
path: sa.path,
|
|
s3_path: sa.s3_path,
|
|
format: sa.format,
|
|
fullname: fragment("concat(name, '.', format)")
|
|
}
|
|
Repo.all(query)
|
|
end
|
|
end
|