aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/upload.ex
diff options
context:
space:
mode:
authordtluna <dtluna@openmailbox.org>2017-04-16 17:10:25 +0300
committerdtluna <dtluna@openmailbox.org>2017-04-16 17:10:25 +0300
commit85bd480be333896ca9cc0ade0e68ea99e10aaaa7 (patch)
treeaa545ad989ae70367aff25e1dfe970f28e88e55a /lib/pleroma/upload.ex
parentce1eef9c989f0387168b48f6f61a4c1f84b3f5b5 (diff)
parente158e32124a62f2c93a8910bf3edc4519c4a13e6 (diff)
downloadpleroma-85bd480be333896ca9cc0ade0e68ea99e10aaaa7.tar.gz
Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/help-test
Diffstat (limited to 'lib/pleroma/upload.ex')
-rw-r--r--lib/pleroma/upload.ex25
1 files changed, 25 insertions, 0 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)