diff options
author | Roger Braun <roger@rogerbraun.net> | 2017-04-16 14:23:30 +0200 |
---|---|---|
committer | Roger Braun <roger@rogerbraun.net> | 2017-04-16 14:23:30 +0200 |
commit | 7617a593b9acef6d87ed020714d4ea523da201b0 (patch) | |
tree | 3b57a84ad62dcbfaf24334d9385b3742ed5f684c /lib | |
parent | b179b3413e7dbdafbe8a3839ba7719b9eb2a582d (diff) | |
download | pleroma-7617a593b9acef6d87ed020714d4ea523da201b0.tar.gz |
Support data uris in uploads.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/upload.ex | 25 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 2 |
2 files changed, 26 insertions, 1 deletions
diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index d22421d37..3aabf8157 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -18,6 +18,31 @@ defmodule Pleroma.Upload 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) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 0d3360ee1..125473b96 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -167,7 +167,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub 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 |