mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-12-26 10:46:13 +00:00
f77ec96707
According to [the S3 docs][s3], the characters safe for use in object keys are: * 0-9 * a-z * A-Z * ! * - * _ * . * * * ' * ( * ) (The / character is not listed but mentioned being safe outside of the list.) Several characters that are valid in filenames can cause problems, for example spaces are not valid in URLs and need to be escaped, sequences of spaces can become squeezed by S3, some characters like \ are documented to require “significant special handling”. To avoid these problems, this change encodes the filename before using it as part of the S3 object name by replacing all characters except those documented as “safe” with dashes. [s3]: https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
29 lines
745 B
Elixir
29 lines
745 B
Elixir
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
|