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