qb_akkoma/lib/pleroma/upload.ex

33 lines
845 B
Elixir
Raw Normal View History

2017-03-28 23:39:01 +00:00
defmodule Pleroma.Upload do
def store(%Plug.Upload{} = file) do
uuid = Ecto.UUID.generate
upload_folder = Path.join(upload_path(), uuid)
File.mkdir_p!(upload_folder)
result_file = Path.join(upload_folder, file.filename)
File.cp!(file.path, result_file)
%{
"type" => "Image",
"href" => url_for(Path.join(uuid, file.filename)),
"name" => file.filename,
"uuid" => uuid
}
end
defp upload_path do
Application.get_env(:pleroma, Pleroma.Upload)
|> Keyword.fetch!(:uploads)
end
defp url_for(file) do
host =
Application.get_env(:pleroma, Pleroma.Web.Endpoint)
|> Keyword.fetch!(:url)
|> Keyword.fetch!(:host)
2017-03-29 00:05:51 +00:00
protocol = Application.get_env(:pleroma, Pleroma.Web.Endpoint) |> Keyword.fetch!(:protocol)
"#{protocol}://#{host}/media/#{file}"
2017-03-28 23:39:01 +00:00
end
end