cleaning up a bit.
[akkoma] / lib / pleroma / upload.ex
1 defmodule Pleroma.Upload do
2 alias Ecto.UUID
3
4 def store(%Plug.Upload{} = file, should_dedupe) do
5 settings = Application.get_env(:pleroma, Pleroma.Upload)
6 storage_backend = Keyword.fetch!(settings, :storage_backend)
7
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, content_type)
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 # XXX: does this code actually work? i am skeptical. --kaniini
30 def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
31 settings = Application.get_env(:pleroma, Pleroma.Upload)
32 use_s3 = Keyword.fetch!(settings, :use_s3)
33
34 parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
35 data = Base.decode64!(parsed["data"], ignore: :whitespace)
36 uuid = UUID.generate()
37 uuidpath = Path.join(upload_path(), uuid)
38 uuid = UUID.generate()
39
40 File.mkdir_p!(upload_path())
41
42 File.write!(uuidpath, data)
43
44 content_type = get_content_type(uuidpath)
45
46 name =
47 create_name(
48 String.downcase(Base.encode16(:crypto.hash(:sha256, data))),
49 parsed["filetype"],
50 content_type
51 )
52
53 upload_folder = get_upload_path(uuid, should_dedupe)
54 url_path = get_url(name, uuid, should_dedupe)
55
56 File.mkdir_p!(upload_folder)
57 result_file = Path.join(upload_folder, name)
58
59 if should_dedupe do
60 if !File.exists?(result_file) do
61 File.rename(uuidpath, result_file)
62 else
63 File.rm!(uuidpath)
64 end
65 else
66 File.rename(uuidpath, result_file)
67 end
68
69 strip_exif_data(content_type, result_file)
70
71 url_path =
72 if use_s3 do
73 put_s3_file(name, uuid, result_file, content_type)
74 else
75 url_path
76 end
77
78 %{
79 "type" => "Image",
80 "url" => [
81 %{
82 "type" => "Link",
83 "mediaType" => content_type,
84 "href" => url_path
85 }
86 ],
87 "name" => name
88 }
89 end
90 """
91
92 def strip_exif_data(content_type, file) do
93 settings = Application.get_env(:pleroma, Pleroma.Upload)
94 do_strip = Keyword.fetch!(settings, :strip_exif)
95 [filetype, _ext] = String.split(content_type, "/")
96
97 if filetype == "image" and do_strip == true do
98 Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true)
99 end
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 def get_content_type(file) do
146 match =
147 File.open(file, [:read], fn f ->
148 case IO.binread(f, 8) do
149 <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>> ->
150 "image/png"
151
152 <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
153 "image/gif"
154
155 <<0xFF, 0xD8, 0xFF, _, _, _, _, _>> ->
156 "image/jpeg"
157
158 <<0x1A, 0x45, 0xDF, 0xA3, _, _, _, _>> ->
159 "video/webm"
160
161 <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
162 "video/mp4"
163
164 <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
165 "audio/mpeg"
166
167 <<255, 251, _, 68, 0, 0, 0, 0>> ->
168 "audio/mpeg"
169
170 <<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
171 "audio/ogg"
172
173 <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
174 "audio/wav"
175
176 _ ->
177 "application/octet-stream"
178 end
179 end)
180
181 case match do
182 {:ok, type} -> type
183 _e -> "application/octet-stream"
184 end
185 end
186 end