Add backend failure handling with :ok | :error so the uploader can handle it.
authorThurloat <thurloat@gmail.com>
Thu, 30 Aug 2018 01:07:28 +0000 (22:07 -0300)
committerThurloat <thurloat@gmail.com>
Thu, 30 Aug 2018 01:07:28 +0000 (22:07 -0300)
defaulting to :ok, since that's the currently level of error handling.

lib/pleroma/upload.ex
lib/pleroma/uploaders/local.ex
lib/pleroma/uploaders/s3.ex
lib/pleroma/uploaders/swift/swift.ex
lib/pleroma/uploaders/swift/uploader.ex
lib/pleroma/uploaders/uploader.ex

index 7d3b362878835d4d87ce967c8a1b2b773cb90a02..f188a5f3285709d28ab56faea4a919fafd1b81ec 100644 (file)
@@ -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/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, 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])
index 39dca49c9ade14819942dbed86aea8de5f41b02b..d4624661f9ec638468ffefb573700601c382ad5d 100644 (file)
@@ -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
index e18deb6b330a6253877e51dacbd12dfb3a25edd1..ce0ed3e34f38ae8ff72ce0dd8c766287a2fbc8a5 100644 (file)
@@ -19,6 +19,6 @@ defmodule Pleroma.Uploaders.S3 do
       ])
       |> ExAws.request()
 
-    "#{public_endpoint}/#{bucket}/#{s3_name}"
+    {:ok, "#{public_endpoint}/#{bucket}/#{s3_name}"}
   end
 end
index 4f45255f127145a4c0de6ae03c7d20db45849844..819dfebda1f77442dae16977266f3c541484c17b 100644 (file)
@@ -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
index c71808c2deac815da03a75a31132618d8c94d8c5..794f76cb08f1698f7ff824bfb114db354c736e2f 100644 (file)
@@ -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
index 7380320af41cd2f5b4e71f1feb140a61a1abe714..19bea77dcfa41ef0de8692a10258a522cd64e5e3 100644 (file)
@@ -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