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.
This commit is contained in:
Thurloat 2018-08-29 22:07:28 -03:00
parent d424e9fa5f
commit af01f0196a
6 changed files with 14 additions and 25 deletions

View File

@ -12,7 +12,8 @@ def store(%Plug.Upload{} = file, should_dedupe) 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 @@ def store(%{"img" => "data:image/" <> image_data}, should_dedupe) 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 @@ def store(%{"img" => "data:image/" <> image_data}, should_dedupe) 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 @@ def store(%{"img" => "data:image/" <> image_data}, should_dedupe) 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])

View File

@ -17,7 +17,7 @@ def put_file(name, uuid, tmpfile, _content_type, should_dedupe) do
File.cp!(tmpfile, result_file)
end
url_path
{:ok, url_path}
end
def upload_path do

View File

@ -19,6 +19,6 @@ def put_file(name, uuid, path, content_type, _should_dedupe) do
])
|> ExAws.request()
"#{public_endpoint}/#{bucket}/#{s3_name}"
{:ok, "#{public_endpoint}/#{bucket}/#{s3_name}"}
end
end

View File

@ -11,20 +11,18 @@ def process_url(url) 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

View 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

View 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