Merge branch 'fix-mastodon-notifications-without-nickname' into 'develop'
[akkoma] / lib / pleroma / upload.ex
1 defmodule Pleroma.Upload do
2 alias Ecto.UUID
3 alias Pleroma.Web
4
5 def store(%Plug.Upload{} = file, should_dedupe) do
6 settings = Application.get_env(:pleroma, Pleroma.Upload)
7 use_s3 = Keyword.fetch!(settings, :use_s3)
8
9 content_type = get_content_type(file.path)
10 uuid = get_uuid(file, should_dedupe)
11 name = get_name(file, uuid, content_type, should_dedupe)
12 upload_folder = get_upload_path(uuid, should_dedupe)
13 url_path = get_url(name, uuid, should_dedupe)
14
15 strip_exif_data(content_type, file.path)
16
17 File.mkdir_p!(upload_folder)
18 result_file = Path.join(upload_folder, name)
19
20 if File.exists?(result_file) do
21 File.rm!(file.path)
22 else
23 File.cp!(file.path, result_file)
24 end
25
26 url_path =
27 if use_s3 do
28 put_s3_file(name, uuid, result_file, content_type)
29 else
30 url_path
31 end
32
33 %{
34 "type" => "Document",
35 "url" => [
36 %{
37 "type" => "Link",
38 "mediaType" => content_type,
39 "href" => url_path
40 }
41 ],
42 "name" => name
43 }
44 end
45
46 # XXX: does this code actually work? i am skeptical. --kaniini
47 def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
48 settings = Application.get_env(:pleroma, Pleroma.Upload)
49 use_s3 = Keyword.fetch!(settings, :use_s3)
50
51 parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
52 data = Base.decode64!(parsed["data"], ignore: :whitespace)
53 uuid = UUID.generate()
54 uuidpath = Path.join(upload_path(), uuid)
55 uuid = UUID.generate()
56
57 File.mkdir_p!(upload_path())
58
59 File.write!(uuidpath, data)
60
61 content_type = get_content_type(uuidpath)
62
63 name =
64 create_name(
65 String.downcase(Base.encode16(:crypto.hash(:sha256, data))),
66 parsed["filetype"],
67 content_type
68 )
69
70 upload_folder = get_upload_path(uuid, should_dedupe)
71 url_path = get_url(name, uuid, should_dedupe)
72
73 File.mkdir_p!(upload_folder)
74 result_file = Path.join(upload_folder, name)
75
76 if should_dedupe do
77 if !File.exists?(result_file) do
78 File.rename(uuidpath, result_file)
79 else
80 File.rm!(uuidpath)
81 end
82 else
83 File.rename(uuidpath, result_file)
84 end
85
86 strip_exif_data(content_type, result_file)
87
88 url_path =
89 if use_s3 do
90 put_s3_file(name, uuid, result_file, content_type)
91 else
92 url_path
93 end
94
95 %{
96 "type" => "Image",
97 "url" => [
98 %{
99 "type" => "Link",
100 "mediaType" => content_type,
101 "href" => url_path
102 }
103 ],
104 "name" => name
105 }
106 end
107
108 def strip_exif_data(content_type, file) do
109 settings = Application.get_env(:pleroma, Pleroma.Upload)
110 do_strip = Keyword.fetch!(settings, :strip_exif)
111 [filetype, ext] = String.split(content_type, "/")
112
113 if filetype == "image" and do_strip == true do
114 Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true)
115 end
116 end
117
118 def upload_path do
119 settings = Application.get_env(:pleroma, Pleroma.Upload)
120 Keyword.fetch!(settings, :uploads)
121 end
122
123 defp create_name(uuid, ext, type) do
124 case type do
125 "application/octet-stream" ->
126 String.downcase(Enum.join([uuid, ext], "."))
127
128 "audio/mpeg" ->
129 String.downcase(Enum.join([uuid, "mp3"], "."))
130
131 _ ->
132 String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
133 end
134 end
135
136 defp get_uuid(file, should_dedupe) do
137 if should_dedupe do
138 Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
139 else
140 UUID.generate()
141 end
142 end
143
144 defp get_name(file, uuid, type, should_dedupe) do
145 if should_dedupe do
146 create_name(uuid, List.last(String.split(file.filename, ".")), type)
147 else
148 parts = String.split(file.filename, ".")
149
150 new_filename =
151 if length(parts) > 1 do
152 Enum.drop(parts, -1) |> Enum.join(".")
153 else
154 Enum.join(parts)
155 end
156
157 case type do
158 "application/octet-stream" -> file.filename
159 "audio/mpeg" -> new_filename <> ".mp3"
160 "image/jpeg" -> new_filename <> ".jpg"
161 _ -> Enum.join([new_filename, String.split(type, "/") |> List.last()], ".")
162 end
163 end
164 end
165
166 defp get_upload_path(uuid, should_dedupe) do
167 if should_dedupe do
168 upload_path()
169 else
170 Path.join(upload_path(), uuid)
171 end
172 end
173
174 defp get_url(name, uuid, should_dedupe) do
175 if should_dedupe do
176 url_for(:cow_uri.urlencode(name))
177 else
178 url_for(Path.join(uuid, :cow_uri.urlencode(name)))
179 end
180 end
181
182 defp url_for(file) do
183 "#{Web.base_url()}/media/#{file}"
184 end
185
186 def get_content_type(file) do
187 match =
188 File.open(file, [:read], fn f ->
189 case IO.binread(f, 8) do
190 <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>> ->
191 "image/png"
192
193 <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
194 "image/gif"
195
196 <<0xFF, 0xD8, 0xFF, _, _, _, _, _>> ->
197 "image/jpeg"
198
199 <<0x1A, 0x45, 0xDF, 0xA3, _, _, _, _>> ->
200 "video/webm"
201
202 <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
203 "video/mp4"
204
205 <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
206 "audio/mpeg"
207
208 <<255, 251, _, 68, 0, 0, 0, 0>> ->
209 "audio/mpeg"
210
211 <<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
212 "audio/ogg"
213
214 <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
215 "audio/wav"
216
217 _ ->
218 "application/octet-stream"
219 end
220 end)
221
222 case match do
223 {:ok, type} -> type
224 _e -> "application/octet-stream"
225 end
226 end
227
228 defp put_s3_file(name, uuid, path, content_type) do
229 settings = Application.get_env(:pleroma, Pleroma.Upload)
230 bucket = Keyword.fetch!(settings, :bucket)
231 public_endpoint = Keyword.fetch!(settings, :public_endpoint)
232
233 {:ok, file_data} = File.read(path)
234
235 File.rm!(path)
236
237 s3_name = "#{uuid}/#{name}"
238
239 {:ok, result} =
240 ExAws.S3.put_object(bucket, s3_name, file_data, [
241 {:acl, :public_read},
242 {:content_type, content_type}
243 ])
244 |> ExAws.request()
245
246 "#{public_endpoint}/#{bucket}/#{s3_name}"
247 end
248 end