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)
26 "mediaType" => content_type,
34 def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
35 parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
36 data = Base.decode64!(parsed["data"], ignore: :whitespace)
37 uuid = UUID.generate()
38 uuidpath = Path.join(upload_path(), uuid)
39 uuid = UUID.generate()
41 File.mkdir_p!(upload_path())
43 File.write!(uuidpath, data)
45 content_type = get_content_type(uuidpath)
49 String.downcase(Base.encode16(:crypto.hash(:sha256, data))),
54 upload_folder = get_upload_path(uuid, should_dedupe)
55 url_path = get_url(name, uuid, should_dedupe)
57 File.mkdir_p!(upload_folder)
58 result_file = Path.join(upload_folder, name)
61 if !File.exists?(result_file) do
62 File.rename(uuidpath, result_file)
67 File.rename(uuidpath, result_file)
75 "mediaType" => content_type,
84 settings = Application.get_env(:pleroma, Pleroma.Upload)
85 Keyword.fetch!(settings, :uploads)
88 defp create_name(uuid, ext, type) do
90 "application/octet-stream" ->
91 String.downcase(Enum.join([uuid, ext], "."))
94 String.downcase(Enum.join([uuid, "mp3"], "."))
97 String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
101 defp get_uuid(file, should_dedupe) do
103 Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
109 defp get_name(file, uuid, type, should_dedupe) do
111 create_name(uuid, List.last(String.split(file.filename, ".")), type)
113 unless String.contains?(file.filename, ".") do
115 "image/png" -> file.filename <> ".png"
116 "image/jpeg" -> file.filename <> ".jpg"
117 "image/gif" -> file.filename <> ".gif"
118 "video/webm" -> file.filename <> ".webm"
119 "video/mp4" -> file.filename <> ".mp4"
120 "audio/mpeg" -> file.filename <> ".mp3"
121 "audio/ogg" -> file.filename <> ".ogg"
122 "audio/wav" -> file.filename <> ".wav"
131 defp get_upload_path(uuid, should_dedupe) do
135 Path.join(upload_path(), uuid)
139 defp get_url(name, uuid, should_dedupe) do
141 url_for(:cow_uri.urlencode(name))
143 url_for(Path.join(uuid, :cow_uri.urlencode(name)))
147 defp url_for(file) do
148 "#{Web.base_url()}/media/#{file}"
151 def get_content_type(file) do
153 File.open(file, [:read], fn f ->
154 case IO.binread(f, 8) do
155 <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>> ->
158 <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
161 <<0xFF, 0xD8, 0xFF, _, _, _, _, _>> ->
164 <<0x1A, 0x45, 0xDF, 0xA3, _, _, _, _>> ->
167 <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
170 <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
173 <<255, 251, _, 68, 0, 0, 0, 0>> ->
176 <<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
179 <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
183 "application/octet-stream"
189 _e -> "application/octet-stream"