alias Pleroma.Web
def store(%Plug.Upload{} = file, should_dedupe) do
+ settings = Application.get_env(:pleroma, Pleroma.Upload)
+ use_s3 = Keyword.fetch!(settings, :use_s3)
+
content_type = get_content_type(file.path)
uuid = get_uuid(file, should_dedupe)
name = get_name(file, uuid, content_type, should_dedupe)
upload_folder = get_upload_path(uuid, should_dedupe)
url_path = get_url(name, uuid, should_dedupe)
+ strip_exif_data(content_type, file.path)
+
File.mkdir_p!(upload_folder)
result_file = Path.join(upload_folder, name)
File.cp!(file.path, result_file)
end
+ url_path =
+ if use_s3 do
+ put_s3_file(name, uuid, result_file, content_type)
+ else
+ url_path
+ end
+
%{
- "type" => "Image",
+ "type" => "Document",
"url" => [
%{
"type" => "Link",
}
end
+ # XXX: does this code actually work? i am skeptical. --kaniini
def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
+ settings = Application.get_env(:pleroma, Pleroma.Upload)
+ use_s3 = Keyword.fetch!(settings, :use_s3)
+
parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
data = Base.decode64!(parsed["data"], ignore: :whitespace)
uuid = UUID.generate()
File.rename(uuidpath, result_file)
end
+ strip_exif_data(content_type, result_file)
+
+ url_path =
+ if use_s3 do
+ put_s3_file(name, uuid, result_file, content_type)
+ else
+ url_path
+ end
+
%{
"type" => "Image",
"url" => [
}
end
+ def strip_exif_data(content_type, file) do
+ settings = Application.get_env(:pleroma, Pleroma.Upload)
+ do_strip = Keyword.fetch!(settings, :strip_exif)
+ [filetype, ext] = String.split(content_type, "/")
+
+ if filetype == "image" and do_strip == true do
+ Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true)
+ end
+ end
+
def upload_path do
settings = Application.get_env(:pleroma, Pleroma.Upload)
Keyword.fetch!(settings, :uploads)
end
defp create_name(uuid, ext, type) do
- if type == "application/octet-stream" do
- String.downcase(Enum.join([uuid, ext], "."))
- else
- String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
+ case type do
+ "application/octet-stream" ->
+ String.downcase(Enum.join([uuid, ext], "."))
+
+ "audio/mpeg" ->
+ String.downcase(Enum.join([uuid, "mp3"], "."))
+
+ _ ->
+ String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
end
end
if should_dedupe do
create_name(uuid, List.last(String.split(file.filename, ".")), type)
else
- file.filename
+ parts = String.split(file.filename, ".")
+
+ new_filename =
+ if length(parts) > 1 do
+ Enum.drop(parts, -1) |> Enum.join(".")
+ else
+ Enum.join(parts)
+ end
+
+ case type do
+ "application/octet-stream" -> file.filename
+ "audio/mpeg" -> new_filename <> ".mp3"
+ "image/jpeg" -> new_filename <> ".jpg"
+ _ -> Enum.join([new_filename, String.split(type, "/") |> List.last()], ".")
+ end
end
end
<<0x49, 0x44, 0x33, _, _, _, _, _>> ->
"audio/mpeg"
+ <<255, 251, _, 68, 0, 0, 0, 0>> ->
+ "audio/mpeg"
+
<<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
"audio/ogg"
_e -> "application/octet-stream"
end
end
+
+ defp put_s3_file(name, uuid, path, content_type) do
+ settings = Application.get_env(:pleroma, Pleroma.Upload)
+ bucket = Keyword.fetch!(settings, :bucket)
+ public_endpoint = Keyword.fetch!(settings, :public_endpoint)
+
+ {:ok, file_data} = File.read(path)
+
+ File.rm!(path)
+
+ s3_name = "#{uuid}/#{name}"
+
+ {:ok, result} =
+ ExAws.S3.put_object(bucket, s3_name, file_data, [
+ {:acl, :public_read},
+ {:content_type, content_type}
+ ])
+ |> ExAws.request()
+
+ "#{public_endpoint}/#{bucket}/#{s3_name}"
+ end
end