1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Uploaders.S3 do
6 @behaviour Pleroma.Uploaders.Uploader
11 # The file name is re-encoded with S3's constraints here to comply with previous
12 # links with less strict filenames
15 config = Config.get([__MODULE__])
16 bucket = Keyword.fetch!(config, :bucket)
18 bucket_with_namespace =
20 truncated_namespace = Keyword.get(config, :truncated_namespace) ->
23 namespace = Keyword.get(config, :bucket_namespace) ->
24 namespace <> ":" <> bucket
33 Keyword.fetch!(config, :public_endpoint),
34 bucket_with_namespace,
35 strict_encode(URI.decode(file))
40 def put_file(%Pleroma.Upload{} = upload) do
41 config = Config.get([__MODULE__])
42 bucket = Keyword.get(config, :bucket)
43 streaming = Keyword.get(config, :streaming_enabled)
45 s3_name = strict_encode(upload.path)
51 |> ExAws.S3.Upload.stream_file()
52 |> ExAws.S3.upload(bucket, s3_name, [
54 {:content_type, upload.content_type}
57 # set s3 upload timeout to respect :upload pool timeout
58 # timeout should be slightly larger, so s3 can retry upload on fail
59 timeout = Pleroma.HTTP.AdapterHelper.pool_timeout(:upload) + 1_000
60 opts = Keyword.put(op.opts, :timeout, timeout)
61 Map.put(op, :opts, opts)
63 {:ok, file_data} = File.read(upload.tempfile)
65 ExAws.S3.put_object(bucket, s3_name, file_data, [
67 {:content_type, upload.content_type}
71 case ExAws.request(op) do
73 {:ok, {:file, s3_name}}
76 Logger.error("#{__MODULE__}: #{inspect(error)}")
77 {:error, "S3 Upload failed"}
82 def delete_file(file) do
85 |> ExAws.S3.delete_object(file)
88 {:ok, %{status_code: 204}} -> :ok
89 error -> {:error, inspect(error)}
93 @regex Regex.compile!("[^0-9a-zA-Z!.*/'()_-]")
94 def strict_encode(name) do
95 String.replace(name, @regex, "-")