1 defmodule Pleroma.Upload do
4 @storage_backend Application.get_env(:pleroma, Pleroma.Upload)
5 |> Keyword.fetch!(:uploader)
7 def check_file_size(path, nil), do: true
9 def check_file_size(path, size_limit) do
10 {:ok, %{size: size}} = File.stat(path)
14 def store(file, should_dedupe, size_limit \\ nil)
16 def store(%Plug.Upload{} = file, should_dedupe, size_limit) do
17 content_type = get_content_type(file.path)
19 with uuid <- get_uuid(file, should_dedupe),
20 name <- get_name(file, uuid, content_type, should_dedupe),
21 true <- check_file_size(file.path, size_limit) do
22 strip_exif_data(content_type, file.path)
25 @storage_backend.put_file(name, uuid, file.path, content_type, should_dedupe)
32 "mediaType" => content_type,
43 def store(%{"img" => "data:image/" <> image_data}, should_dedupe, size_limit) do
44 parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
45 data = Base.decode64!(parsed["data"], ignore: :whitespace)
47 with tmp_path <- tempfile_for_image(data),
48 uuid <- UUID.generate(),
49 true <- check_file_size(tmp_path, size_limit) do
50 content_type = get_content_type(tmp_path)
51 strip_exif_data(content_type, tmp_path)
55 String.downcase(Base.encode16(:crypto.hash(:sha256, data))),
61 @storage_backend.put_file(name, uuid, tmp_path, content_type, should_dedupe)
68 "mediaType" => content_type,
80 Creates a tempfile using the Plug.Upload Genserver which cleans them up
83 def tempfile_for_image(data) do
84 {:ok, tmp_path} = Plug.Upload.random_file("profile_pics")
85 {:ok, tmp_file} = File.open(tmp_path, [:write, :raw, :binary])
86 IO.binwrite(tmp_file, data)
91 def strip_exif_data(content_type, file) do
92 settings = Application.get_env(:pleroma, Pleroma.Upload)
93 do_strip = Keyword.fetch!(settings, :strip_exif)
94 [filetype, _ext] = String.split(content_type, "/")
96 if filetype == "image" and do_strip == true do
97 Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true)
101 defp create_name(uuid, ext, type) do
103 "application/octet-stream" ->
104 String.downcase(Enum.join([uuid, ext], "."))
107 String.downcase(Enum.join([uuid, "mp3"], "."))
110 String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
114 defp get_uuid(file, should_dedupe) do
116 Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
122 defp get_name(file, uuid, type, should_dedupe) do
124 create_name(uuid, List.last(String.split(file.filename, ".")), type)
126 parts = String.split(file.filename, ".")
129 if length(parts) > 1 do
130 Enum.drop(parts, -1) |> Enum.join(".")
136 "application/octet-stream" -> file.filename
137 "audio/mpeg" -> new_filename <> ".mp3"
138 "image/jpeg" -> new_filename <> ".jpg"
139 _ -> Enum.join([new_filename, String.split(type, "/") |> List.last()], ".")
144 def get_content_type(file) do
146 File.open(file, [:read], fn f ->
147 case IO.binread(f, 8) do
148 <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>> ->
151 <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
154 <<0xFF, 0xD8, 0xFF, _, _, _, _, _>> ->
157 <<0x1A, 0x45, 0xDF, 0xA3, _, _, _, _>> ->
160 <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
163 <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
166 <<255, 251, _, 68, 0, 0, 0, 0>> ->
169 <<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
172 <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
176 "application/octet-stream"
182 _e -> "application/octet-stream"