defaulting to :ok, since that's the currently level of error handling.
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",
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()
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",
}
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])
File.cp!(tmpfile, result_file)
end
- url_path
+ {:ok, url_path}
end
def upload_path do
])
|> ExAws.request()
- "#{public_endpoint}/#{bucket}/#{s3_name}"
+ {:ok, "#{public_endpoint}/#{bucket}/#{s3_name}"}
end
end
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
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
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