From e7dc39e40cde5599f2e1e0dd1715670fd1e76720 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 29 Mar 2017 02:05:51 +0200 Subject: [PATCH] Basic file uploading via TwAPI. --- config/config.exs | 1 + config/dev.exs | 1 + lib/pleroma/upload.ex | 4 +++- lib/pleroma/web/activity_pub/activity_pub.ex | 11 +++++++++-- lib/pleroma/web/endpoint.ex | 3 +-- lib/pleroma/web/router.ex | 1 + lib/pleroma/web/twitter_api/twitter_api.ex | 17 +++++++++++++++++ .../web/twitter_api/twitter_api_controller.ex | 6 ++++++ test/web/activity_pub/activity_pub_test.exs | 11 ++++++++++- test/web/twitter_api/twitter_api_test.exs | 8 ++++++++ 10 files changed, 57 insertions(+), 6 deletions(-) diff --git a/config/config.exs b/config/config.exs index bf050197f..2b041b10f 100644 --- a/config/config.exs +++ b/config/config.exs @@ -15,6 +15,7 @@ config :pleroma, Pleroma.Upload, # Configures the endpoint config :pleroma, Pleroma.Web.Endpoint, url: [host: "localhost"], + protocol: "https", secret_key_base: "aK4Abxf29xU9TTDKre9coZPUgevcVCFQJe/5xP/7Lt4BEif6idBIbjupVbOrbKxl", render_errors: [view: Pleroma.Web.ErrorView, accepts: ~w(json)], pubsub: [name: Pleroma.PubSub, diff --git a/config/dev.exs b/config/dev.exs index 78f543720..6adde84a2 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -8,6 +8,7 @@ use Mix.Config # with brunch.io to recompile .js and .css sources. config :pleroma, Pleroma.Web.Endpoint, http: [port: 4000], + protocol: "http", debug_errors: true, code_reloader: true, check_origin: false, diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index 43ebe98e3..fdda2634e 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -25,6 +25,8 @@ defmodule Pleroma.Upload do |> Keyword.fetch!(:url) |> Keyword.fetch!(:host) - "https://#{host}/media/#{file}" + protocol = Application.get_env(:pleroma, Pleroma.Web.Endpoint) |> Keyword.fetch!(:protocol) + + "#{protocol}://#{host}/media/#{file}" end end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 043e33042..f7ba1bb37 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1,6 +1,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do alias Pleroma.Repo - alias Pleroma.Activity + alias Pleroma.{Activity, Object, Upload} import Ecto.Query def insert(map) when is_map(map) do @@ -33,7 +33,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do Application.get_env(:pleroma, Pleroma.Web.Endpoint) |> Keyword.fetch!(:url) |> Keyword.fetch!(:host) - "https://#{host}/#{type}/#{Ecto.UUID.generate}" + + protocol = Application.get_env(:pleroma, Pleroma.Web.Endpoint) |> Keyword.fetch!(:protocol) + "#{protocol}://#{host}/#{type}/#{Ecto.UUID.generate}" end def fetch_public_activities(opts \\ %{}) do @@ -66,4 +68,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do where: fragment("? @> ?", activity.data, ^%{ context: context }) Repo.all(query) end + + def upload(%Plug.Upload{} = file) do + data = Upload.store(file) + Repo.insert(%Object{data: data}) + end end diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index e35a94c84..6af42a685 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -8,8 +8,7 @@ defmodule Pleroma.Web.Endpoint do # You should set gzip to true if you are running phoenix.digest # when deploying your static files in production. plug Plug.Static, - at: "/", from: :pleroma, gzip: false, - only: ~w(css fonts images js favicon.ico robots.txt) + at: "/media", from: "uploads", gzip: false # Code reloading can be explicitly enabled under the # :code_reloader configuration of your endpoint. diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index c60107072..52030d684 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -35,5 +35,6 @@ defmodule Pleroma.Web.Router do get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline post "/friendships/create", TwitterAPI.Controller, :follow post "/friendships/destroy", TwitterAPI.Controller, :unfollow + post "/statusnet/media/upload", TwitterAPI.Controller, :upload end end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index e240dc2ea..b6e7d5cc6 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -96,6 +96,23 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end end + def upload(%Plug.Upload{} = file) do + {:ok, object} = ActivityPub.upload(file) + + # Fake this as good as possible... + """ + + + #{object.id} + #{object.id} + #{object.id} + #{object.data["href"]} + #{object.data["href"]} + + + """ + end + defp add_conversation_id(activity) do if is_integer(activity.data["statusnetConversationId"]) do {:ok, activity} diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index f42cee0b0..a26885bf1 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -65,6 +65,12 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, response) end + def upload(conn, %{"media" => media}) do + response = TwitterAPI.upload(media) + conn + |> put_resp_content_type("application/atom+xml") + |> send_resp(200, response) + end defp json_reply(conn, status, json) do conn diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index b773c323e..0e778d887 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -1,7 +1,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do use Pleroma.DataCase alias Pleroma.Web.ActivityPub.ActivityPub - alias Pleroma.Activity + alias Pleroma.{Activity, Object} alias Pleroma.Builders.ActivityBuilder describe "insertion" do @@ -94,4 +94,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert last == last_expected end end + + describe "uploading files" do + test "copies the file to the configured folder" do + file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"} + + {:ok, %Object{} = object} = ActivityPub.upload(file) + assert object.data["name"] == "an_image.jpg" + end + end end diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 5325110e1..dcffab2b8 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -116,4 +116,12 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert Enum.at(statuses, 0)["id"] == activity.id assert Enum.at(statuses, 1)["id"] == activity_two.id end + + test "upload a file" do + file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"} + + response = TwitterAPI.upload(file) + + assert is_binary(response) + end end