2293ff54e4fab1d3604771980207fab48c3925d9
[akkoma] / lib / pleroma / upload.ex
1 defmodule Pleroma.Upload do
2 alias Ecto.UUID
3
4 @storage_backend Application.get_env(:pleroma, Pleroma.Upload)
5 |> Keyword.fetch!(:uploader)
6
7 def check_file_size(path, nil), do: true
8
9 def check_file_size(path, size_limit) do
10 {:ok, %{size: size}} = File.stat(path)
11 size <= size_limit
12 end
13
14 def store(file, should_dedupe, size_limit \\ nil)
15
16 def store(%Plug.Upload{} = file, should_dedupe, size_limit) do
17 content_type = get_content_type(file.path)
18
19 with uuid <- get_uuid(file, should_dedupe),
20 name <- get_name(file, uuid, content_type, should_dedupe),
21 true <- check_file_size(file.path, size_limit) do
22 strip_exif_data(content_type, file.path)
23
24 {:ok, url_path} =
25 @storage_backend.put_file(name, uuid, file.path, content_type, should_dedupe)
26
27 %{
28 "type" => "Document",
29 "url" => [
30 %{
31 "type" => "Link",
32 "mediaType" => content_type,
33 "href" => url_path
34 }
35 ],
36 "name" => name
37 }
38 else
39 _e -> nil
40 end
41 end
42
43 def store(%{"img" => "data:image/" <> image_data}, should_dedupe, size_limit) do
44 parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
45 data = Base.decode64!(parsed["data"], ignore: :whitespace)
46
47 with tmp_path <- tempfile_for_image(data),
48 uuid <- UUID.generate(),
49 true <- check_file_size(tmp_path, size_limit) do
50 content_type = get_content_type(tmp_path)
51 strip_exif_data(content_type, tmp_path)
52
53 name =
54 create_name(
55 String.downcase(Base.encode16(:crypto.hash(:sha256, data))),
56 parsed["filetype"],
57 content_type
58 )
59
60 {:ok, url_path} =
61 @storage_backend.put_file(name, uuid, tmp_path, content_type, should_dedupe)
62
63 %{
64 "type" => "Image",
65 "url" => [
66 %{
67 "type" => "Link",
68 "mediaType" => content_type,
69 "href" => url_path
70 }
71 ],
72 "name" => name
73 }
74 else
75 _e -> nil
76 end
77 end
78
79 @doc """
80 Creates a tempfile using the Plug.Upload Genserver which cleans them up
81 automatically.
82 """
83 def tempfile_for_image(data) do
84 {:ok, tmp_path} = Plug.Upload.random_file("profile_pics")
85 {:ok, tmp_file} = File.open(tmp_path, [:write, :raw, :binary])
86 IO.binwrite(tmp_file, data)
87
88 tmp_path
89 end
90
91 def strip_exif_data(content_type, file) do
92 settings = Application.get_env(:pleroma, Pleroma.Upload)
93 do_strip = Keyword.fetch!(settings, :strip_exif)
94 [filetype, _ext] = String.split(content_type, "/")
95
96 if filetype == "image" and do_strip == true do
97 Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true)
98 end
99 end
100
101 defp create_name(uuid, ext, type) do
102 case type do
103 "application/octet-stream" ->
104 String.downcase(Enum.join([uuid, ext], "."))
105
106 "audio/mpeg" ->
107 String.downcase(Enum.join([uuid, "mp3"], "."))
108
109 _ ->
110 String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
111 end
112 end
113
114 defp get_uuid(file, should_dedupe) do
115 if should_dedupe do
116 Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
117 else
118 UUID.generate()
119 end
120 end
121
122 defp get_name(file, uuid, type, should_dedupe) do
123 if should_dedupe do
124 create_name(uuid, List.last(String.split(file.filename, ".")), type)
125 else
126 parts = String.split(file.filename, ".")
127
128 new_filename =
129 if length(parts) > 1 do
130 Enum.drop(parts, -1) |> Enum.join(".")
131 else
132 Enum.join(parts)
133 end
134
135 case type do
136 "application/octet-stream" -> file.filename
137 "audio/mpeg" -> new_filename <> ".mp3"
138 "image/jpeg" -> new_filename <> ".jpg"
139 _ -> Enum.join([new_filename, String.split(type, "/") |> List.last()], ".")
140 end
141 end
142 end
143
144 def get_content_type(file) do
145 match =
146 File.open(file, [:read], fn f ->
147 case IO.binread(f, 8) do
148 <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>> ->
149 "image/png"
150
151 <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
152 "image/gif"
153
154 <<0xFF, 0xD8, 0xFF, _, _, _, _, _>> ->
155 "image/jpeg"
156
157 <<0x1A, 0x45, 0xDF, 0xA3, _, _, _, _>> ->
158 "video/webm"
159
160 <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
161 "video/mp4"
162
163 <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
164 "audio/mpeg"
165
166 <<255, 251, _, 68, 0, 0, 0, 0>> ->
167 "audio/mpeg"
168
169 <<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
170 "audio/ogg"
171
172 <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
173 "audio/wav"
174
175 _ ->
176 "application/octet-stream"
177 end
178 end)
179
180 case match do
181 {:ok, type} -> type
182 _e -> "application/octet-stream"
183 end
184 end
185 end