A hobbldey-working swift client.
authorThurloat <thurloat@gmail.com>
Wed, 29 Aug 2018 01:32:24 +0000 (22:32 -0300)
committerThurloat <thurloat@gmail.com>
Wed, 29 Aug 2018 01:32:24 +0000 (22:32 -0300)
apparently, all elixir openstack libraries are trash
luckily, the APIs are stupid easy.

lib/pleroma/upload.ex
lib/pleroma/uploaders/swift.ex [deleted file]
lib/pleroma/uploaders/swift/keystone.ex [new file with mode: 0644]
lib/pleroma/uploaders/swift/swift.ex [new file with mode: 0644]
lib/pleroma/uploaders/swift/uploader.ex [new file with mode: 0644]

index b70758dc795e0bb77cc079532f0c52343dedc045..7d3b362878835d4d87ce967c8a1b2b773cb90a02 100644 (file)
@@ -12,7 +12,7 @@ defmodule Pleroma.Upload do
 
     strip_exif_data(content_type, file.path)
 
-    url_path = @storage_backend.put_file(name, uuid, file, content_type, should_dedupe)
+    url_path = @storage_backend.put_file(name, uuid, file.path, content_type, should_dedupe)
 
     %{
       "type" => "Document",
diff --git a/lib/pleroma/uploaders/swift.ex b/lib/pleroma/uploaders/swift.ex
deleted file mode 100644 (file)
index 8b13789..0000000
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/lib/pleroma/uploaders/swift/keystone.ex b/lib/pleroma/uploaders/swift/keystone.ex
new file mode 100644 (file)
index 0000000..a792143
--- /dev/null
@@ -0,0 +1,48 @@
+defmodule Pleroma.Uploaders.Swift.Keystone do
+  use HTTPoison.Base
+
+  @settings Application.get_env(:pleroma, Pleroma.Uploaders.Swift)
+
+  def process_url(url) do
+    Enum.join(
+      [Keyword.fetch!(@settings, :auth_url), url],
+      "/"
+    )
+  end
+
+  def process_response_body(body) do
+    body
+    |> Poison.decode!()
+  end
+
+  def get_token() do
+    username = Keyword.fetch!(@settings, :username)
+    password = Keyword.fetch!(@settings, :password)
+    tenant_id = Keyword.fetch!(@settings, :tenant_id)
+
+    case post(
+           "/tokens",
+           make_auth_body(username, password, tenant_id),
+           ["Content-Type": "application/json"],
+           hackney: [:insecure]
+         ) do
+      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
+        body["access"]["token"]["id"]
+
+      {:ok, %HTTPoison.Response{status_code: _}} ->
+        ""
+    end
+  end
+
+  def make_auth_body(username, password, tenant) do
+    Poison.encode!(%{
+      :auth => %{
+        :passwordCredentials => %{
+          :username => username,
+          :password => password
+        },
+        :tenantId => tenant
+      }
+    })
+  end
+end
diff --git a/lib/pleroma/uploaders/swift/swift.ex b/lib/pleroma/uploaders/swift/swift.ex
new file mode 100644 (file)
index 0000000..4f45255
--- /dev/null
@@ -0,0 +1,30 @@
+defmodule Pleroma.Uploaders.Swift.Client do
+  use HTTPoison.Base
+
+  @settings Application.get_env(:pleroma, Pleroma.Uploaders.Swift)
+
+  def process_url(url) do
+    Enum.join(
+      [Keyword.fetch!(@settings, :storage_url), url],
+      "/"
+    )
+  end
+
+  def upload_file(filename, body, content_type) do
+    token = Pleroma.Uploaders.Swift.Keystone.get_token()
+
+    case put("#{filename}", body, "X-Auth-Token": token, "Content-Type": content_type) do
+      {:ok, %HTTPoison.Response{status_code: 201}} ->
+        # lgtm
+        ""
+
+      {:ok, %HTTPoison.Response{status_code: 401}} ->
+        # bad token
+        ""
+
+      {:error, _} ->
+        # bad news
+        ""
+    end
+  end
+end
diff --git a/lib/pleroma/uploaders/swift/uploader.ex b/lib/pleroma/uploaders/swift/uploader.ex
new file mode 100644 (file)
index 0000000..c71808c
--- /dev/null
@@ -0,0 +1,15 @@
+defmodule Pleroma.Uploaders.Swift do
+  @behaviour Pleroma.Uploaders.Uploader
+
+  @settings Application.get_env(:pleroma, Pleroma.Uploaders.Swift)
+
+  def put_file(name, uuid, tmp_path, content_type, _should_dedupe) do
+    {:ok, file_data} = File.read(tmp_path)
+    remote_name = "#{uuid}/#{name}"
+
+    Pleroma.Uploaders.Swift.Client.upload_file(remote_name, file_data, content_type)
+
+    object_url = Keyword.fetch!(@settings, :object_url)
+    "#{object_url}/#{remote_name}"
+  end
+end