aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/frontend.ex37
-rw-r--r--lib/pleroma/web/controllers/frontend/default_controller.ex5
-rw-r--r--lib/pleroma/web/controllers/frontend/pleroma_controller.ex4
-rw-r--r--lib/pleroma/web/frontend_controller.ex38
-rw-r--r--lib/pleroma/web/mastodon_api/controllers/auth_controller.ex2
-rw-r--r--lib/pleroma/web/preload/instance.ex15
-rw-r--r--lib/pleroma/web/router.ex2
7 files changed, 52 insertions, 51 deletions
diff --git a/lib/pleroma/frontend.ex b/lib/pleroma/frontend.ex
index b344edd9e..941e2cfe9 100644
--- a/lib/pleroma/frontend.ex
+++ b/lib/pleroma/frontend.ex
@@ -29,4 +29,41 @@ defmodule Pleroma.Frontend do
static: fe_config[:static] || false
}
end
+
+ @doc """
+ Returns path to index.html file for the frontend from the given config.
+ If config is not provided, config for the `:primary` frontend is fetched and used.
+ If index.html file is not found for the requested frontend, the function fallback
+ to looking the file at instance static directory and then, in case of failure,
+ in priv/static directory.
+ Path returned in case of success is guaranteed to be existing file.
+ """
+ @spec fe_file_path(String.t(), map()) :: {:ok, String.t()} | {:error, String.t()}
+ def fe_file_path(filename, config \\ nil) do
+ %{"name" => name, "ref" => ref} =
+ with nil <- config do
+ get_primary_fe_opts()[:config]
+ end
+
+ instance_base_path = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
+
+ frontend_path = Path.join([instance_base_path, "frontends", name, ref, filename])
+ instance_path = Path.join([instance_base_path, filename])
+ priv_path = Application.app_dir(:pleroma, ["priv", "static", filename])
+
+ cond do
+ File.exists?(instance_path) ->
+ {:ok, instance_path}
+
+ File.exists?(frontend_path) ->
+ {:ok, frontend_path}
+
+ File.exists?(priv_path) ->
+ {:ok, priv_path}
+
+ true ->
+ {:error,
+ "File #{filename} was not found in #{inspect([instance_path, frontend_path, priv_path])}"}
+ end
+ end
end
diff --git a/lib/pleroma/web/controllers/frontend/default_controller.ex b/lib/pleroma/web/controllers/frontend/default_controller.ex
index e56288f06..fef748a19 100644
--- a/lib/pleroma/web/controllers/frontend/default_controller.ex
+++ b/lib/pleroma/web/controllers/frontend/default_controller.ex
@@ -5,11 +5,12 @@
defmodule Pleroma.Web.Frontend.DefaultController do
defmacro __using__(_opts) do
quote do
- import Pleroma.Web.FrontendController, only: [index_file_path: 0, index_file_path: 1]
+ import Pleroma.Frontend, only: [fe_file_path: 1, fe_file_path: 2]
def index(conn, _params) do
status = conn.status || 200
- {:ok, index_file_path} = index_file_path(conn.private[:frontend][:config])
+
+ {:ok, index_file_path} = fe_file_path("index.html", conn.private[:frontend][:config])
conn
|> put_resp_content_type("text/html")
diff --git a/lib/pleroma/web/controllers/frontend/pleroma_controller.ex b/lib/pleroma/web/controllers/frontend/pleroma_controller.ex
index b1286be54..2c48aeaba 100644
--- a/lib/pleroma/web/controllers/frontend/pleroma_controller.ex
+++ b/lib/pleroma/web/controllers/frontend/pleroma_controller.ex
@@ -24,7 +24,7 @@ defmodule Pleroma.Web.Frontend.PleromaController do
# not intended to be matched from router, but can be called from the app internally
def index_with_meta(conn, params) do
- {:ok, path} = index_file_path()
+ {:ok, path} = fe_file_path("index.html")
{:ok, index_content} = File.read(path)
tags =
@@ -48,7 +48,7 @@ defmodule Pleroma.Web.Frontend.PleromaController do
end
def index_with_preload(conn, params) do
- {:ok, path} = index_file_path()
+ {:ok, path} = fe_file_path("index.html")
{:ok, index_content} = File.read(path)
preloads = preload_data(conn, params)
diff --git a/lib/pleroma/web/frontend_controller.ex b/lib/pleroma/web/frontend_controller.ex
index 2ba445df9..b5dc41503 100644
--- a/lib/pleroma/web/frontend_controller.ex
+++ b/lib/pleroma/web/frontend_controller.ex
@@ -35,42 +35,4 @@ defmodule Pleroma.Web.FrontendController do
|> put_view(Phoenix.Controller.__view__(controller))
|> controller.call(controller.init(action))
end
-
- @doc """
- Returns path to index.html file for the frontend from the given config.
- If config is not provided, config for the `:primary` frontend is fetched and used.
- If index.html file is not found for the requested frontend, the function fallback
- to looking the file at instance static directory and then, in case of failure,
- in priv/static directory.
- Path returned in case of success is guaranteed to be existing file.
- """
- @spec index_file_path(Map.t()) :: {:ok, String.t()} | {:error, String.t()}
- def index_file_path(fe_config \\ nil) do
- filename = "index.html"
- instance_base_path = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
-
- %{"name" => name, "ref" => ref} =
- with nil <- fe_config do
- Pleroma.Frontend.get_primary_fe_opts()[:config]
- end
-
- frontend_path = Path.join([instance_base_path, "frontends", name, ref, filename])
- instance_path = Path.join([instance_base_path, filename])
- priv_path = Application.app_dir(:pleroma, ["priv", "static", filename])
-
- cond do
- File.exists?(instance_path) ->
- {:ok, instance_path}
-
- File.exists?(frontend_path) ->
- {:ok, frontend_path}
-
- File.exists?(priv_path) ->
- {:ok, priv_path}
-
- true ->
- {:error,
- "index.html file was not found in #{inspect([instance_path, frontend_path, priv_path])}"}
- end
- end
end
diff --git a/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex b/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex
index 2aa68c2cf..1404fcad9 100644
--- a/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex
+++ b/lib/pleroma/web/mastodon_api/controllers/auth_controller.ex
@@ -52,7 +52,7 @@ defmodule Pleroma.Web.MastodonAPI.AuthController do
def logout(conn, _) do
conn
|> clear_session()
- |> redirect(to: frontend_path(conn, :index, []))
+ |> redirect(to: frontend_path(conn, :index_with_preload, []))
end
@doc "POST /auth/password"
diff --git a/lib/pleroma/web/preload/instance.ex b/lib/pleroma/web/preload/instance.ex
index 50d1f3382..05bdb4fdb 100644
--- a/lib/pleroma/web/preload/instance.ex
+++ b/lib/pleroma/web/preload/instance.ex
@@ -3,7 +3,6 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Preload.Providers.Instance do
- alias Pleroma.Plugs.InstanceStatic
alias Pleroma.Web.MastodonAPI.InstanceView
alias Pleroma.Web.Nodeinfo.Nodeinfo
alias Pleroma.Web.Preload.Providers.Provider
@@ -28,13 +27,15 @@ defmodule Pleroma.Web.Preload.Providers.Instance do
end
defp build_panel_tag(acc) do
- instance_path = InstanceStatic.file_path(@panel_url |> to_string())
+ panel_file = Path.basename(@panel_url)
- if File.exists?(instance_path) do
- panel_data = File.read!(instance_path)
- Map.put(acc, @panel_url, panel_data)
- else
- acc
+ case Pleroma.Frontend.fe_file_path(panel_file) do
+ {:ok, instance_path} ->
+ panel_data = File.read!(instance_path)
+ Map.put(acc, @panel_url, panel_data)
+
+ {:error, _e} ->
+ acc
end
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index e25ffd317..42b3bd864 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -721,7 +721,7 @@ defmodule Pleroma.Web.Router do
get("/registration/:token", FrontendController, :registration_page)
get("/:maybe_nickname_or_id", FrontendController, :index_with_meta)
get("/api*path", FrontendController, :api_not_implemented)
- get("/*path", FrontendController, :index)
+ get("/*path", FrontendController, :index_with_preload)
options("/*path", FrontendController, :empty)
end