Skip to content
Snippets Groups Projects
Commit e7dc39e4 authored by lain's avatar lain
Browse files

Basic file uploading via TwAPI.

parent 08fdbd6f
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,7 @@ config :pleroma, Pleroma.Upload, ...@@ -15,6 +15,7 @@ config :pleroma, Pleroma.Upload,
# Configures the endpoint # Configures the endpoint
config :pleroma, Pleroma.Web.Endpoint, config :pleroma, Pleroma.Web.Endpoint,
url: [host: "localhost"], url: [host: "localhost"],
protocol: "https",
secret_key_base: "aK4Abxf29xU9TTDKre9coZPUgevcVCFQJe/5xP/7Lt4BEif6idBIbjupVbOrbKxl", secret_key_base: "aK4Abxf29xU9TTDKre9coZPUgevcVCFQJe/5xP/7Lt4BEif6idBIbjupVbOrbKxl",
render_errors: [view: Pleroma.Web.ErrorView, accepts: ~w(json)], render_errors: [view: Pleroma.Web.ErrorView, accepts: ~w(json)],
pubsub: [name: Pleroma.PubSub, pubsub: [name: Pleroma.PubSub,
......
...@@ -8,6 +8,7 @@ use Mix.Config ...@@ -8,6 +8,7 @@ use Mix.Config
# with brunch.io to recompile .js and .css sources. # with brunch.io to recompile .js and .css sources.
config :pleroma, Pleroma.Web.Endpoint, config :pleroma, Pleroma.Web.Endpoint,
http: [port: 4000], http: [port: 4000],
protocol: "http",
debug_errors: true, debug_errors: true,
code_reloader: true, code_reloader: true,
check_origin: false, check_origin: false,
......
...@@ -25,6 +25,8 @@ defmodule Pleroma.Upload do ...@@ -25,6 +25,8 @@ defmodule Pleroma.Upload do
|> Keyword.fetch!(:url) |> Keyword.fetch!(:url)
|> Keyword.fetch!(:host) |> Keyword.fetch!(:host)
"https://#{host}/media/#{file}" protocol = Application.get_env(:pleroma, Pleroma.Web.Endpoint) |> Keyword.fetch!(:protocol)
"#{protocol}://#{host}/media/#{file}"
end end
end end
defmodule Pleroma.Web.ActivityPub.ActivityPub do defmodule Pleroma.Web.ActivityPub.ActivityPub do
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.Activity alias Pleroma.{Activity, Object, Upload}
import Ecto.Query import Ecto.Query
def insert(map) when is_map(map) do def insert(map) when is_map(map) do
...@@ -33,7 +33,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do ...@@ -33,7 +33,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
Application.get_env(:pleroma, Pleroma.Web.Endpoint) Application.get_env(:pleroma, Pleroma.Web.Endpoint)
|> Keyword.fetch!(:url) |> Keyword.fetch!(:url)
|> Keyword.fetch!(:host) |> 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 end
def fetch_public_activities(opts \\ %{}) do def fetch_public_activities(opts \\ %{}) do
...@@ -66,4 +68,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do ...@@ -66,4 +68,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
where: fragment("? @> ?", activity.data, ^%{ context: context }) where: fragment("? @> ?", activity.data, ^%{ context: context })
Repo.all(query) Repo.all(query)
end end
def upload(%Plug.Upload{} = file) do
data = Upload.store(file)
Repo.insert(%Object{data: data})
end
end end
...@@ -8,8 +8,7 @@ defmodule Pleroma.Web.Endpoint do ...@@ -8,8 +8,7 @@ defmodule Pleroma.Web.Endpoint do
# You should set gzip to true if you are running phoenix.digest # You should set gzip to true if you are running phoenix.digest
# when deploying your static files in production. # when deploying your static files in production.
plug Plug.Static, plug Plug.Static,
at: "/", from: :pleroma, gzip: false, at: "/media", from: "uploads", gzip: false
only: ~w(css fonts images js favicon.ico robots.txt)
# Code reloading can be explicitly enabled under the # Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint. # :code_reloader configuration of your endpoint.
......
...@@ -35,5 +35,6 @@ defmodule Pleroma.Web.Router do ...@@ -35,5 +35,6 @@ defmodule Pleroma.Web.Router do
get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline
post "/friendships/create", TwitterAPI.Controller, :follow post "/friendships/create", TwitterAPI.Controller, :follow
post "/friendships/destroy", TwitterAPI.Controller, :unfollow post "/friendships/destroy", TwitterAPI.Controller, :unfollow
post "/statusnet/media/upload", TwitterAPI.Controller, :upload
end end
end end
...@@ -96,6 +96,23 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do ...@@ -96,6 +96,23 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end end
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 defp add_conversation_id(activity) do
if is_integer(activity.data["statusnetConversationId"]) do if is_integer(activity.data["statusnetConversationId"]) do
{:ok, activity} {:ok, activity}
......
...@@ -65,6 +65,12 @@ defmodule Pleroma.Web.TwitterAPI.Controller do ...@@ -65,6 +65,12 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|> json_reply(200, response) |> json_reply(200, response)
end 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 defp json_reply(conn, status, json) do
conn conn
......
defmodule Pleroma.Web.ActivityPub.ActivityPubTest do defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Activity alias Pleroma.{Activity, Object}
alias Pleroma.Builders.ActivityBuilder alias Pleroma.Builders.ActivityBuilder
describe "insertion" do describe "insertion" do
...@@ -94,4 +94,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do ...@@ -94,4 +94,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
assert last == last_expected assert last == last_expected
end end
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 end
...@@ -116,4 +116,12 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do ...@@ -116,4 +116,12 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
assert Enum.at(statuses, 0)["id"] == activity.id assert Enum.at(statuses, 0)["id"] == activity.id
assert Enum.at(statuses, 1)["id"] == activity_two.id assert Enum.at(statuses, 1)["id"] == activity_two.id
end 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 end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment