-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,5 @@ erl_crash.dump | |
/config/*.secret.exs | ||
|
||
/.idea | ||
|
||
/config/test_credential.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
defmodule Materia.Files.GoogleCloudPlatformConfig do | ||
use Goth.Config | ||
|
||
def init(config) do | ||
{:ok, Keyword.put(config, :json, System.get_env("GCP_JSON_CREDENTIALS") |> File.read!)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Materia.Files.GoogleCloudPlatformConfigStub do | ||
use Goth.Config | ||
|
||
def init(config) do | ||
{:ok, json} = Poison.encode( | ||
%{ | ||
project_id: "", | ||
private_key: "", | ||
} | ||
) | ||
{:ok, Keyword.put(config, :json, json)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule Materia.Files.GoogleCloudStorage do | ||
@moduledoc false | ||
alias MateriaUtils.String.StringUtil | ||
alias Materia.Errors.BusinessError | ||
|
||
@doc false | ||
def upload_file_private(params) do | ||
params | ||
|> check_file_params() | ||
|> upload([{:predefinedAcl, "private"}]) | ||
end | ||
|
||
@doc false | ||
def upload_file_public(params) do | ||
params | ||
|> check_file_params() | ||
|> upload([{:predefinedAcl, "publicread"}]) | ||
end | ||
|
||
@doc false | ||
defp check_file_params(params) do | ||
if StringUtil.is_empty(params["file"]) do | ||
raise BusinessError, message: "file not found." | ||
else | ||
params | ||
end | ||
end | ||
|
||
@doc false | ||
defp get_connection() do | ||
{:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform") | ||
GoogleApi.Storage.V1.Connection.new(token.token) | ||
end | ||
|
||
@doc false | ||
defp upload(params, optional_params) do | ||
file = params["file"] | ||
directory_name = params["directory"] | ||
file_name = | ||
if StringUtil.is_empty(directory_name) do | ||
"#{UUID.uuid4()}/#{file.filename}" | ||
else | ||
"#{directory_name}/#{UUID.uuid4()}/#{file.filename}" | ||
end | ||
{:ok, object} = GoogleApi.Storage.V1.Api.Objects.storage_objects_insert_simple( | ||
get_connection(), | ||
get_bucket_id(), | ||
"multipart", | ||
%{name: file_name, contentType: file.content_type}, | ||
file.path, | ||
optional_params | ||
) | ||
object | ||
end | ||
|
||
@doc false | ||
defp get_bucket_id() do | ||
bucket_id = Application.get_env(:materia, :bucket_id) | ||
if StringUtil.is_empty(bucket_id) do | ||
raise BusinessError, message: "bucket_id not found." | ||
else | ||
bucket_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule MateriaWeb.FileController do | ||
use MateriaWeb, :controller | ||
|
||
alias Materia.Files.GoogleCloudStorage | ||
|
||
action_fallback MateriaWeb.FallbackController | ||
|
||
def upload_file_public(conn, params) do | ||
upload = GoogleCloudStorage.upload_file_public(params) | ||
render(conn, "file.json", upload: upload) | ||
end | ||
|
||
def upload_file_private(conn, params) do | ||
upload = GoogleCloudStorage.upload_file_private(params) | ||
render(conn, "file.json", upload: upload) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule MateriaWeb.FileView do | ||
use MateriaWeb, :view | ||
|
||
def render("file.json", %{upload: upload}) do | ||
%{ | ||
name: upload.name, | ||
content_type: upload.contentType, | ||
url: "https://storage.googleapis.com/#{upload.bucket}/#{upload.name}" | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters