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(upload = %Pleroma.Upload{}) do
19 config = Pleroma.Config.get([__MODULE__])
20 bucket = Keyword.get(config, :bucket)
22 {:ok, file_data} = File.read(upload.tempfile)
24 s3_name = strict_encode(upload.path)
27 ExAws.S3.put_object(bucket, s3_name, file_data, [
29 {:content_type, upload.content_type}
32 case ExAws.request(op) do
34 {:ok, {:file, s3_name}}
37 Logger.error("#{__MODULE__}: #{inspect(error)}")
38 {:error, "S3 Upload failed"}
42 @regex Regex.compile!("[^0-9a-zA-Z!.*/'()_-]")
43 def strict_encode(name) do
44 String.replace(name, @regex, "-")