diff options
author | dtluna <dtluna@openmailbox.org> | 2017-04-16 17:10:25 +0300 |
---|---|---|
committer | dtluna <dtluna@openmailbox.org> | 2017-04-16 17:10:25 +0300 |
commit | 85bd480be333896ca9cc0ade0e68ea99e10aaaa7 (patch) | |
tree | aa545ad989ae70367aff25e1dfe970f28e88e55a /lib | |
parent | ce1eef9c989f0387168b48f6f61a4c1f84b3f5b5 (diff) | |
parent | e158e32124a62f2c93a8910bf3edc4519c4a13e6 (diff) | |
download | pleroma-85bd480be333896ca9cc0ade0e68ea99e10aaaa7.tar.gz |
Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/help-test
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/upload.ex | 25 | ||||
-rw-r--r-- | lib/pleroma/user.ex | 1 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 1 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/representers/user_representer.ex | 6 | ||||
-rw-r--r-- | lib/pleroma/web/twitter_api/twitter_api_controller.ex | 13 |
6 files changed, 45 insertions, 3 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/user.ex b/lib/pleroma/user.ex index ed85447fe..fdcc1b7d5 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -13,6 +13,7 @@ defmodule Pleroma.User do field :password_confirmation, :string, virtual: true field :following, { :array, :string }, default: [] field :ap_id, :string + field :avatar, :map timestamps() end 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 diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 30e8e12cb..0446f622b 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -47,5 +47,6 @@ defmodule Pleroma.Web.Router do post "/favorites/create", TwitterAPI.Controller, :favorite post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite post "/statuses/retweet/:id", TwitterAPI.Controller, :retweet + post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar end end diff --git a/lib/pleroma/web/twitter_api/representers/user_representer.ex b/lib/pleroma/web/twitter_api/representers/user_representer.ex index d8f98488e..2ee4ee254 100644 --- a/lib/pleroma/web/twitter_api/representers/user_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/user_representer.ex @@ -4,8 +4,10 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do alias Pleroma.User def to_map(user, opts) do - - image = "https://placehold.it/48x48" + image = case user.avatar do + %{"url" => [%{"href" => href} | _]} -> href + _ -> "https://placehold.it/48x48" + end following = if opts[:for] do User.following?(opts[:for], user) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index f2ca83915..6014aedc4 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -3,6 +3,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} alias Pleroma.{Repo, Activity} + alias Pleroma.Web.ActivityPub.ActivityPub def verify_credentials(%{assigns: %{user: user}} = conn, _params) do response = user |> UserRepresenter.to_json(%{for: user}) @@ -146,6 +147,18 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end + def update_avatar(%{assigns: %{user: user}} = conn, params) do + {:ok, object} = ActivityPub.upload(params) + change = Ecto.Changeset.change(user, %{avatar: object.data}) + {:ok, user} = Repo.update(change) + + response = UserRepresenter.to_map(user, %{for: user}) + |> Poison.encode! + + conn + |> json_reply(200, response) + end + defp json_reply(conn, status, json) do conn |> put_resp_content_type("application/json") |