aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/uploaders/s3.ex
blob: 87322753dd97b29e0ad23226a1d2819b7e53c604 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
defmodule Pleroma.Uploaders.S3 do
  @behaviour Pleroma.Uploaders.Uploader

  def put_file(name, uuid, path, content_type, _should_dedupe) do
    settings = Application.get_env(:pleroma, Pleroma.Uploaders.S3)
    bucket = Keyword.fetch!(settings, :bucket)
    public_endpoint = Keyword.fetch!(settings, :public_endpoint)

    {:ok, file_data} = File.read(path)

    File.rm!(path)

    s3_name = "#{uuid}/#{encode(name)}"

    {:ok, _} =
      ExAws.S3.put_object(bucket, s3_name, file_data, [
        {:acl, :public_read},
        {:content_type, content_type}
      ])
      |> ExAws.request()

    {:ok, "#{public_endpoint}/#{bucket}/#{s3_name}"}
  end

  defp encode(name) do
    String.replace(name, ~r/[^0-9a-zA-Z!.*'()_-]/, "-")
  end
end