16149d4dd148632485516bc04fb94a93f13ec5d4
[akkoma] / lib / pleroma / upload.ex
1 defmodule Pleroma.Upload do
2 alias Ecto.UUID
3 import Logger
4
5 @storage_backend Application.get_env(:pleroma, Pleroma.Upload)
6 |> Keyword.fetch!(:uploader)
7
8 def store(%Plug.Upload{} = file, should_dedupe) do
9 content_type = get_content_type(file.path)
10
11 uuid = get_uuid(file, should_dedupe)
12 name = get_name(file, uuid, content_type, should_dedupe)
13
14 strip_exif_data(content_type, file.path)
15
16 url_path = @storage_backend.put_file(name, uuid, file, 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 # XXX: does this code actually work? i am skeptical. --kaniini
32 def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
33 parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
34 data = Base.decode64!(parsed["data"], ignore: :whitespace)
35
36 tmp_path = mkupload_for_image(data)
37
38 uuid = UUID.generate()
39
40 # create temp local storage, like plug upload provides for us.
41
42 Logger.info(tmp_path)
43
44 content_type = get_content_type(tmp_path)
45 strip_exif_data(content_type, tmp_path)
46
47 name =
48 create_name(
49 String.downcase(Base.encode16(:crypto.hash(:sha256, data))),
50 parsed["filetype"],
51 content_type
52 )
53
54 url_path = @storage_backend.put_file(name, uuid, tmp_path, content_type, should_dedupe)
55
56 %{
57 "type" => "Image",
58 "url" => [
59 %{
60 "type" => "Link",
61 "mediaType" => content_type,
62 "href" => url_path
63 }
64 ],
65 "name" => name
66 }
67 end
68
69 def mkupload_for_image(data) do
70 {:ok, tmp_path} = Plug.Upload.random_file("profile_pics")
71 :file.write_file(tmp_path, data, [:write, :raw, :exclusive, :binary])
72 tmp_path
73 end
74
75 def strip_exif_data(content_type, file) do
76 settings = Application.get_env(:pleroma, Pleroma.Upload)
77 do_strip = Keyword.fetch!(settings, :strip_exif)
78 [filetype, _ext] = String.split(content_type, "/")
79
80 if filetype == "image" and do_strip == true do
81 Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true)
82 end
83 end
84
85 defp create_name(uuid, ext, type) do
86 case type do
87 "application/octet-stream" ->
88 String.downcase(Enum.join([uuid, ext], "."))
89
90 "audio/mpeg" ->
91 String.downcase(Enum.join([uuid, "mp3"], "."))
92
93 _ ->
94 String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
95 end
96 end
97
98 defp get_uuid(file, should_dedupe) do
99 if should_dedupe do
100 Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
101 else
102 UUID.generate()
103 end
104 end
105
106 defp get_name(file, uuid, type, should_dedupe) do
107 if should_dedupe do
108 create_name(uuid, List.last(String.split(file.filename, ".")), type)
109 else
110 parts = String.split(file.filename, ".")
111
112 new_filename =
113 if length(parts) > 1 do
114 Enum.drop(parts, -1) |> Enum.join(".")
115 else
116 Enum.join(parts)
117 end
118
119 case type do
120 "application/octet-stream" -> file.filename
121 "audio/mpeg" -> new_filename <> ".mp3"
122 "image/jpeg" -> new_filename <> ".jpg"
123 _ -> Enum.join([new_filename, String.split(type, "/") |> List.last()], ".")
124 end
125 end
126 end
127
128 def get_content_type(file) do
129 match =
130 File.open(file, [:read], fn f ->
131 case IO.binread(f, 8) do
132 <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>> ->
133 "image/png"
134
135 <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
136 "image/gif"
137
138 <<0xFF, 0xD8, 0xFF, _, _, _, _, _>> ->
139 "image/jpeg"
140
141 <<0x1A, 0x45, 0xDF, 0xA3, _, _, _, _>> ->
142 "video/webm"
143
144 <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
145 "video/mp4"
146
147 <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
148 "audio/mpeg"
149
150 <<255, 251, _, 68, 0, 0, 0, 0>> ->
151 "audio/mpeg"
152
153 <<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
154 "audio/ogg"
155
156 <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
157 "audio/wav"
158
159 _ ->
160 "application/octet-stream"
161 end
162 end)
163
164 case match do
165 {:ok, type} -> type
166 _e -> "application/octet-stream"
167 end
168 end
169 end