1 defmodule Pleroma.Upload do
5 def store(%Plug.Upload{} = file, should_dedupe) do
6 settings = Application.get_env(:pleroma, Pleroma.Upload)
7 use_s3 = Keyword.fetch!(settings, :use_s3)
9 content_type = get_content_type(file.path)
10 uuid = get_uuid(file, should_dedupe)
11 name = get_name(file, uuid, content_type, should_dedupe)
12 upload_folder = get_upload_path(uuid, should_dedupe)
13 url_path = get_url(name, uuid, should_dedupe)
15 strip_exif_data(content_type, file.path)
17 File.mkdir_p!(upload_folder)
18 result_file = Path.join(upload_folder, name)
20 if File.exists?(result_file) do
23 File.cp!(file.path, result_file)
28 put_s3_file(name, uuid, result_file, content_type)
38 "mediaType" => content_type,
46 # XXX: does this code actually work? i am skeptical. --kaniini
47 def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
48 settings = Application.get_env(:pleroma, Pleroma.Upload)
49 use_s3 = Keyword.fetch!(settings, :use_s3)
51 parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
52 data = Base.decode64!(parsed["data"], ignore: :whitespace)
53 uuid = UUID.generate()
54 uuidpath = Path.join(upload_path(), uuid)
55 uuid = UUID.generate()
57 File.mkdir_p!(upload_path())
59 File.write!(uuidpath, data)
61 content_type = get_content_type(uuidpath)
65 String.downcase(Base.encode16(:crypto.hash(:sha256, data))),
70 upload_folder = get_upload_path(uuid, should_dedupe)
71 url_path = get_url(name, uuid, should_dedupe)
73 File.mkdir_p!(upload_folder)
74 result_file = Path.join(upload_folder, name)
77 if !File.exists?(result_file) do
78 File.rename(uuidpath, result_file)
83 File.rename(uuidpath, result_file)
86 strip_exif_data(content_type, result_file)
90 put_s3_file(name, uuid, result_file, content_type)
100 "mediaType" => content_type,
108 def strip_exif_data(content_type, file) do
109 settings = Application.get_env(:pleroma, Pleroma.Upload)
110 do_strip = Keyword.fetch!(settings, :strip_exif)
111 [filetype, ext] = String.split(content_type, "/")
113 if filetype == "image" and do_strip == true do
114 Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true)
119 settings = Application.get_env(:pleroma, Pleroma.Upload)
120 Keyword.fetch!(settings, :uploads)
123 defp create_name(uuid, ext, type) do
125 "application/octet-stream" ->
126 String.downcase(Enum.join([uuid, ext], "."))
129 String.downcase(Enum.join([uuid, "mp3"], "."))
132 String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
136 defp get_uuid(file, should_dedupe) do
138 Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
144 defp get_name(file, uuid, type, should_dedupe) do
146 create_name(uuid, List.last(String.split(file.filename, ".")), type)
148 parts = String.split(file.filename, ".")
151 if length(parts) > 1 do
152 Enum.drop(parts, -1) |> Enum.join(".")
158 "application/octet-stream" -> file.filename
159 "audio/mpeg" -> new_filename <> ".mp3"
160 "image/jpeg" -> new_filename <> ".jpg"
161 _ -> Enum.join([new_filename, String.split(type, "/") |> List.last()], ".")
166 defp get_upload_path(uuid, should_dedupe) do
170 Path.join(upload_path(), uuid)
174 defp get_url(name, uuid, should_dedupe) do
176 url_for(:cow_uri.urlencode(name))
178 url_for(Path.join(uuid, :cow_uri.urlencode(name)))
182 defp url_for(file) do
183 "#{Web.base_url()}/media/#{file}"
186 def get_content_type(file) do
188 File.open(file, [:read], fn f ->
189 case IO.binread(f, 8) do
190 <<0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A>> ->
193 <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> ->
196 <<0xFF, 0xD8, 0xFF, _, _, _, _, _>> ->
199 <<0x1A, 0x45, 0xDF, 0xA3, _, _, _, _>> ->
202 <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> ->
205 <<0x49, 0x44, 0x33, _, _, _, _, _>> ->
208 <<255, 251, _, 68, 0, 0, 0, 0>> ->
211 <<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
214 <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> ->
218 "application/octet-stream"
224 _e -> "application/octet-stream"
228 defp put_s3_file(name, uuid, path, content_type) do
229 settings = Application.get_env(:pleroma, Pleroma.Upload)
230 bucket = Keyword.fetch!(settings, :bucket)
231 public_endpoint = Keyword.fetch!(settings, :public_endpoint)
233 {:ok, file_data} = File.read(path)
237 s3_name = "#{uuid}/#{name}"
240 ExAws.S3.put_object(bucket, s3_name, file_data, [
241 {:acl, :public_read},
242 {:content_type, content_type}
246 "#{public_endpoint}/#{bucket}/#{s3_name}"