1 defmodule Pleroma.Upload do
4 def check_file_size(path, nil), do: true
6 def check_file_size(path, size_limit) do
7 {:ok, %{size: size}} = File.stat(path)
11 def store(file, should_dedupe, size_limit \\ nil)
13 def store(%Plug.Upload{} = file, should_dedupe, size_limit) do
14 content_type = get_content_type(file.path)
16 with uuid <- get_uuid(file, should_dedupe),
17 name <- get_name(file, uuid, content_type, should_dedupe),
18 true <- check_file_size(file.path, size_limit) do
19 strip_exif_data(content_type, file.path)
21 {:ok, url_path} = uploader().put_file(name, uuid, file.path, content_type, should_dedupe)
28 "mediaType" => content_type,
39 def store(%{"img" => "data:image/" <> image_data}, should_dedupe, size_limit) do
40 parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
41 data = Base.decode64!(parsed["data"], ignore: :whitespace)
43 with tmp_path <- tempfile_for_image(data),
44 uuid <- UUID.generate(),
45 true <- check_file_size(tmp_path, size_limit) do
46 content_type = get_content_type(tmp_path)
47 strip_exif_data(content_type, tmp_path)
51 String.downcase(Base.encode16(:crypto.hash(:sha256, data))),
56 {:ok, url_path} = uploader().put_file(name, uuid, tmp_path, content_type, should_dedupe)
63 "mediaType" => content_type,
75 Creates a tempfile using the Plug.Upload Genserver which cleans them up
78 def tempfile_for_image(data) do
79 {:ok, tmp_path} = Plug.Upload.random_file("profile_pics")
80 {:ok, tmp_file} = File.open(tmp_path, [:write, :raw, :binary])
81 IO.binwrite(tmp_file, data)
86 def strip_exif_data(content_type, file) do
87 settings = Application.get_env(:pleroma, Pleroma.Upload)
88 do_strip = Keyword.fetch!(settings, :strip_exif)
89 [filetype, _ext] = String.split(content_type, "/")
91 if filetype == "image" and do_strip == true do
92 Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true)
96 defp create_name(uuid, ext, type) do
98 "application/octet-stream" ->
99 String.downcase(Enum.join([uuid, ext], "."))
102 String.downcase(Enum.join([uuid, "mp3"], "."))
105 String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
109 defp get_uuid(file, should_dedupe) do
111 Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
117 defp get_name(file, uuid, type, should_dedupe) do
119 create_name(uuid, List.last(String.split(file.filename, ".")), type)
121 parts = String.split(file.filename, ".")
124 if length(parts) > 1 do
125 Enum.drop(parts, -1) |> Enum.join(".")
131 "application/octet-stream" -> file.filename
132 "audio/mpeg" -> new_filename <> ".mp3"
133 "image/jpeg" -> new_filename <> ".jpg"
134 _ -> Enum.join([new_filename, String.split(type, "/") |> List.last()], ".")
139 def get_content_type(file) do
141 File.open(file, [:read], fn f ->
142 case IO.binread(f, 8) do
143 <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>> ->
146 <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
149 <<0xFF, 0xD8, 0xFF, _, _, _, _, _>> ->
152 <<0x1A, 0x45, 0xDF, 0xA3, _, _, _, _>> ->
155 <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
158 <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
161 <<255, 251, _, 68, 0, 0, 0, 0>> ->
164 <<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
165 case IO.binread(f, 27) do
166 <<_::size(160), 0x80, 0x74, 0x68, 0x65, 0x6F, 0x72, 0x61>> ->
173 <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
177 "application/octet-stream"
183 _e -> "application/octet-stream"
188 Pleroma.Config.get!([Pleroma.Upload, :uploader])