Support data uris in uploads.

This commit is contained in:
Roger Braun 2017-04-16 14:23:30 +02:00
parent b179b3413e
commit 7617a593b9
3 changed files with 36 additions and 1 deletions

View File

@ -18,6 +18,31 @@ def store(%Plug.Upload{} = file) do
}
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
Application.get_env(:pleroma, Pleroma.Upload)
|> Keyword.fetch!(:uploads)

View File

@ -167,7 +167,7 @@ def fetch_activities_for_context(context) do
Repo.all(query)
end
def upload(%Plug.Upload{} = file) do
def upload(file) do
data = Upload.store(file)
Repo.insert(%Object{data: data})
end

File diff suppressed because one or more lines are too long