Skip to content

Commit

Permalink
chore: Refactoring CID and file extension upload part. #69
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Jul 11, 2023
1 parent cb0a9a3 commit beb03a1
Showing 1 changed file with 82 additions and 70 deletions.
152 changes: 82 additions & 70 deletions lib/app/upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,85 @@ defmodule App.Upload do
the an error is returned `{:error, reason}`.
"""
def upload(image) do
# Create `CID` from file contents so filenames are unique
with {:ok, {file_cid, file_extension}} <- check_file_binary_and_extension(image) do
# Check if file `cid` and extension are valid.
case {file_cid, file_extension} do
{"invalid data type", nil} ->
{:error, :invalid_extension_and_cid}

{"invalid data type", _extension} ->
{:error, :invalid_cid}

{_cid, nil} ->
{:error, :invalid_extension}

# If the `cid` and extension are valid, we are safe to upload
{file_cid, file_extension} ->
# Creating filename with the retrieved extension
file_name = "#{file_cid}.#{file_extension}"

# Upload to S3
request_response =
try do
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())
rescue
_e ->
{:error, :upload_fail}
end

# Check if the request was successful
case request_response do
# If the request was successful, we parse the result
{:ok, body} ->
# 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}}

# If the request was unsuccessful, throw an error
{:error, _reason} ->
{:error, :upload_fail}
end
end

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

defp check_file_binary_and_extension(image) do
case File.read(image.path) do
# Create `CID` from file contents so filenames are unique
{:ok, file_binary} ->
file_cid = Cid.cid(file_binary)

Expand All @@ -28,81 +105,16 @@ defmodule App.Upload do
|> MIME.extensions()
|> List.first()

# Check if file `cid` and extension are valid.
case {file_cid, file_extension} do
{"invalid data type", nil} ->
{:error, :invalid_extension_and_cid}

{"invalid data type", _extension} ->
{:error, :invalid_cid}

{_cid, nil} ->
{:error, :invalid_extension}

# If the `cid` and extension are valid, we are safe to upload
{file_cid, file_extension} ->
# Creating filename with the retrieved extension
file_name = "#{file_cid}.#{file_extension}"

# Upload to S3
request_response =
try do
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())
rescue
_e ->
{:error, :upload_fail}
end

# Check if the request was successful
case request_response do
# If the request was successful, we parse the result
{:ok, body} ->
# 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}}

# If the request was unsuccessful, throw an error
{:error, _reason} ->
{:error, :upload_fail}
end
end
# Return the file's content CID and its MIME extension
{:ok, {file_cid, file_extension}}

# If image can't be opened, return error
{:error, _reason} ->
{:error, :failure_read}
end
end


def get_ex_aws_request_config_override,
do: Application.get_env(:ex_aws, :request_config_override)
end

0 comments on commit beb03a1

Please sign in to comment.