From af01f0196a43454728f6e0ca8b9b8be208743251 Mon Sep 17 00:00:00 2001 From: Thurloat Date: Wed, 29 Aug 2018 22:07:28 -0300 Subject: [PATCH] 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