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