Basic file uploading via TwAPI.

This commit is contained in:
Roger Braun 2017-03-29 02:05:51 +02:00
parent 08fdbd6f3a
commit e7dc39e40c
10 changed files with 57 additions and 6 deletions

View File

@ -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,

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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...
"""
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
<mediaid>#{object.id}</mediaid>
<media_id>#{object.id}</media_id>
<media_id_string>#{object.id}</media_id_string>
<media_url>#{object.data["href"]}</media_url>
<mediaurl>#{object.data["href"]}</mediaurl>
<atom:link rel="enclosure" href="#{object.data["href"]}" type="image"></atom:link>
</rsp>
"""
end
defp add_conversation_id(activity) do
if is_integer(activity.data["statusnetConversationId"]) do
{:ok, activity}

View File

@ -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

View File

@ -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

View File

@ -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