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