6dbef90856c2fba0f9f5bddc52d56743cef7c81d
[akkoma] / lib / pleroma / uploaders / s3.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Uploaders.S3 do
6 @behaviour Pleroma.Uploaders.Uploader
7 require Logger
8
9 alias Pleroma.Config
10
11 # The file name is re-encoded with S3's constraints here to comply with previous
12 # links with less strict filenames
13 @impl true
14 def get_file(file) do
15 config = Config.get([__MODULE__])
16 bucket = Keyword.fetch!(config, :bucket)
17
18 bucket_with_namespace =
19 cond do
20 truncated_namespace = Keyword.get(config, :truncated_namespace) ->
21 truncated_namespace
22
23 namespace = Keyword.get(config, :bucket_namespace) ->
24 namespace <> ":" <> bucket
25
26 true ->
27 bucket
28 end
29
30 {:ok,
31 {:url,
32 Path.join([
33 Keyword.fetch!(config, :public_endpoint),
34 bucket_with_namespace,
35 strict_encode(URI.decode(file))
36 ])}}
37 end
38
39 @impl true
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)
44
45 s3_name = strict_encode(upload.path)
46
47 op =
48 if streaming do
49 op =
50 upload.tempfile
51 |> ExAws.S3.Upload.stream_file()
52 |> ExAws.S3.upload(bucket, s3_name, [
53 {:acl, :public_read},
54 {:content_type, upload.content_type}
55 ])
56
57 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do
58 # set s3 upload timeout to respect :upload pool timeout
59 # timeout should be slightly larger, so s3 can retry upload on fail
60 timeout = Pleroma.HTTP.AdapterHelper.Gun.pool_timeout(:upload) + 1_000
61 opts = Keyword.put(op.opts, :timeout, timeout)
62 Map.put(op, :opts, opts)
63 else
64 op
65 end
66 else
67 {:ok, file_data} = File.read(upload.tempfile)
68
69 ExAws.S3.put_object(bucket, s3_name, file_data, [
70 {:acl, :public_read},
71 {:content_type, upload.content_type}
72 ])
73 end
74
75 case ExAws.request(op) do
76 {:ok, _} ->
77 {:ok, {:file, s3_name}}
78
79 error ->
80 Logger.error("#{__MODULE__}: #{inspect(error)}")
81 {:error, "S3 Upload failed"}
82 end
83 end
84
85 @impl true
86 def delete_file(file) do
87 [__MODULE__, :bucket]
88 |> Config.get()
89 |> ExAws.S3.delete_object(file)
90 |> ExAws.request()
91 |> case do
92 {:ok, %{status_code: 204}} -> :ok
93 error -> {:error, inspect(error)}
94 end
95 end
96
97 @regex Regex.compile!("[^0-9a-zA-Z!.*/'()_-]")
98 def strict_encode(name) do
99 String.replace(name, @regex, "-")
100 end
101 end