Skip to content

Commit

Permalink
fix: Pattern matching when file is read. #69
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Jun 27, 2023
1 parent 2f51c88 commit 3fa6ba3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 41 deletions.
79 changes: 42 additions & 37 deletions lib/app/upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,51 @@ defmodule App.Upload do
"""
def upload(image) do
# Create `CID` from file contents so filenames are unique
#
{:ok, file_binary} = File.read(image.path)
file_cid = Cid.cid(file_binary)
file_name = "#{file_cid}.#{Enum.at(MIME.extensions(image.content_type), 0)}"
case File.read(image.path) do
{:ok, file_binary} ->

# Upload to S3
{:ok, body} =
image.path
|> ExAws.S3.Upload.stream_file()
|> ExAws.S3.upload(Application.get_env(:ex_aws, :original_bucket), file_name, acl: :public_read, content_type: image.content_type )
|> ExAws.request(get_ex_aws_request_config_override())
file_cid = Cid.cid(file_binary)
file_name = "#{file_cid}.#{Enum.at(MIME.extensions(image.content_type), 0)}"

# Sample response:
# %{
# body: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n
# <CompleteMultipartUploadResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">
# <Location>https://s3.eu-west-3.amazonaws.com/imgup-original/qvWtbC7WaT.jpg</Location>
# <Bucket>imgup-original</Bucket><Key>qvWtbC7WaT.jpg</Key>
# <ETag>\"4ecd62951576b7e5b4a3e869e5e98a0f-1\"</ETag></CompleteMultipartUploadResult>",
# headers: [
# {"x-amz-id-2",
# "wNTNZKt82vgnOuT1o2Tz8z3gcRzd6wXofYxQmBUkGbBGTpmv1WbwjjGiRAUtOTYIm92bh/VJHhI="},
# {"x-amz-request-id", "QRENBY1MJTQWD7CZ"},
# {"Date", "Tue, 13 Jun 2023 10:22:44 GMT"},
# {"x-amz-expiration",
# "expiry-date=\"Thu, 15 Jun 2023 00:00:00 GMT\", rule-id=\"delete-after-1-day\""},
# {"x-amz-server-side-encryption", "AES256"},
# {"Content-Type", "application/xml"},
# {"Transfer-Encoding", "chunked"},
# {"Server", "AmazonS3"}
# ],
# status_code: 200
# }
# Upload to S3
{:ok, body} =
image.path
|> ExAws.S3.Upload.stream_file()
|> ExAws.S3.upload(Application.get_env(:ex_aws, :original_bucket), file_name, acl: :public_read, content_type: image.content_type )
|> ExAws.request(get_ex_aws_request_config_override())

# Fetch the contents of the returned XML string from `ex_aws`.
# This XML is parsed with `sweet_xml`:
# github.com/kbrw/sweet_xml#the-x-sigil
url = body.body |> xpath(~x"//text()") |> List.to_string()
compressed_url = "#{@compressed_baseurl}#{file_name}"
{:ok, %{url: url, compressed_url: compressed_url}}
# Sample response:
# %{
# body: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n
# <CompleteMultipartUploadResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">
# <Location>https://s3.eu-west-3.amazonaws.com/imgup-original/qvWtbC7WaT.jpg</Location>
# <Bucket>imgup-original</Bucket><Key>qvWtbC7WaT.jpg</Key>
# <ETag>\"4ecd62951576b7e5b4a3e869e5e98a0f-1\"</ETag></CompleteMultipartUploadResult>",
# headers: [
# {"x-amz-id-2",
# "wNTNZKt82vgnOuT1o2Tz8z3gcRzd6wXofYxQmBUkGbBGTpmv1WbwjjGiRAUtOTYIm92bh/VJHhI="},
# {"x-amz-request-id", "QRENBY1MJTQWD7CZ"},
# {"Date", "Tue, 13 Jun 2023 10:22:44 GMT"},
# {"x-amz-expiration",
# "expiry-date=\"Thu, 15 Jun 2023 00:00:00 GMT\", rule-id=\"delete-after-1-day\""},
# {"x-amz-server-side-encryption", "AES256"},
# {"Content-Type", "application/xml"},
# {"Transfer-Encoding", "chunked"},
# {"Server", "AmazonS3"}
# ],
# status_code: 200
# }

# Fetch the contents of the returned XML string from `ex_aws`.
# This XML is parsed with `sweet_xml`:
# github.com/kbrw/sweet_xml#the-x-sigil
url = body.body |> xpath(~x"//text()") |> List.to_string()
compressed_url = "#{@compressed_baseurl}#{file_name}"
{:ok, %{url: url, compressed_url: compressed_url}}

{:error, _reason} ->
{:error, :failure_read}
end
end

def get_ex_aws_request_config_override,
Expand Down
11 changes: 9 additions & 2 deletions lib/app_web/controllers/api_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ defmodule AppWeb.ApiController do
# check if content_type e.g: "image/png"
if String.contains?(params.content_type, "image") do
try do
{:ok, body} = App.Upload.upload(params)
render(conn, :success, body)

case App.Upload.upload(params) do
{:ok, body} -> render(conn, :success, body)
{:error, :failure_read} ->
render(conn |> put_status(400), %{body: "Error uploading file. Failure reading file."})
{:error, _reason} ->
render(conn |> put_status(400), %{body: "Error uploading file #26"})
end

rescue
e ->
Logger.error(Exception.format(:error, e, __STACKTRACE__))
Expand Down
3 changes: 1 addition & 2 deletions test/app_web/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@ defmodule AppWeb.APITest do

# github.com/elixir-lang/elixir/blob/main/lib/elixir/test/elixir/kernel/raise_test.exs
test "non existent image throws runtime error (test rescue branch)", %{conn: conn} do
IO.puts(" -> Don't Panic! This error stack trace is expected when file doesn't exist: ")
conn = post(conn, ~p"/api/images", @non_existent_image)

assert Map.get(Jason.decode!(response(conn, 400)), "errors") == %{
"detail" => "Error uploading file #26"
"detail" => "Error uploading file. Failure reading file."
}
end
end

0 comments on commit 3fa6ba3

Please sign in to comment.