19287c53220aa713b7c874700e4b34be817cebdc
[anni] / lib / pleroma / uploaders / s3.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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     {:ok,
16      {:url,
17       Path.join([
18         Pleroma.Upload.base_url(),
19         strict_encode(URI.decode(file))
20       ])}}
21   end
22
23   @impl true
24   def put_file(%Pleroma.Upload{} = upload) do
25     config = Config.get([__MODULE__])
26     bucket = Keyword.get(config, :bucket)
27     streaming = Keyword.get(config, :streaming_enabled)
28
29     s3_name = strict_encode(upload.path)
30
31     op =
32       if streaming do
33         op =
34           upload.tempfile
35           |> ExAws.S3.Upload.stream_file()
36           |> ExAws.S3.upload(bucket, s3_name, [
37             {:acl, :public_read},
38             {:content_type, upload.content_type}
39           ])
40
41         if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do
42           # set s3 upload timeout to respect :upload pool timeout
43           # timeout should be slightly larger, so s3 can retry upload on fail
44           timeout = Pleroma.HTTP.AdapterHelper.Gun.pool_timeout(:upload) + 1_000
45           opts = Keyword.put(op.opts, :timeout, timeout)
46           Map.put(op, :opts, opts)
47         else
48           op
49         end
50       else
51         {:ok, file_data} = File.read(upload.tempfile)
52
53         ExAws.S3.put_object(bucket, s3_name, file_data, [
54           {:acl, :public_read},
55           {:content_type, upload.content_type}
56         ])
57       end
58
59     case ExAws.request(op) do
60       {:ok, _} ->
61         {:ok, {:file, s3_name}}
62
63       error ->
64         Logger.error("#{__MODULE__}: #{inspect(error)}")
65         {:error, "S3 Upload failed"}
66     end
67   end
68
69   @impl true
70   def delete_file(file) do
71     [__MODULE__, :bucket]
72     |> Config.get()
73     |> ExAws.S3.delete_object(file)
74     |> ExAws.request()
75     |> case do
76       {:ok, %{status_code: 204}} -> :ok
77       error -> {:error, inspect(error)}
78     end
79   end
80
81   @regex Regex.compile!("[^0-9a-zA-Z!.*/'()_-]")
82   def strict_encode(name) do
83     String.replace(name, @regex, "-")
84   end
85 end