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:
parent
d424e9fa5f
commit
af01f0196a
@ -12,7 +12,8 @@ def store(%Plug.Upload{} = file, should_dedupe) do
|
|||||||
|
|
||||||
strip_exif_data(content_type, file.path)
|
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",
|
"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)
|
parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
|
||||||
data = Base.decode64!(parsed["data"], ignore: :whitespace)
|
data = Base.decode64!(parsed["data"], ignore: :whitespace)
|
||||||
|
|
||||||
# create temp local storage, like plug upload provides.
|
|
||||||
tmp_path = tempfile_for_image(data)
|
tmp_path = tempfile_for_image(data)
|
||||||
|
|
||||||
uuid = UUID.generate()
|
uuid = UUID.generate()
|
||||||
@ -46,7 +46,7 @@ def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
|
|||||||
content_type
|
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",
|
"type" => "Image",
|
||||||
@ -61,6 +61,10 @@ def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Creates a tempfile using the Plug.Upload Genserver which cleans them up
|
||||||
|
automatically.
|
||||||
|
"""
|
||||||
def tempfile_for_image(data) do
|
def tempfile_for_image(data) do
|
||||||
{:ok, tmp_path} = Plug.Upload.random_file("profile_pics")
|
{:ok, tmp_path} = Plug.Upload.random_file("profile_pics")
|
||||||
{:ok, tmp_file} = File.open(tmp_path, [:write, :raw, :binary])
|
{:ok, tmp_file} = File.open(tmp_path, [:write, :raw, :binary])
|
||||||
|
@ -17,7 +17,7 @@ def put_file(name, uuid, tmpfile, _content_type, should_dedupe) do
|
|||||||
File.cp!(tmpfile, result_file)
|
File.cp!(tmpfile, result_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
url_path
|
{:ok, url_path}
|
||||||
end
|
end
|
||||||
|
|
||||||
def upload_path do
|
def upload_path do
|
||||||
|
@ -19,6 +19,6 @@ def put_file(name, uuid, path, content_type, _should_dedupe) do
|
|||||||
])
|
])
|
||||||
|> ExAws.request()
|
|> ExAws.request()
|
||||||
|
|
||||||
"#{public_endpoint}/#{bucket}/#{s3_name}"
|
{:ok, "#{public_endpoint}/#{bucket}/#{s3_name}"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -11,20 +11,18 @@ def process_url(url) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def upload_file(filename, body, content_type) do
|
def upload_file(filename, body, content_type) do
|
||||||
|
object_url = Keyword.fetch!(@settings, :object_url)
|
||||||
token = Pleroma.Uploaders.Swift.Keystone.get_token()
|
token = Pleroma.Uploaders.Swift.Keystone.get_token()
|
||||||
|
|
||||||
case put("#{filename}", body, "X-Auth-Token": token, "Content-Type": content_type) do
|
case put("#{filename}", body, "X-Auth-Token": token, "Content-Type": content_type) do
|
||||||
{:ok, %HTTPoison.Response{status_code: 201}} ->
|
{:ok, %HTTPoison.Response{status_code: 201}} ->
|
||||||
# lgtm
|
{:ok, "#{object_url}/#{filename}"}
|
||||||
""
|
|
||||||
|
|
||||||
{:ok, %HTTPoison.Response{status_code: 401}} ->
|
{:ok, %HTTPoison.Response{status_code: 401}} ->
|
||||||
# bad token
|
{:error, "Unauthorized, Bad Token"}
|
||||||
""
|
|
||||||
|
|
||||||
{:error, _} ->
|
{:error, _} ->
|
||||||
# bad news
|
{:error, "Swift Upload Error"}
|
||||||
""
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,15 +1,10 @@
|
|||||||
defmodule Pleroma.Uploaders.Swift do
|
defmodule Pleroma.Uploaders.Swift do
|
||||||
@behaviour Pleroma.Uploaders.Uploader
|
@behaviour Pleroma.Uploaders.Uploader
|
||||||
|
|
||||||
@settings Application.get_env(:pleroma, Pleroma.Uploaders.Swift)
|
|
||||||
|
|
||||||
def put_file(name, uuid, tmp_path, content_type, _should_dedupe) do
|
def put_file(name, uuid, tmp_path, content_type, _should_dedupe) do
|
||||||
{:ok, file_data} = File.read(tmp_path)
|
{:ok, file_data} = File.read(tmp_path)
|
||||||
remote_name = "#{uuid}/#{name}"
|
remote_name = "#{uuid}/#{name}"
|
||||||
|
|
||||||
Pleroma.Uploaders.Swift.Client.upload_file(remote_name, file_data, content_type)
|
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
|
||||||
end
|
end
|
||||||
|
@ -14,13 +14,5 @@ defmodule Pleroma.Uploaders.Uploader do
|
|||||||
file :: File.t(),
|
file :: File.t(),
|
||||||
content_type :: String.t(),
|
content_type :: String.t(),
|
||||||
should_dedupe :: Boolean.t()
|
should_dedupe :: Boolean.t()
|
||||||
) :: String.t()
|
) :: {:ok, String.t()} | {:error, 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
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user