Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR] Adding env variables to deploy workflow so compile_env works. #102

Merged
merged 11 commits into from
Jul 19, 2023
Merged
1 change: 1 addition & 0 deletions .github/workflows/fly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
push:
branches:
- main

jobs:
deploy:
name: Deploy app
Expand Down
9 changes: 7 additions & 2 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ add:
{:ok, upload_response_body} =
image.path
|> ExAws.S3.Upload.stream_file()
|> ExAws.S3.upload(@original_bucket, file_name,
|> ExAws.S3.upload(Application.get_env(:ex_aws, :original_bucket), file_name,
nelsonic marked this conversation as resolved.
Show resolved Hide resolved
acl: :public_read,
content_type: image.content_type
)
Expand Down Expand Up @@ -710,8 +710,13 @@ making it simpler!
# 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
#
# Fetching the URL of the returned file.
url = upload_response_body.body |> xpath(~x"//text()") |> List.to_string()
compressed_url = "#{@compressed_baseurl}#{file_name}"

# Creating the compressed URL to return as well
compressed_bucket_baseurl = "https://s3.eu-west-3.amazonaws.com/#{Application.get_env(:ex_aws, :compressed_bucket)}/"
compressed_url = "#{compressed_bucket_baseurl}#{file_name}"
{:ok, %{url: url, compressed_url: compressed_url}}
else
{:error, reason} -> {:error, reason}
Expand Down
12 changes: 6 additions & 6 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ if config_env() == :prod do

# https://github.com/dwyl/imgup/issues/68
config :ex_aws,
access_key_id: System.get_env("AWS_ACCESS_KEY_ID"),
secret_access_key: System.get_env("AWS_SECRET_ACCESS_KEY"),
region: System.get_env("AWS_REGION"),
original_bucket: System.get_env("AWS_S3_BUCKET_ORIGINAL"),
compressed_bucket: System.get_env("AWS_S3_BUCKET_COMPRESSED"),
request_config_override: %{}
access_key_id: System.get_env("AWS_ACCESS_KEY_ID"),
secret_access_key: System.get_env("AWS_SECRET_ACCESS_KEY"),
region: System.get_env("AWS_REGION"),
original_bucket: System.get_env("AWS_S3_BUCKET_ORIGINAL"),
compressed_bucket: System.get_env("AWS_S3_BUCKET_COMPRESSED"),
request_config_override: %{}
end
22 changes: 16 additions & 6 deletions lib/app/upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ defmodule App.Upload do
import SweetXml
require Logger

@original_bucket Application.compile_env(:ex_aws, :original_bucket)
@compressed_bucket Application.compile_env(:ex_aws, :compressed_bucket)
@compressed_baseurl "https://s3.eu-west-3.amazonaws.com/#{@compressed_bucket}/"
# function gets cached
defp compressed_baseurl() do
compressed_bucket = Application.get_env(:ex_aws, :compressed_bucket)
"https://s3.eu-west-3.amazonaws.com/#{compressed_bucket}/"
end

@doc """
`upload/1` receives an `image` with the format
Expand Down Expand Up @@ -48,8 +50,12 @@ defmodule App.Upload do
# 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
#
# Fetching the URL of the returned file.
url = upload_response_body.body |> xpath(~x"//text()") |> List.to_string()
compressed_url = "#{@compressed_baseurl}#{file_name}"

# Creating the compressed URL to return as well
compressed_url = "#{compressed_baseurl()}#{file_name}"
{:ok, %{url: url, compressed_url: compressed_url}}
else
{:error, reason} -> {:error, reason}
Expand All @@ -59,6 +65,7 @@ defmodule App.Upload do
def upload_file_to_s3(file_cid, file_extension, image) do
# Creating filename with the retrieved extension
file_name = "#{file_cid}.#{file_extension}"
s3_bucket = Application.get_env(:ex_aws, :original_bucket)

# Make request.
# Return the body of the response if successful.
Expand All @@ -67,7 +74,7 @@ defmodule App.Upload do
{:ok, upload_response_body} =
image.path
|> ExAws.S3.Upload.stream_file()
|> ExAws.S3.upload(@original_bucket, file_name,
|> ExAws.S3.upload(s3_bucket, file_name,
acl: :public_read,
content_type: image.content_type
)
Expand Down Expand Up @@ -98,7 +105,10 @@ defmodule App.Upload do
# Otherwise, return error.
case {file_cid, file_extension} do
{"invalid data type", nil} ->
Logger.error("File extension is invalid and the CID derived from the file contents is also invalid: #{inspect(image)}")
Logger.error(
"File extension is invalid and the CID derived from the file contents is also invalid: #{inspect(image)}"
)

{:error, :invalid_extension_and_cid}

{"invalid data type", _extension} ->
Expand Down