1 defmodule Pleroma.Uploaders.S3 do
2 @behaviour Pleroma.Uploaders.Uploader
5 # The file name is re-encoded with S3's constraints here to comply with previous links with less strict filenames
7 config = Pleroma.Config.get([__MODULE__])
12 Keyword.fetch!(config, :public_endpoint),
13 Keyword.fetch!(config, :bucket),
14 strict_encode(URI.decode(file))
18 def put_file(name, uuid, path, content_type, _opts) do
19 config = Pleroma.Config.get([__MODULE__])
20 bucket = Keyword.get(config, :bucket)
22 {:ok, file_data} = File.read(path)
26 s3_name = "#{uuid}/#{strict_encode(name)}"
29 ExAws.S3.put_object(bucket, s3_name, file_data, [
31 {:content_type, content_type}
34 case ExAws.request(op) do
36 {:ok, {:file, s3_name}}
39 Logger.error("#{__MODULE__}: #{inspect(error)}")
40 {:error, "S3 Upload failed"}
44 @regex Regex.compile!("[^0-9a-zA-Z!.*/'()_-]")
45 def strict_encode(name) do
46 String.replace(name, @regex, "-")