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 @@
# 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,
......
......@@ -8,6 +8,7 @@
# 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,
......
......@@ -25,6 +25,8 @@ defp url_for(file) 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
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 @@ def generate_id(type) 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 @@ def fetch_activities_for_context(context) 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
......@@ -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.
......
......@@ -35,5 +35,6 @@ def user_fetcher(username) 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
......@@ -96,6 +96,23 @@ def unfollow(%User{} = follower, followed_id) 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}
......
......@@ -65,6 +65,12 @@ def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) 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
......
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 @@ test "retrieves ids starting from a since_id" 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
......@@ -116,4 +116,12 @@ test "fetch statuses in a context using the conversation id" 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
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