2018-08-27 19:20:54 -06:00
|
|
|
defmodule Pleroma.Uploaders.S3 do
|
2018-10-29 12:00:59 -06:00
|
|
|
alias Pleroma.Web.MediaProxy
|
|
|
|
|
2018-08-28 06:57:41 -06:00
|
|
|
@behaviour Pleroma.Uploaders.Uploader
|
2018-08-27 19:20:54 -06:00
|
|
|
|
2018-08-27 19:45:53 -06:00
|
|
|
def put_file(name, uuid, path, content_type, _should_dedupe) do
|
2018-08-27 19:20:54 -06:00
|
|
|
settings = Application.get_env(:pleroma, Pleroma.Uploaders.S3)
|
|
|
|
bucket = Keyword.fetch!(settings, :bucket)
|
|
|
|
public_endpoint = Keyword.fetch!(settings, :public_endpoint)
|
2018-10-29 12:00:59 -06:00
|
|
|
force_media_proxy = Keyword.fetch!(settings, :force_media_proxy)
|
2018-08-27 19:20:54 -06:00
|
|
|
|
|
|
|
{:ok, file_data} = File.read(path)
|
|
|
|
|
|
|
|
File.rm!(path)
|
|
|
|
|
2018-09-24 07:38:32 -06:00
|
|
|
s3_name = "#{uuid}/#{encode(name)}"
|
2018-08-27 19:20:54 -06:00
|
|
|
|
2018-08-27 19:45:53 -06:00
|
|
|
{:ok, _} =
|
2018-08-27 19:20:54 -06:00
|
|
|
ExAws.S3.put_object(bucket, s3_name, file_data, [
|
|
|
|
{:acl, :public_read},
|
|
|
|
{:content_type, content_type}
|
|
|
|
])
|
|
|
|
|> ExAws.request()
|
|
|
|
|
2018-10-29 12:00:59 -06:00
|
|
|
url_base = "#{public_endpoint}/#{bucket}/#{s3_name}"
|
|
|
|
|
|
|
|
public_url =
|
|
|
|
if force_media_proxy do
|
|
|
|
MediaProxy.url(url_base)
|
|
|
|
else
|
|
|
|
url_base
|
|
|
|
end
|
|
|
|
|
|
|
|
{:ok, public_url}
|
2018-08-27 19:20:54 -06:00
|
|
|
end
|
2018-09-24 07:38:32 -06:00
|
|
|
|
|
|
|
defp encode(name) do
|
|
|
|
String.replace(name, ~r/[^0-9a-zA-Z!.*'()_-]/, "-")
|
|
|
|
end
|
2018-08-27 19:20:54 -06:00
|
|
|
end
|