2018-08-28 03:20:54 +02:00
|
|
|
defmodule Pleroma.Uploaders.Local do
|
2018-08-28 14:57:41 +02:00
|
|
|
@behaviour Pleroma.Uploaders.Uploader
|
2018-08-28 03:20:54 +02:00
|
|
|
|
2018-08-28 03:45:53 +02:00
|
|
|
alias Pleroma.Web
|
|
|
|
|
2018-08-29 00:48:03 +02:00
|
|
|
def put_file(name, uuid, tmpfile, _content_type, should_dedupe) do
|
2018-08-28 03:45:53 +02:00
|
|
|
upload_folder = get_upload_path(uuid, should_dedupe)
|
2018-08-28 03:20:54 +02:00
|
|
|
url_path = get_url(name, uuid, should_dedupe)
|
|
|
|
|
|
|
|
File.mkdir_p!(upload_folder)
|
|
|
|
|
|
|
|
result_file = Path.join(upload_folder, name)
|
|
|
|
|
|
|
|
if File.exists?(result_file) do
|
2018-08-29 00:48:03 +02:00
|
|
|
File.rm!(tmpfile)
|
2018-08-28 03:20:54 +02:00
|
|
|
else
|
2018-08-29 00:48:03 +02:00
|
|
|
File.cp!(tmpfile, result_file)
|
2018-08-28 03:20:54 +02:00
|
|
|
end
|
|
|
|
|
2018-08-30 03:07:28 +02:00
|
|
|
{:ok, url_path}
|
2018-08-28 03:20:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def upload_path do
|
|
|
|
settings = Application.get_env(:pleroma, Pleroma.Uploaders.Local)
|
|
|
|
Keyword.fetch!(settings, :uploads)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_upload_path(uuid, should_dedupe) do
|
|
|
|
if should_dedupe do
|
|
|
|
upload_path()
|
|
|
|
else
|
|
|
|
Path.join(upload_path(), uuid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_url(name, uuid, should_dedupe) do
|
|
|
|
if should_dedupe do
|
|
|
|
url_for(:cow_uri.urlencode(name))
|
|
|
|
else
|
|
|
|
url_for(Path.join(uuid, :cow_uri.urlencode(name)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp url_for(file) do
|
2018-09-01 17:14:14 +02:00
|
|
|
settings = Application.get_env(:pleroma, Pleroma.Uploaders.Local)
|
|
|
|
|
|
|
|
Keyword.get(settings, :uploads_url)
|
|
|
|
|> String.replace("{{file}}", file)
|
|
|
|
|> String.replace("{{base_url}}", Web.base_url())
|
2018-08-28 03:20:54 +02:00
|
|
|
end
|
|
|
|
end
|