1 defmodule Pleroma.Upload do
5 def store(%Plug.Upload{} = file, should_dedupe) do
6 content_type = get_content_type(file.path)
7 uuid = get_uuid(file, should_dedupe)
8 name = get_name(file, uuid, content_type, should_dedupe)
9 upload_folder = get_upload_path(uuid, should_dedupe)
10 url_path = get_url(name, uuid, should_dedupe)
12 File.mkdir_p!(upload_folder)
13 result_file = Path.join(upload_folder, name)
15 if File.exists?(result_file) do
18 File.cp!(file.path, result_file)
21 strip_exif_data(content_type, result_file)
28 "mediaType" => content_type,
36 def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
37 parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
38 data = Base.decode64!(parsed["data"], ignore: :whitespace)
39 uuid = UUID.generate()
40 uuidpath = Path.join(upload_path(), uuid)
41 uuid = UUID.generate()
43 File.mkdir_p!(upload_path())
45 File.write!(uuidpath, data)
47 content_type = get_content_type(uuidpath)
51 String.downcase(Base.encode16(:crypto.hash(:sha256, data))),
56 upload_folder = get_upload_path(uuid, should_dedupe)
57 url_path = get_url(name, uuid, should_dedupe)
59 File.mkdir_p!(upload_folder)
60 result_file = Path.join(upload_folder, name)
63 if !File.exists?(result_file) do
64 File.rename(uuidpath, result_file)
69 File.rename(uuidpath, result_file)
72 strip_exif_data(content_type, result_file)
79 "mediaType" => content_type,
87 def strip_exif_data(content_type, file) do
88 settings = Application.get_env(:pleroma, Pleroma.Upload)
89 do_strip = Keyword.fetch!(settings, :strip_exif)
90 [filetype, ext] = String.split(content_type, "/")
92 if filetype == "image" and do_strip == true do
93 Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true)
98 settings = Application.get_env(:pleroma, Pleroma.Upload)
99 Keyword.fetch!(settings, :uploads)
102 defp create_name(uuid, ext, type) do
104 "application/octet-stream" ->
105 String.downcase(Enum.join([uuid, ext], "."))
108 String.downcase(Enum.join([uuid, "mp3"], "."))
111 String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
115 defp get_uuid(file, should_dedupe) do
117 Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
123 defp get_name(file, uuid, type, should_dedupe) do
125 create_name(uuid, List.last(String.split(file.filename, ".")), type)
127 unless String.contains?(file.filename, ".") do
129 "image/png" -> file.filename <> ".png"
130 "image/jpeg" -> file.filename <> ".jpg"
131 "image/gif" -> file.filename <> ".gif"
132 "video/webm" -> file.filename <> ".webm"
133 "video/mp4" -> file.filename <> ".mp4"
134 "audio/mpeg" -> file.filename <> ".mp3"
135 "audio/ogg" -> file.filename <> ".ogg"
136 "audio/wav" -> file.filename <> ".wav"
145 defp get_upload_path(uuid, should_dedupe) do
149 Path.join(upload_path(), uuid)
153 defp get_url(name, uuid, should_dedupe) do
155 url_for(:cow_uri.urlencode(name))
157 url_for(Path.join(uuid, :cow_uri.urlencode(name)))
161 defp url_for(file) do
162 "#{Web.base_url()}/media/#{file}"
165 def get_content_type(file) do
167 File.open(file, [:read], fn f ->
168 case IO.binread(f, 8) do
169 <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>> ->
172 <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
175 <<0xFF, 0xD8, 0xFF, _, _, _, _, _>> ->
178 <<0x1A, 0x45, 0xDF, 0xA3, _, _, _, _>> ->
181 <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
184 <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
187 <<255, 251, _, 68, 0, 0, 0, 0>> ->
190 <<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
193 <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
197 "application/octet-stream"
203 _e -> "application/octet-stream"