From 709816a0f891d6c26c43b54577a3b727c1fe4af6 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Mon, 27 Aug 2018 22:20:54 -0300 Subject: [PATCH 01/10] example of flexible storage backends --- config/config.exs | 10 ++++-- lib/pleroma/upload.ex | 66 ++-------------------------------- lib/pleroma/uploaders/local.ex | 44 +++++++++++++++++++++++ lib/pleroma/uploaders/s3.ex | 24 +++++++++++++ lib/pleroma/uploaders/swift.ex | 0 5 files changed, 77 insertions(+), 67 deletions(-) create mode 100644 lib/pleroma/uploaders/local.ex create mode 100644 lib/pleroma/uploaders/s3.ex create mode 100644 lib/pleroma/uploaders/swift.ex diff --git a/config/config.exs b/config/config.exs index d234d23eb..4eb59994c 100644 --- a/config/config.exs +++ b/config/config.exs @@ -11,9 +11,13 @@ config :pleroma, ecto_repos: [Pleroma.Repo] config :pleroma, Pleroma.Repo, types: Pleroma.PostgresTypes config :pleroma, Pleroma.Upload, - uploads: "uploads", - strip_exif: false, - use_s3: false, + uploader: Pleroma.Uploaders.Local + strip_exif: false + +config :pleroma, Pleroma.Uploaders.Local, + uploads: "uploads" + +config :pleroma, Pleroma.Uploaders.S3, s3_bucket: nil config :pleroma, :emoji, shortcode_globs: ["/emoji/custom/**/*.png"] diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index a744e6fd4..d7cc8122a 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -4,31 +4,15 @@ defmodule Pleroma.Upload do def store(%Plug.Upload{} = file, should_dedupe) do settings = Application.get_env(:pleroma, Pleroma.Upload) - use_s3 = Keyword.fetch!(settings, :use_s3) + storage_backend = Keyword.fetch!(settings, :storage_backend) content_type = get_content_type(file.path) uuid = get_uuid(file, should_dedupe) name = get_name(file, uuid, content_type, should_dedupe) - upload_folder = get_upload_path(uuid, should_dedupe) - url_path = get_url(name, uuid, should_dedupe) strip_exif_data(content_type, file.path) - File.mkdir_p!(upload_folder) - result_file = Path.join(upload_folder, name) - - if File.exists?(result_file) do - File.rm!(file.path) - else - File.cp!(file.path, result_file) - end - - url_path = - if use_s3 do - put_s3_file(name, uuid, result_file, content_type) - else - url_path - end + url_path = storage_backend.put_file(name, uuid, content_type) %{ "type" => "Document", @@ -115,11 +99,6 @@ defmodule Pleroma.Upload do end end - def upload_path do - settings = Application.get_env(:pleroma, Pleroma.Upload) - Keyword.fetch!(settings, :uploads) - end - defp create_name(uuid, ext, type) do case type do "application/octet-stream" -> @@ -163,26 +142,6 @@ defmodule Pleroma.Upload do end end - defp get_upload_path(uuid, should_dedupe) do - if should_dedupe do - upload_path() - else - Path.join(upload_path(), uuid) - end - end - - defp get_url(name, uuid, should_dedupe) do - if should_dedupe do - url_for(:cow_uri.urlencode(name)) - else - url_for(Path.join(uuid, :cow_uri.urlencode(name))) - end - end - - defp url_for(file) do - "#{Web.base_url()}/media/#{file}" - end - def get_content_type(file) do match = File.open(file, [:read], fn f -> @@ -224,25 +183,4 @@ defmodule Pleroma.Upload do _e -> "application/octet-stream" end end - - defp put_s3_file(name, uuid, path, content_type) do - settings = Application.get_env(:pleroma, Pleroma.Upload) - bucket = Keyword.fetch!(settings, :bucket) - public_endpoint = Keyword.fetch!(settings, :public_endpoint) - - {:ok, file_data} = File.read(path) - - File.rm!(path) - - s3_name = "#{uuid}/#{name}" - - {:ok, result} = - ExAws.S3.put_object(bucket, s3_name, file_data, [ - {:acl, :public_read}, - {:content_type, content_type} - ]) - |> ExAws.request() - - "#{public_endpoint}/#{bucket}/#{s3_name}" - end end diff --git a/lib/pleroma/uploaders/local.ex b/lib/pleroma/uploaders/local.ex new file mode 100644 index 000000000..7e5d7a59b --- /dev/null +++ b/lib/pleroma/uploaders/local.ex @@ -0,0 +1,44 @@ +defmodule Pleroma.Uploaders.Local do + def put_file(name, uuid, file, content_type) do + + upload_path = get_upload_path(uuid, should_dedupe) + url_path = get_url(name, uuid, should_dedupe) + + File.mkdir_p!(upload_folder) + + result_file = Path.join(upload_folder, name) + + if File.exists?(result_file) do + File.rm!(file.path) + else + File.cp!(file.path, result_file) + end + + url_path + end + + def upload_path do + settings = Application.get_env(:pleroma, Pleroma.Uploaders.Local) + Keyword.fetch!(settings, :uploads) + end + + defp get_upload_path(uuid, should_dedupe) do + if should_dedupe do + upload_path() + else + Path.join(upload_path(), uuid) + end + end + + defp get_url(name, uuid, should_dedupe) do + if should_dedupe do + url_for(:cow_uri.urlencode(name)) + else + url_for(Path.join(uuid, :cow_uri.urlencode(name))) + end + end + + defp url_for(file) do + "#{Web.base_url()}/media/#{file}" + end +end diff --git a/lib/pleroma/uploaders/s3.ex b/lib/pleroma/uploaders/s3.ex new file mode 100644 index 000000000..95f20be67 --- /dev/null +++ b/lib/pleroma/uploaders/s3.ex @@ -0,0 +1,24 @@ +defmodule Pleroma.Uploaders.S3 do + + def put_file(name, uuid, path, content_type) do + + settings = Application.get_env(:pleroma, Pleroma.Uploaders.S3) + bucket = Keyword.fetch!(settings, :bucket) + public_endpoint = Keyword.fetch!(settings, :public_endpoint) + + {:ok, file_data} = File.read(path) + + File.rm!(path) + + s3_name = "#{uuid}/#{name}" + + {:ok, result} = + ExAws.S3.put_object(bucket, s3_name, file_data, [ + {:acl, :public_read}, + {:content_type, content_type} + ]) + |> ExAws.request() + + "#{public_endpoint}/#{bucket}/#{s3_name}" + end +end diff --git a/lib/pleroma/uploaders/swift.ex b/lib/pleroma/uploaders/swift.ex new file mode 100644 index 000000000..e69de29bb From 0df558a6a5f5a5f64de57c91074981429da08764 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Mon, 27 Aug 2018 22:45:53 -0300 Subject: [PATCH 02/10] cleaning up a bit. --- config/config.exs | 2 +- lib/pleroma/upload.ex | 6 +++--- lib/pleroma/uploaders/local.ex | 7 +++++-- lib/pleroma/uploaders/s3.ex | 4 ++-- lib/pleroma/web/endpoint.ex | 2 +- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/config/config.exs b/config/config.exs index 4eb59994c..fe4bfe8a0 100644 --- a/config/config.exs +++ b/config/config.exs @@ -11,7 +11,7 @@ config :pleroma, ecto_repos: [Pleroma.Repo] config :pleroma, Pleroma.Repo, types: Pleroma.PostgresTypes config :pleroma, Pleroma.Upload, - uploader: Pleroma.Uploaders.Local + uploader: Pleroma.Uploaders.Local, strip_exif: false config :pleroma, Pleroma.Uploaders.Local, diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index d7cc8122a..e3ad6757b 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -1,6 +1,5 @@ defmodule Pleroma.Upload do alias Ecto.UUID - alias Pleroma.Web def store(%Plug.Upload{} = file, should_dedupe) do settings = Application.get_env(:pleroma, Pleroma.Upload) @@ -26,7 +25,7 @@ defmodule Pleroma.Upload do "name" => name } end - + """ # XXX: does this code actually work? i am skeptical. --kaniini def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do settings = Application.get_env(:pleroma, Pleroma.Upload) @@ -88,11 +87,12 @@ defmodule Pleroma.Upload do "name" => name } end + """ def strip_exif_data(content_type, file) do settings = Application.get_env(:pleroma, Pleroma.Upload) do_strip = Keyword.fetch!(settings, :strip_exif) - [filetype, ext] = String.split(content_type, "/") + [filetype, _ext] = String.split(content_type, "/") if filetype == "image" and do_strip == true do Mogrify.open(file) |> Mogrify.custom("strip") |> Mogrify.save(in_place: true) diff --git a/lib/pleroma/uploaders/local.ex b/lib/pleroma/uploaders/local.ex index 7e5d7a59b..1ba68776f 100644 --- a/lib/pleroma/uploaders/local.ex +++ b/lib/pleroma/uploaders/local.ex @@ -1,7 +1,10 @@ defmodule Pleroma.Uploaders.Local do - def put_file(name, uuid, file, content_type) do - upload_path = get_upload_path(uuid, should_dedupe) + alias Pleroma.Web + + def put_file(name, uuid, file, _content_type, should_dedupe) do + + upload_folder = get_upload_path(uuid, should_dedupe) url_path = get_url(name, uuid, should_dedupe) File.mkdir_p!(upload_folder) diff --git a/lib/pleroma/uploaders/s3.ex b/lib/pleroma/uploaders/s3.ex index 95f20be67..ea9e49cbf 100644 --- a/lib/pleroma/uploaders/s3.ex +++ b/lib/pleroma/uploaders/s3.ex @@ -1,6 +1,6 @@ defmodule Pleroma.Uploaders.S3 do - def put_file(name, uuid, path, content_type) do + def put_file(name, uuid, path, content_type, _should_dedupe) do settings = Application.get_env(:pleroma, Pleroma.Uploaders.S3) bucket = Keyword.fetch!(settings, :bucket) @@ -12,7 +12,7 @@ defmodule Pleroma.Uploaders.S3 do s3_name = "#{uuid}/#{name}" - {:ok, result} = + {:ok, _} = ExAws.S3.put_object(bucket, s3_name, file_data, [ {:acl, :public_read}, {:content_type, content_type} diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index cbedca004..1e5ac2721 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -11,7 +11,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: "/media", from: Pleroma.Upload.upload_path(), gzip: false) + plug(Plug.Static, at: "/media", from: Pleroma.Uploaders.Local.upload_path(), gzip: false) plug( Plug.Static, From 8d2d7a8859754ab4beffcc43a87218631b07f378 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Tue, 28 Aug 2018 09:57:41 -0300 Subject: [PATCH 03/10] Implement uploader behaviour run formatter <# --- config/config.exs | 6 ++---- lib/pleroma/upload.ex | 9 +++++---- lib/pleroma/uploaders/local.ex | 2 +- lib/pleroma/uploaders/s3.ex | 2 +- lib/pleroma/uploaders/swift.ex | 1 + 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/config/config.exs b/config/config.exs index fe4bfe8a0..fa36209ac 100644 --- a/config/config.exs +++ b/config/config.exs @@ -14,11 +14,9 @@ config :pleroma, Pleroma.Upload, uploader: Pleroma.Uploaders.Local, strip_exif: false -config :pleroma, Pleroma.Uploaders.Local, - uploads: "uploads" +config :pleroma, Pleroma.Uploaders.Local, uploads: "uploads" -config :pleroma, Pleroma.Uploaders.S3, - s3_bucket: nil +config :pleroma, Pleroma.Uploaders.S3, s3_bucket: nil config :pleroma, :emoji, shortcode_globs: ["/emoji/custom/**/*.png"] diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index e3ad6757b..e786693ad 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -1,17 +1,17 @@ defmodule Pleroma.Upload do alias Ecto.UUID - def store(%Plug.Upload{} = file, should_dedupe) do - settings = Application.get_env(:pleroma, Pleroma.Upload) - storage_backend = Keyword.fetch!(settings, :storage_backend) + @storage_backend Application.get_env(:pleroma, Pleroma.Upload) + |> Keyword.fetch!(:uploader) + def store(%Plug.Upload{} = file, should_dedupe) do content_type = get_content_type(file.path) uuid = get_uuid(file, should_dedupe) name = get_name(file, uuid, content_type, should_dedupe) strip_exif_data(content_type, file.path) - url_path = storage_backend.put_file(name, uuid, content_type) + url_path = @storage_backend.put_file(name, uuid, file, content_type, should_dedupe) %{ "type" => "Document", @@ -25,6 +25,7 @@ defmodule Pleroma.Upload do "name" => name } end + """ # XXX: does this code actually work? i am skeptical. --kaniini def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do diff --git a/lib/pleroma/uploaders/local.ex b/lib/pleroma/uploaders/local.ex index 1ba68776f..b089c8f14 100644 --- a/lib/pleroma/uploaders/local.ex +++ b/lib/pleroma/uploaders/local.ex @@ -1,9 +1,9 @@ defmodule Pleroma.Uploaders.Local do + @behaviour Pleroma.Uploaders.Uploader alias Pleroma.Web def put_file(name, uuid, file, _content_type, should_dedupe) do - upload_folder = get_upload_path(uuid, should_dedupe) url_path = get_url(name, uuid, should_dedupe) diff --git a/lib/pleroma/uploaders/s3.ex b/lib/pleroma/uploaders/s3.ex index ea9e49cbf..e18deb6b3 100644 --- a/lib/pleroma/uploaders/s3.ex +++ b/lib/pleroma/uploaders/s3.ex @@ -1,7 +1,7 @@ defmodule Pleroma.Uploaders.S3 do + @behaviour Pleroma.Uploaders.Uploader def put_file(name, uuid, path, content_type, _should_dedupe) do - settings = Application.get_env(:pleroma, Pleroma.Uploaders.S3) bucket = Keyword.fetch!(settings, :bucket) public_endpoint = Keyword.fetch!(settings, :public_endpoint) diff --git a/lib/pleroma/uploaders/swift.ex b/lib/pleroma/uploaders/swift.ex index e69de29bb..8b1378917 100644 --- a/lib/pleroma/uploaders/swift.ex +++ b/lib/pleroma/uploaders/swift.ex @@ -0,0 +1 @@ + From dad39b24a1bca0341d5cf47cc4a32ea66219c654 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Tue, 28 Aug 2018 19:48:03 -0300 Subject: [PATCH 04/10] add the behaviour, work on actually making it work. --- lib/pleroma/upload.ex | 50 ++++++++++--------------------- lib/pleroma/uploaders/local.ex | 6 ++-- lib/pleroma/uploaders/uploader.ex | 26 ++++++++++++++++ 3 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 lib/pleroma/uploaders/uploader.ex diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index e786693ad..16149d4dd 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -1,11 +1,13 @@ defmodule Pleroma.Upload do alias Ecto.UUID + import Logger @storage_backend Application.get_env(:pleroma, Pleroma.Upload) |> Keyword.fetch!(:uploader) def store(%Plug.Upload{} = file, should_dedupe) do content_type = get_content_type(file.path) + uuid = get_uuid(file, should_dedupe) name = get_name(file, uuid, content_type, should_dedupe) @@ -26,23 +28,21 @@ defmodule Pleroma.Upload do } end - """ # XXX: does this code actually work? i am skeptical. --kaniini def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do - settings = Application.get_env(:pleroma, Pleroma.Upload) - use_s3 = Keyword.fetch!(settings, :use_s3) - parsed = Regex.named_captures(~r/(?jpeg|png|gif);base64,(?.*)/, image_data) data = Base.decode64!(parsed["data"], ignore: :whitespace) - uuid = UUID.generate() - uuidpath = Path.join(upload_path(), uuid) + + tmp_path = mkupload_for_image(data) + uuid = UUID.generate() - File.mkdir_p!(upload_path()) + # create temp local storage, like plug upload provides for us. - File.write!(uuidpath, data) + Logger.info(tmp_path) - content_type = get_content_type(uuidpath) + content_type = get_content_type(tmp_path) + strip_exif_data(content_type, tmp_path) name = create_name( @@ -51,30 +51,7 @@ defmodule Pleroma.Upload do content_type ) - upload_folder = get_upload_path(uuid, should_dedupe) - url_path = get_url(name, uuid, should_dedupe) - - File.mkdir_p!(upload_folder) - result_file = Path.join(upload_folder, name) - - if should_dedupe do - if !File.exists?(result_file) do - File.rename(uuidpath, result_file) - else - File.rm!(uuidpath) - end - else - File.rename(uuidpath, result_file) - end - - strip_exif_data(content_type, result_file) - - url_path = - if use_s3 do - put_s3_file(name, uuid, result_file, content_type) - else - url_path - end + url_path = @storage_backend.put_file(name, uuid, tmp_path, content_type, should_dedupe) %{ "type" => "Image", @@ -88,7 +65,12 @@ defmodule Pleroma.Upload do "name" => name } end - """ + + def mkupload_for_image(data) do + {:ok, tmp_path} = Plug.Upload.random_file("profile_pics") + :file.write_file(tmp_path, data, [:write, :raw, :exclusive, :binary]) + tmp_path + end def strip_exif_data(content_type, file) do settings = Application.get_env(:pleroma, Pleroma.Upload) diff --git a/lib/pleroma/uploaders/local.ex b/lib/pleroma/uploaders/local.ex index b089c8f14..39dca49c9 100644 --- a/lib/pleroma/uploaders/local.ex +++ b/lib/pleroma/uploaders/local.ex @@ -3,7 +3,7 @@ defmodule Pleroma.Uploaders.Local do alias Pleroma.Web - def put_file(name, uuid, file, _content_type, should_dedupe) do + def put_file(name, uuid, tmpfile, _content_type, should_dedupe) do upload_folder = get_upload_path(uuid, should_dedupe) url_path = get_url(name, uuid, should_dedupe) @@ -12,9 +12,9 @@ defmodule Pleroma.Uploaders.Local do result_file = Path.join(upload_folder, name) if File.exists?(result_file) do - File.rm!(file.path) + File.rm!(tmpfile) else - File.cp!(file.path, result_file) + File.cp!(tmpfile, result_file) end url_path diff --git a/lib/pleroma/uploaders/uploader.ex b/lib/pleroma/uploaders/uploader.ex new file mode 100644 index 000000000..7380320af --- /dev/null +++ b/lib/pleroma/uploaders/uploader.ex @@ -0,0 +1,26 @@ +defmodule Pleroma.Uploaders.Uploader do + @moduledoc """ + Defines the contract to put an uploaded file to any backend. + """ + + @doc """ + Put a file to the backend. + + Returns a `String.t` containing the path of the uploaded file. + """ + @callback put_file( + name :: String.t(), + uuid :: String.t(), + file :: File.t(), + content_type :: String.t(), + should_dedupe :: Boolean.t() + ) :: String.t() + + @callback put_file( + name :: String.t(), + uuid :: String.t(), + image_data :: String.t(), + content_type :: String.t(), + should_dedupe :: String.t() + ) :: String.t() +end From 9fc20ed5720bccb77289ce3d6eb9bc3a69ceeb8a Mon Sep 17 00:00:00 2001 From: Thurloat Date: Tue, 28 Aug 2018 20:04:26 -0300 Subject: [PATCH 05/10] works now, tested with profile photo upload on local backend. --- lib/pleroma/upload.ex | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index 16149d4dd..b70758dc7 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -1,6 +1,5 @@ defmodule Pleroma.Upload do alias Ecto.UUID - import Logger @storage_backend Application.get_env(:pleroma, Pleroma.Upload) |> Keyword.fetch!(:uploader) @@ -28,19 +27,15 @@ defmodule Pleroma.Upload do } end - # XXX: does this code actually work? i am skeptical. --kaniini def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do parsed = Regex.named_captures(~r/(?jpeg|png|gif);base64,(?.*)/, image_data) data = Base.decode64!(parsed["data"], ignore: :whitespace) - tmp_path = mkupload_for_image(data) + # create temp local storage, like plug upload provides. + tmp_path = tempfile_for_image(data) uuid = UUID.generate() - # create temp local storage, like plug upload provides for us. - - Logger.info(tmp_path) - content_type = get_content_type(tmp_path) strip_exif_data(content_type, tmp_path) @@ -66,9 +61,11 @@ defmodule Pleroma.Upload do } end - def mkupload_for_image(data) do + def tempfile_for_image(data) do {:ok, tmp_path} = Plug.Upload.random_file("profile_pics") - :file.write_file(tmp_path, data, [:write, :raw, :exclusive, :binary]) + {:ok, tmp_file} = File.open(tmp_path, [:write, :raw, :binary]) + IO.binwrite(tmp_file, data) + tmp_path end From 2ff25ac0ceb98f2ee1c803aeb8aecc112e335877 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Tue, 28 Aug 2018 22:32:24 -0300 Subject: [PATCH 06/10] A hobbldey-working swift client. apparently, all elixir openstack libraries are trash luckily, the APIs are stupid easy. --- lib/pleroma/upload.ex | 2 +- lib/pleroma/uploaders/swift.ex | 1 - lib/pleroma/uploaders/swift/keystone.ex | 48 +++++++++++++++++++++++++ lib/pleroma/uploaders/swift/swift.ex | 30 ++++++++++++++++ lib/pleroma/uploaders/swift/uploader.ex | 15 ++++++++ 5 files changed, 94 insertions(+), 2 deletions(-) delete mode 100644 lib/pleroma/uploaders/swift.ex create mode 100644 lib/pleroma/uploaders/swift/keystone.ex create mode 100644 lib/pleroma/uploaders/swift/swift.ex create mode 100644 lib/pleroma/uploaders/swift/uploader.ex diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index b70758dc7..7d3b36287 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -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 index 8b1378917..000000000 --- a/lib/pleroma/uploaders/swift.ex +++ /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 index 000000000..a79214319 --- /dev/null +++ b/lib/pleroma/uploaders/swift/keystone.ex @@ -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 index 000000000..4f45255f1 --- /dev/null +++ b/lib/pleroma/uploaders/swift/swift.ex @@ -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 index 000000000..c71808c2d --- /dev/null +++ b/lib/pleroma/uploaders/swift/uploader.ex @@ -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 From ab9e5d64d6dc6911dadac7219d38f3749971f53c Mon Sep 17 00:00:00 2001 From: Thurloat Date: Tue, 28 Aug 2018 22:39:33 -0300 Subject: [PATCH 07/10] add a sample swift config --- lib/mix/tasks/sample_config.eex | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/mix/tasks/sample_config.eex b/lib/mix/tasks/sample_config.eex index 2acf35ed9..3b4953cda 100644 --- a/lib/mix/tasks/sample_config.eex +++ b/lib/mix/tasks/sample_config.eex @@ -44,3 +44,21 @@ config :pleroma, Pleroma.Repo, # For using third-party S3 clones like wasabi, also do: # config :ex_aws, :s3, # host: "s3.wasabisys.com" + + +# Configure Openstack Swift support if desired. +# +# Many openstack deployments are different, so config is left very open with +# no assumptions made on which provider you're using. This should allow very +# wide support without needing separate handlers for OVH, Rackspace, etc. +# +# config :pleroma, Pleroma.Uploaders.Swift, +# container: "some-container", +# username: "api-username-yyyy", +# password: "api-key-xxxx", +# tenant_id: "", +# auth_url: "https://keystone-endpoint.provider.com", +# storage_url: "https://swift-endpoint.prodider.com/v1/AUTH_/", +# object_url: "https://cdn-endpoint.provider.com/" +# + From d424e9fa5f3d0d1ff9e416f6bf548e0e8bb02361 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Tue, 28 Aug 2018 23:49:23 -0300 Subject: [PATCH 08/10] fix S3 ref in sample config to generate proper path. --- lib/mix/tasks/sample_config.eex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/mix/tasks/sample_config.eex b/lib/mix/tasks/sample_config.eex index 3b4953cda..3881ead26 100644 --- a/lib/mix/tasks/sample_config.eex +++ b/lib/mix/tasks/sample_config.eex @@ -29,8 +29,7 @@ config :pleroma, Pleroma.Repo, # The public S3 endpoint is different depending on region and provider, # consult your S3 provider's documentation for details on what to use. # -# config :pleroma, Pleroma.Upload, -# use_s3: true, +# config :pleroma, Pleroma.Uploaders.S3, # bucket: "some-bucket", # public_endpoint: "https://s3.amazonaws.com" # From af01f0196a43454728f6e0ca8b9b8be208743251 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Wed, 29 Aug 2018 22:07:28 -0300 Subject: [PATCH 09/10] Add backend failure handling with :ok | :error so the uploader can handle it. defaulting to :ok, since that's the currently level of error handling. --- lib/pleroma/upload.ex | 10 +++++++--- lib/pleroma/uploaders/local.ex | 2 +- lib/pleroma/uploaders/s3.ex | 2 +- lib/pleroma/uploaders/swift/swift.ex | 10 ++++------ lib/pleroma/uploaders/swift/uploader.ex | 5 ----- lib/pleroma/uploaders/uploader.ex | 10 +--------- 6 files changed, 14 insertions(+), 25 deletions(-) diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index 7d3b36287..f188a5f32 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -12,7 +12,8 @@ defmodule Pleroma.Upload do strip_exif_data(content_type, file.path) - url_path = @storage_backend.put_file(name, uuid, file.path, content_type, should_dedupe) + {:ok, url_path} = + @storage_backend.put_file(name, uuid, file.path, content_type, should_dedupe) %{ "type" => "Document", @@ -31,7 +32,6 @@ defmodule Pleroma.Upload do parsed = Regex.named_captures(~r/(?jpeg|png|gif);base64,(?.*)/, image_data) data = Base.decode64!(parsed["data"], ignore: :whitespace) - # create temp local storage, like plug upload provides. tmp_path = tempfile_for_image(data) uuid = UUID.generate() @@ -46,7 +46,7 @@ defmodule Pleroma.Upload do content_type ) - url_path = @storage_backend.put_file(name, uuid, tmp_path, content_type, should_dedupe) + {:ok, url_path} = @storage_backend.put_file(name, uuid, tmp_path, content_type, should_dedupe) %{ "type" => "Image", @@ -61,6 +61,10 @@ defmodule Pleroma.Upload do } end + @doc """ + Creates a tempfile using the Plug.Upload Genserver which cleans them up + automatically. + """ def tempfile_for_image(data) do {:ok, tmp_path} = Plug.Upload.random_file("profile_pics") {:ok, tmp_file} = File.open(tmp_path, [:write, :raw, :binary]) diff --git a/lib/pleroma/uploaders/local.ex b/lib/pleroma/uploaders/local.ex index 39dca49c9..d4624661f 100644 --- a/lib/pleroma/uploaders/local.ex +++ b/lib/pleroma/uploaders/local.ex @@ -17,7 +17,7 @@ defmodule Pleroma.Uploaders.Local do File.cp!(tmpfile, result_file) end - url_path + {:ok, url_path} end def upload_path do diff --git a/lib/pleroma/uploaders/s3.ex b/lib/pleroma/uploaders/s3.ex index e18deb6b3..ce0ed3e34 100644 --- a/lib/pleroma/uploaders/s3.ex +++ b/lib/pleroma/uploaders/s3.ex @@ -19,6 +19,6 @@ defmodule Pleroma.Uploaders.S3 do ]) |> ExAws.request() - "#{public_endpoint}/#{bucket}/#{s3_name}" + {:ok, "#{public_endpoint}/#{bucket}/#{s3_name}"} end end diff --git a/lib/pleroma/uploaders/swift/swift.ex b/lib/pleroma/uploaders/swift/swift.ex index 4f45255f1..819dfebda 100644 --- a/lib/pleroma/uploaders/swift/swift.ex +++ b/lib/pleroma/uploaders/swift/swift.ex @@ -11,20 +11,18 @@ defmodule Pleroma.Uploaders.Swift.Client do end def upload_file(filename, body, content_type) do + object_url = Keyword.fetch!(@settings, :object_url) 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, "#{object_url}/#{filename}"} {:ok, %HTTPoison.Response{status_code: 401}} -> - # bad token - "" + {:error, "Unauthorized, Bad Token"} {:error, _} -> - # bad news - "" + {:error, "Swift Upload Error"} end end end diff --git a/lib/pleroma/uploaders/swift/uploader.ex b/lib/pleroma/uploaders/swift/uploader.ex index c71808c2d..794f76cb0 100644 --- a/lib/pleroma/uploaders/swift/uploader.ex +++ b/lib/pleroma/uploaders/swift/uploader.ex @@ -1,15 +1,10 @@ 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 diff --git a/lib/pleroma/uploaders/uploader.ex b/lib/pleroma/uploaders/uploader.ex index 7380320af..19bea77dc 100644 --- a/lib/pleroma/uploaders/uploader.ex +++ b/lib/pleroma/uploaders/uploader.ex @@ -14,13 +14,5 @@ defmodule Pleroma.Uploaders.Uploader do file :: File.t(), content_type :: String.t(), should_dedupe :: Boolean.t() - ) :: String.t() - - @callback put_file( - name :: String.t(), - uuid :: String.t(), - image_data :: String.t(), - content_type :: String.t(), - should_dedupe :: String.t() - ) :: String.t() + ) :: {:ok, String.t()} | {:error, String.t()} end From adffad55028a8c43a6f2fcf43376009a43c25cf7 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Thu, 30 Aug 2018 09:20:29 -0300 Subject: [PATCH 10/10] increase uploader behaviour documentation accuracy. --- lib/pleroma/uploaders/uploader.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/uploaders/uploader.ex b/lib/pleroma/uploaders/uploader.ex index 19bea77dc..b58fc6d71 100644 --- a/lib/pleroma/uploaders/uploader.ex +++ b/lib/pleroma/uploaders/uploader.ex @@ -6,7 +6,9 @@ defmodule Pleroma.Uploaders.Uploader do @doc """ Put a file to the backend. - Returns a `String.t` containing the path of the uploaded file. + Returns `{:ok, String.t } | {:error, String.t} containing the path of the + uploaded file, or error information if the file failed to be saved to the + respective backend. """ @callback put_file( name :: String.t(),