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

Feldspar s3 page not found #485

Merged
merged 15 commits into from
Nov 24, 2023
Merged
19 changes: 7 additions & 12 deletions core/config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import Config

# Setup for MinIO
config :ex_aws, :s3,
scheme: "http://",
host: "localhost",
port: 9000

# Only in tests, remove the complexity from the password hashing algorithm
config :bcrypt_elixir, :log_rounds, 1

Expand Down Expand Up @@ -83,12 +77,13 @@ config :core,
:admins,
["[email protected]"]

config :core, :s3, bucket: "eylixir"

# For Minio (local S3)
config :ex_aws,
access_key_id: "my_access_key",
secret_access_key: "a_super_secret"
# # For Minio (local S3)
# config :ex_aws,
# scheme: "http://",
# host: "localhost",
# port: 9000
# access_key_id: "my_access_key",
# secret_access_key: "a_super_secret"

config :core, :feldspar,
backend: Systems.Feldspar.LocalFS,
Expand Down
2 changes: 1 addition & 1 deletion core/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ if config_env() == :prod do
config :core, :feldspar,
backend: Systems.Feldspar.S3,
bucket: System.get_env("FELDSPAR_S3_BUCKET"),
prefix: System.get_env("FELDSPAR_S3_PREFIX", ""),
prefix: System.get_env("FELDSPAR_S3_PREFIX", nil),
# The public URL must point to the root's (bucket) publicly accessible URL.
# It should have a policy that allows anonymous users to read all files.
public_url: System.get_env("FELDSPAR_S3_PUBLIC_URL")
Expand Down
1 change: 1 addition & 0 deletions core/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ defmodule Core.MixProject do
{:csv, "~> 2.4"},
{:sentry, "~> 8.0"},
{:libcluster, "~> 3.3"},
{:mime, "~> 2.0"},
# i18n
{:ex_cldr, "~> 2.25"},
{:ex_cldr_numbers, "~> 2.23"},
Expand Down
5 changes: 4 additions & 1 deletion core/systems/assignment/start_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ defmodule Systems.Assignment.StartView do
def compose(:description, %{work_item: {%{description: description}, _}}), do: description

@impl true
def compose(:icon, %{work_item: {%{group: group}, _}}), do: group
def compose(:icon, %{work_item: {%{group: nil}, _}}), do: nil

@impl true
def compose(:icon, %{work_item: {%{group: group}, _}}), do: String.downcase(group)

defp start_action({%{tool_ref: tool_ref}, _task} = item) do
Project.ToolRefModel.tool(tool_ref)
Expand Down
3 changes: 3 additions & 0 deletions core/systems/feldspar/app_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ defmodule Systems.Feldspar.AppPage do
use CoreWeb, :live_view
use CoreWeb.Layouts.Stripped.Component, :projects

require Logger

import Feldspar.AppView

@impl true
def mount(%{"id" => app_id}, _session, socket) do
app_url = Feldspar.Public.get_public_url(app_id) <> "/index.html"
Logger.info("[Feldspar.AppPage] Starting feldspar app from: #{app_url}")

{
:ok,
Expand Down
18 changes: 13 additions & 5 deletions core/systems/feldspar/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule Systems.Feldspar.S3 do
def get_public_url(id) do
settings = s3_settings()
public_url = Access.get(settings, :public_url)
"#{public_url}/#{object_key(id)}/index.html"
"#{public_url}/#{object_key(id)}"
end

def remove(id) do
Expand All @@ -36,7 +36,9 @@ defmodule Systems.Feldspar.S3 do

contents
|> Enum.map(fn {:zip_file, file, info, _, _, _} -> {file, info} end)
|> Task.async_stream(&upload_file(&1, zip_handle, target, s3_settings()), max_concurrency: 10)
|> Task.async_stream(&upload_file(&1, zip_handle, target, s3_settings()),
max_concurrency: 10
)
|> Stream.run()
end

Expand All @@ -47,12 +49,15 @@ defmodule Systems.Feldspar.S3 do
S3.put_object(
Access.fetch!(settings, :bucket),
"#{object_key(target)}/#{name}",
data
data,
content_type: content_type(name)
)
|> backend().request!()
end
end

defp content_type(name), do: MIME.from_path(name)

@doc """
See: https://www.erlang.org/doc/man/file#type-file_info
Files types: device | directory | other | regular | symlink | undefined
Expand All @@ -62,8 +67,11 @@ defmodule Systems.Feldspar.S3 do
def is_regular_file(_), do: false

defp object_key(id) do
prefix = Access.get(s3_settings(), :prefix, "")
"#{prefix}#{id}"
prefix = Access.get(s3_settings(), :prefix, nil)

[prefix, id]
|> Enum.filter(&(&1 != nil))
|> Enum.join("/")
end

defp s3_settings do
Expand Down
4 changes: 2 additions & 2 deletions core/test/systems/feldspar/s3_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ defmodule Systems.Feldspar.S3Test do
end
end

describe "get_public_url/1" do
describe "get_public_url/1 (without prefix)" do
test "returns URL" do
id = Ecto.UUID.generate()
url = S3.get_public_url(id)
assert "http://example.com/#{id}/index.html" == url
assert "http://example.com/#{id}" == url
end
end

Expand Down
Loading