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

[Aeweb] Incorporate new developments in ref txn #742

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions lib/archethic_web/controllers/api/web_hosting_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ defmodule ArchethicWeb.API.WebHostingController do

defp do_web_hosting(conn, params) do
case get_website(params, get_cache_headers(conn)) do
{:ok, file_content, encodage, mime_type, cached?, etag} ->
send_response(conn, file_content, encodage, mime_type, cached?, etag)
{:ok, file_content, encoding, mime_type, cached?, etag} ->
send_response(conn, file_content, encoding, mime_type, cached?, etag)

{:error, :invalid_address} ->
send_resp(conn, 400, "Invalid address")
Expand All @@ -43,14 +43,14 @@ defmodule ArchethicWeb.API.WebHostingController do
{:error, :file_not_found} ->
send_resp(conn, 404, "Cannot find file content")

{:error, :invalid_encodage} ->
send_resp(conn, 400, "Invalid file encodage")
{:error, :invalid_encoding} ->
send_resp(conn, 400, "Invalid file encoding")

{:error, {:is_a_directory, transaction}} ->
{:ok, listing_html, encodage, mime_type, cached?, etag} =
{:ok, listing_html, encoding, mime_type, cached?, etag} =
dir_listing(conn.request_path, params, transaction, get_cache_headers(conn))

send_response(conn, listing_html, encodage, mime_type, cached?, etag)
send_response(conn, listing_html, encoding, mime_type, cached?, etag)

{:error, _} ->
send_resp(conn, 404, "Not Found")
Expand All @@ -61,13 +61,13 @@ defmodule ArchethicWeb.API.WebHostingController do
Fetch the website file content
"""
@spec get_website(request_params :: map(), cached_headers :: list()) ::
{:ok, file_content :: binary() | nil, encodage :: binary() | nil, mime_type :: binary(),
{:ok, file_content :: binary() | nil, encoding :: binary() | nil, mime_type :: binary(),
cached? :: boolean(), etag :: binary()}
| {:error, :invalid_address}
| {:error, :invalid_content}
| {:error, :file_not_found}
| {:error, :is_a_directory}
| {:error, :invalid_encodage}
| {:error, :invalid_encoding}
| {:error, any()}
def get_website(params = %{"address" => address}, cache_headers) do
url_path = Map.get(params, "url_path", [])
Expand All @@ -81,13 +81,14 @@ defmodule ArchethicWeb.API.WebHostingController do
}} <-
Archethic.get_last_transaction(address) do
with {:ok, json_content} <- Jason.decode(content),
{:ok, file, mime_type} <- get_file(json_content, url_path),
{:ok, metadata, _aeweb_version} <- get_metadata(json_content),
{:ok, file, mime_type} <- get_file(metadata, url_path),
{cached?, etag} <- get_cache(cache_headers, last_address, url_path),
{:ok, file_content, encodage} <- get_file_content(file, cached?, url_path) do
{:ok, file_content, encodage, mime_type, cached?, etag}
{:ok, file_content, encoding} <- get_file_content(file, cached?, url_path) do
{:ok, file_content, encoding, mime_type, cached?, etag}
else
:encodage_error ->
{:error, :invalid_encodage}
:encoding_error ->
{:error, :invalid_encoding}

:file_error ->
{:error, :file_not_found}
Expand Down Expand Up @@ -118,13 +119,17 @@ defmodule ArchethicWeb.API.WebHostingController do
end
end

def get_metadata(%{"metaData" => metadata, "aewebVersion" => aewebversion}) do
{:ok, metadata, aewebversion}
end

@spec dir_listing(
request_path :: String.t(),
params :: map(),
transaction :: Transaction.t(),
cached_headers :: list()
) ::
{:ok, listing_html :: binary() | nil, encodage :: nil | binary(), mime_type :: binary(),
{:ok, listing_html :: binary() | nil, encoding :: nil | binary(), mime_type :: binary(),
cached? :: boolean(), etag :: binary()}
def dir_listing(
request_path,
Expand All @@ -150,7 +155,13 @@ defmodule ArchethicWeb.API.WebHostingController do

{:ok, json_content} ->
assigns =
do_dir_listing(request_path, url_path, json_content, timestamp, last_address)
do_dir_listing(
request_path,
url_path,
elem(get_metadata(json_content), 1),
timestamp,
last_address
)

{:ok,
Phoenix.View.render_to_iodata(ArchethicWeb.DirListingView, "index.html", assigns),
Expand All @@ -177,8 +188,8 @@ defmodule ArchethicWeb.API.WebHostingController do

json_content_subset
|> Enum.map(fn
{key, %{"address" => address}} ->
{:file, key, address}
{key, %{"addresses" => addresses}} ->
{:file, key, addresses}

{key, _} ->
{:dir, key}
Expand Down Expand Up @@ -247,13 +258,13 @@ defmodule ArchethicWeb.API.WebHostingController do
@spec send_response(
Plug.Conn.t(),
file_content :: binary() | nil,
encodage :: binary() | nil,
encoding :: binary() | nil,
mime_type :: binary(),
cached? :: boolean(),
etag :: binary()
) ::
Plug.Conn.t()
def send_response(conn, file_content, encodage, mime_type, cached?, etag) do
def send_response(conn, file_content, encoding, mime_type, cached?, etag) do
conn =
conn
|> put_resp_content_type(mime_type, "utf-8")
Expand All @@ -263,21 +274,21 @@ defmodule ArchethicWeb.API.WebHostingController do
if cached? do
send_resp(conn, 304, "")
else
{conn, response_content} = encode_res(conn, file_content, encodage)
{conn, response_content} = encode_res(conn, file_content, encoding)

send_resp(conn, 200, response_content)
end
end

defp encode_res(conn, file_content, encodage) do
defp encode_res(conn, file_content, encoding) do
if Enum.any?(get_req_header(conn, "accept-encoding"), &String.contains?(&1, "gzip")) do
res_conn = put_resp_header(conn, "content-encoding", "gzip")

if encodage == "gzip",
if encoding == "gzip",
do: {res_conn, file_content},
else: {res_conn, :zlib.gzip(file_content)}
else
if encodage == "gzip",
if encoding == "gzip",
do: {conn, :zlib.gunzip(file_content)},
else: {conn, file_content}
end
Expand All @@ -286,7 +297,7 @@ defmodule ArchethicWeb.API.WebHostingController do
defp get_file(json_content, path), do: get_file(json_content, path, nil)

# case when we're parsing a reference tx
defp get_file(file = %{"address" => _}, [], previous_path_item) do
defp get_file(file = %{"addresses" => _}, [], previous_path_item) do
{:ok, file, MIME.from_path(previous_path_item)}
end

Expand Down Expand Up @@ -358,7 +369,7 @@ defmodule ArchethicWeb.API.WebHostingController do
# All file are encoded in base64 in JSON content
defp get_file_content(_file, _cached? = true, _url_path), do: {:ok, nil, nil}

defp get_file_content(file = %{"address" => address_list}, _cached? = false, url_path) do
defp get_file_content(file = %{"addresses" => address_list}, _cached? = false, url_path) do
try do
content =
Enum.map_join(address_list, fn tx_address ->
Expand All @@ -372,11 +383,11 @@ defmodule ArchethicWeb.API.WebHostingController do
end)

file_content = Base.url_decode64!(content, padding: false)
encodage = Map.get(file, "encodage")
{:ok, file_content, encodage}
encoding = Map.get(file, "encoding")
{:ok, file_content, encoding}
rescue
ArgumentError ->
:encodage_error
:encoding_error

error ->
error
Expand Down
14 changes: 7 additions & 7 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
"credo": {:hex, :credo, "1.6.7", "323f5734350fd23a456f2688b9430e7d517afb313fbd38671b8a4449798a7854", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "41e110bfb007f7eda7f897c10bf019ceab9a0b269ce79f015d54b0dcf4fc7dd3"},
"crontab": {:hex, :crontab, "1.1.11", "4028ced51b813a5061f85b689d4391ef0c27550c8ab09aaf139e4295c3d93ea4", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "ecb045f9ac14a3e2990e54368f70cdb6e2f2abafc5bc329d6c31f0c74b653787"},
"crontab": {:hex, :crontab, "1.1.13", "3bad04f050b9f7f1c237809e42223999c150656a6b2afbbfef597d56df2144c5", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "d67441bec989640e3afb94e123f45a2bc42d76e02988c9613885dc3d01cf7085"},
"dart_sass": {:hex, :dart_sass, "0.5.1", "d45f20a8e324313689fb83287d4702352793ce8c9644bc254155d12656ade8b6", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "24f8a1c67e8b5267c51a33cbe6c0b5ebf12c2c83ace88b5ac04947d676b4ec81"},
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
"dialyxir": {:hex, :dialyxir, "1.2.0", "58344b3e87c2e7095304c81a9ae65cb68b613e28340690dfe1a5597fd08dec37", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "61072136427a851674cab81762be4dbeae7679f85b1272b6d25c3a839aff8463"},
"distillery": {:git, "https://github.com/archethic-foundation/distillery.git", "67accaa239dcbe14fc312832c83b23eaaeed66ff", []},
"earmark": {:hex, :earmark, "1.4.33", "2b33a505180583f98bfa17317f03973b52081bdb24a11be05a7f4fa6d64dd8bf", [:mix], [{:earmark_parser, "~> 1.4.29", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "21b31363d6a0a70802cfbaf2de88355778aa76654298a072bce2e01d1858ae06"},
"earmark": {:hex, :earmark, "1.4.34", "d7f89d3bbd7567a0bffc465e0a949f8f8dcbe43909c3acf96f4761a302cea10c", [:mix], [{:earmark_parser, "~> 1.4.29", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "90b106f3dad85b133b10d7d628167c88246123fd1cecb4557d83d21ec9e65504"},
"earmark_parser": {:hex, :earmark_parser, "1.4.29", "149d50dcb3a93d9f3d6f3ecf18c918fb5a2d3c001b5d3305c926cddfbd33355b", [:mix], [], "hexpm", "4902af1b3eb139016aed210888748db8070b8125c2342ce3dcae4f38dcc63503"},
"easy_ssl": {:hex, :easy_ssl, "1.3.0", "472256942d9dd37652a558a789a8d1cccc27e7f46352e32667d1ca46bb9e22e5", [:mix], [], "hexpm", "ce8fcb7661442713a94853282b56cee0b90c52b983a83aa6af24686d301808e1"},
"ecto": {:hex, :ecto, "3.9.1", "67173b1687afeb68ce805ee7420b4261649d5e2deed8fe5550df23bab0bc4396", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c80bb3d736648df790f7f92f81b36c922d9dd3203ca65be4ff01d067f54eb304"},
"elixir_make": {:hex, :elixir_make, "0.6.3", "bc07d53221216838d79e03a8019d0839786703129599e9619f4ab74c8c096eac", [:mix], [], "hexpm", "f5cbd651c5678bcaabdbb7857658ee106b12509cd976c2c2fca99688e1daf716"},
"ecto": {:hex, :ecto, "3.9.2", "017db3bc786ff64271108522c01a5d3f6ba0aea5c84912cfb0dd73bf13684108", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "21466d5177e09e55289ac7eade579a642578242c7a3a9f91ad5c6583337a9d15"},
"elixir_make": {:hex, :elixir_make, "0.7.0", "03e6a43ac701a2afee73bb5dd030b4dcddcb403bf81abb4753c1da64521cd05d", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "b1ccb45cc1c5df16349473b52adbd718f362585a5481d761b8a9fdb45f25b303"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"esbuild": {:hex, :esbuild, "0.5.0", "d5bb08ff049d7880ee3609ed5c4b864bd2f46445ea40b16b4acead724fb4c4a3", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "f183a0b332d963c4cfaf585477695ea59eef9a6f2204fdd0efa00e099694ffe5"},
"ex_doc": {:hex, :ex_doc, "0.29.0", "4a1cb903ce746aceef9c1f9ae8a6c12b742a5461e6959b9d3b24d813ffbea146", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "f096adb8bbca677d35d278223361c7792d496b3fc0d0224c9d4bc2f651af5db1"},
"ex_doc": {:hex, :ex_doc, "0.29.1", "b1c652fa5f92ee9cf15c75271168027f92039b3877094290a75abcaac82a9f77", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "b7745fa6374a36daf484e2a2012274950e084815b936b1319aeebcf7809574f6"},
"ex_json_schema": {:hex, :ex_json_schema, "0.9.2", "c9a42e04e70cd70eb11a8903a22e8ec344df16edef4cb8e6ec84ed0caffc9f0f", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "4854329cb352b6c01c4c4b8dbfb3be14dc5bea19ea13e0eafade4ff22ba55224"},
"exjsonpath": {:hex, :exjsonpath, "0.9.0", "87e593eb0deb53aa0688ca9f9edc9fb3456aca83c82245f83201ea04d696feba", [:mix], [], "hexpm", "8d7a8e9ba784e1f7a67c6f1074a3ac91a3a79a45969514ee5d95cea5bf749627"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
Expand All @@ -51,8 +51,8 @@
"mmdb2_decoder": {:hex, :mmdb2_decoder, "3.0.1", "78e3aedde88035c6873ada5ceaf41b7f15a6259ed034e0eaca72ccfa937798f0", [:mix], [], "hexpm", "316af0f388fac824782d944f54efe78e7c9691bbbdb0afd5cccdd0510adf559d"},
"mox": {:hex, :mox, "1.0.2", "dc2057289ac478b35760ba74165b4b3f402f68803dd5aecd3bfd19c183815d64", [:mix], [], "hexpm", "f9864921b3aaf763c8741b5b8e6f908f44566f1e427b2630e89e9a73b981fef2"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"observer_cli": {:hex, :observer_cli, "1.7.3", "25d094d485f47239f218b53df0691a102fef13071dfd0d04922b5142297cfc93", [:mix, :rebar3], [{:recon, "~>2.5.1", [hex: :recon, repo: "hexpm", optional: false]}], "hexpm", "a41b6d3e11a3444e063e09cc225f7f3e631ce14019e5fbcaebfda89b1bd788ea"},
"pathex": {:hex, :pathex, "2.4.2", "6b3f25abfd22c2aa2b97ace38fbcd428c97f19910117c17a36a0e9a75908ebfb", [:mix], [], "hexpm", "1c86738ac3edf99ee77a61a3f8438cc80d2d5ce009ec3ef3e336726b6a9499ea"},
"observer_cli": {:hex, :observer_cli, "1.7.4", "3c1bfb6d91bf68f6a3d15f46ae20da0f7740d363ee5bc041191ce8722a6c4fae", [:mix, :rebar3], [{:recon, "~> 2.5.1", [hex: :recon, repo: "hexpm", optional: false]}], "hexpm", "50de6d95d814f447458bd5d72666a74624eddb0ef98bdcee61a0153aae0865ff"},
"pathex": {:hex, :pathex, "2.4.3", "34e807e4cc48923dd621a10fb6062198f3d31c9652b652de3de0074ba4ec1678", [:mix], [], "hexpm", "10e9f2b0d2761d7110d668db0f848c5b4cf10b8429521172fef7948deebcfa50"},
"phoenix": {:hex, :phoenix, "1.6.15", "0a1d96bbc10747fd83525370d691953cdb6f3ccbac61aa01b4acb012474b047d", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 1.0 or ~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d70ab9fbf6b394755ea88b644d34d79d8b146e490973151f248cacd122d20672"},
"phoenix_html": {:hex, :phoenix_html, "3.2.0", "1c1219d4b6cb22ac72f12f73dc5fad6c7563104d083f711c3fcd8551a1f4ae11", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "36ec97ba56d25c0136ef1992c37957e4246b649d620958a1f9fa86165f8bc54f"},
"phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.7.2", "97cc4ff2dba1ebe504db72cb45098cb8e91f11160528b980bd282cc45c73b29c", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.5", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.18.3", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6 or ~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "0e5fdf063c7a3b620c566a30fcf68b7ee02e5e46fe48ee46a6ec3ba382dc05b7"},
Expand Down
Loading