Implement uploader behaviour
[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 uuid = get_uuid(file, should_dedupe)
10 name = get_name(file, uuid, content_type, should_dedupe)
11
12 strip_exif_data(content_type, file.path)
13
14 url_path = @storage_backend.put_file(name, uuid, file, content_type, should_dedupe)
15
16 %{
17 "type" => "Document",
18 "url" => [
19 %{
20 "type" => "Link",
21 "mediaType" => content_type,
22 "href" => url_path
23 }
24 ],
25 "name" => name
26 }
27 end
28
29 """
30 # XXX: does this code actually work? i am skeptical. --kaniini
31 def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
32 settings = Application.get_env(:pleroma, Pleroma.Upload)
33 use_s3 = Keyword.fetch!(settings, :use_s3)
34
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 strip_exif_data(content_type, result_file)
71
72 url_path =
73 if use_s3 do
74 put_s3_file(name, uuid, result_file, content_type)
75 else
76 url_path
77 end
78
79 %{
80 "type" => "Image",
81 "url" => [
82 %{
83 "type" => "Link",
84 "mediaType" => content_type,
85 "href" => url_path
86 }
87 ],
88 "name" => name
89 }
90 end
91 """
92
93 def strip_exif_data(content_type, file) do
94 settings = Application.get_env(:pleroma, Pleroma.Upload)
95 do_strip = Keyword.fetch!(settings, :strip_exif)
96 [filetype, _ext] = String.split(content_type, "/")
97
98 if filetype == "image" and do_strip == true do
99 Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true)
100 end
101 end
102
103 defp create_name(uuid, ext, type) do
104 case type do
105 "application/octet-stream" ->
106 String.downcase(Enum.join([uuid, ext], "."))
107
108 "audio/mpeg" ->
109 String.downcase(Enum.join([uuid, "mp3"], "."))
110
111 _ ->
112 String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
113 end
114 end
115
116 defp get_uuid(file, should_dedupe) do
117 if should_dedupe do
118 Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
119 else
120 UUID.generate()
121 end
122 end
123
124 defp get_name(file, uuid, type, should_dedupe) do
125 if should_dedupe do
126 create_name(uuid, List.last(String.split(file.filename, ".")), type)
127 else
128 parts = String.split(file.filename, ".")
129
130 new_filename =
131 if length(parts) > 1 do
132 Enum.drop(parts, -1) |> Enum.join(".")
133 else
134 Enum.join(parts)
135 end
136
137 case type do
138 "application/octet-stream" -> file.filename
139 "audio/mpeg" -> new_filename <> ".mp3"
140 "image/jpeg" -> new_filename <> ".jpg"
141 _ -> Enum.join([new_filename, String.split(type, "/") |> List.last()], ".")
142 end
143 end
144 end
145
146 def get_content_type(file) do
147 match =
148 File.open(file, [:read], fn f ->
149 case IO.binread(f, 8) do
150 <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>> ->
151 "image/png"
152
153 <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
154 "image/gif"
155
156 <<0xFF, 0xD8, 0xFF, _, _, _, _, _>> ->
157 "image/jpeg"
158
159 <<0x1A, 0x45, 0xDF, 0xA3, _, _, _, _>> ->
160 "video/webm"
161
162 <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
163 "video/mp4"
164
165 <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
166 "audio/mpeg"
167
168 <<255, 251, _, 68, 0, 0, 0, 0>> ->
169 "audio/mpeg"
170
171 <<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
172 "audio/ogg"
173
174 <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
175 "audio/wav"
176
177 _ ->
178 "application/octet-stream"
179 end
180 end)
181
182 case match do
183 {:ok, type} -> type
184 _e -> "application/octet-stream"
185 end
186 end
187 end