Support data uris in uploads.
This commit is contained in:
parent
b179b3413e
commit
7617a593b9
@ -18,6 +18,31 @@ defmodule Pleroma.Upload do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def store(%{"img" => "data:image/" <> image_data}) do
|
||||||
|
parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
|
||||||
|
data = Base.decode64!(parsed["data"])
|
||||||
|
uuid = Ecto.UUID.generate
|
||||||
|
upload_folder = Path.join(upload_path(), uuid)
|
||||||
|
File.mkdir_p!(upload_folder)
|
||||||
|
filename = Base.encode16(:crypto.hash(:sha256, data)) <> ".#{parsed["filetype"]}"
|
||||||
|
result_file = Path.join(upload_folder, filename)
|
||||||
|
|
||||||
|
File.write!(result_file, data)
|
||||||
|
|
||||||
|
content_type = "image/#{parsed["filetype"]}"
|
||||||
|
|
||||||
|
%{
|
||||||
|
"type" => "Image",
|
||||||
|
"url" => [%{
|
||||||
|
"type" => "Link",
|
||||||
|
"mediaType" => content_type,
|
||||||
|
"href" => url_for(Path.join(uuid, filename))
|
||||||
|
}],
|
||||||
|
"name" => filename,
|
||||||
|
"uuid" => uuid
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
defp upload_path do
|
defp upload_path do
|
||||||
Application.get_env(:pleroma, Pleroma.Upload)
|
Application.get_env(:pleroma, Pleroma.Upload)
|
||||||
|> Keyword.fetch!(:uploads)
|
|> Keyword.fetch!(:uploads)
|
||||||
|
@ -167,7 +167,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
|||||||
Repo.all(query)
|
Repo.all(query)
|
||||||
end
|
end
|
||||||
|
|
||||||
def upload(%Plug.Upload{} = file) do
|
def upload(file) do
|
||||||
data = Upload.store(file)
|
data = Upload.store(file)
|
||||||
Repo.insert(%Object{data: data})
|
Repo.insert(%Object{data: data})
|
||||||
end
|
end
|
||||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user