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