frontends dir

parent bf95bf34
Pipeline #23619 passed with stages
in 5 minutes and 16 seconds
......@@ -233,6 +233,7 @@
quarantined_instances: [],
managed_config: true,
static_dir: "instance/static/",
frontends_dir: "instance/frontends/",
allowed_post_formats: [
"text/plain",
"text/html",
......
......@@ -732,6 +732,14 @@
"instance/static/"
]
},
%{
key: :frontends_dir,
type: :string,
description: "Instance frontends directory",
suggestions: [
"instance/frontends/"
]
},
%{
key: :allowed_post_formats,
type: {:list, :string},
......
......@@ -33,7 +33,8 @@
skip_thread_containment: false,
federating: false,
external_user_synchronization: false,
static_dir: "test/instance_static/"
static_dir: "test/instance_static/",
frontends_dir: "test/instance_static/frontends"
config :pleroma, :activitypub, sign_object_fetches: false
......
......@@ -4,7 +4,7 @@
## Download the latest frontend
This downloads a snapshot of the latest Pleroma-FE for a given reference and writes it to the `static_dir`. In a default setup, this means that this snapshot will be served as the frontend by the backend.
This downloads a snapshot of the latest Pleroma-FE for a given reference and writes it to the `frontends_dir`. In a default setup, this means that this snapshot will be served as the frontend by the backend.
```sh tab="OTP"
./bin/pleroma_ctl pleroma.frontend download [<options>]
......
......@@ -4,7 +4,7 @@
defmodule Mix.Tasks.Pleroma.Frontend do
use Mix.Task
alias __MODULE__.Fetcher
import Mix.Pleroma
@shortdoc "Manages the Pleroma frontends"
@moduledoc File.read!("docs/administration/CLI_tasks/frontend.md")
......@@ -15,21 +15,36 @@ def run(["download" | rest]) do
rest,
strict: [
reference: :string
]
],
aliases: [r: :reference]
)
reference = options[:reference] || "master"
IO.puts("Downloading reference #{reference}")
shell_info("Downloading reference #{reference}")
url =
"https://git.pleroma.social/pleroma/pleroma-fe/-/jobs/artifacts/#{reference}/download?job=build"
sd = Pleroma.Config.get([:instance, :static_dir]) |> Path.expand()
sd =
Pleroma.Config.get([:instance, :frontends_dir], "instance/frontends")
|> Path.join("pleroma-fe")
|> Path.expand()
with {_, {:ok, %{status: 200, body: body}}} <- {:fetch, Fetcher.get(url)},
{_, {:ok, results}} <- {:unzip, :zip.unzip(body, [:memory])} do
IO.puts("Writing to #{sd}")
adapter =
if Pleroma.Config.get(:env) == :test do
Tesla.Mock
else
Tesla.Adapter.Httpc
end
client = Tesla.client([Tesla.Middleware.FollowRedirects], adapter)
with {_, {:ok, %{status: 200, body: body}}} <- {:fetch, Tesla.get(client, url)},
{_, {:ok, results}} <- {:unzip, :zip.unzip(body, [:memory])},
shell_info("Cleaning #{sd}"),
{_, {:ok, _}} <- {:clean_up, File.rm_rf(sd)} do
shell_info("Writing to #{sd}")
results
|> Enum.each(fn {path, contents} ->
......@@ -39,16 +54,9 @@ def run(["download" | rest]) do
File.write!(path, contents)
end)
IO.puts("Successfully downloaded and unpacked the frontend")
shell_info("Successfully downloaded and unpacked the frontend")
else
{error, _} -> IO.puts("Step failed: #{error}")
{error, _} -> shell_error("Step failed: #{error}")
end
end
end
defmodule Mix.Tasks.Pleroma.Frontend.Fetcher do
use Tesla
plug(Tesla.Middleware.FollowRedirects)
adapter(Tesla.Adapter.Httpc)
end
......@@ -10,19 +10,25 @@ defmodule Pleroma.Plugs.InstanceStatic do
"""
@behaviour Plug
alias Pleroma.Config
def file_path(path) do
instance_path =
Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path)
instance_path = Path.join(Config.get([:instance, :static_dir], "instance/static/"), path)
frontends_path =
Config.get([:instance, :frontends_dir], "instance/frontends/")
|> Path.join("pleroma-fe")
|> Path.join(path)
if File.exists?(instance_path) do
instance_path
else
Path.join(Application.app_dir(:pleroma, "priv/static/"), path)
cond do
File.exists?(instance_path) -> instance_path
File.exists?(frontends_path) -> frontends_path
true -> Path.join(Application.app_dir(:pleroma, "priv/static/"), path)
end
end
@only ~w(index.html robots.txt static emoji packs sounds images instance favicon.png sw.js
sw-pleroma.js)
sw-pleroma.js fontello)
def init(opts) do
opts
......@@ -38,8 +44,7 @@ def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do
call_static(
conn,
opts,
unquote(at),
Pleroma.Config.get([:instance, :static_dir], "instance/static")
unquote(at)
)
end
end
......@@ -48,7 +53,20 @@ def call(conn, _) do
conn
end
defp call_static(conn, opts, at, from) do
defp call_static(conn, opts, at) do
static_dir = Config.get([:instance, :static_dir], "instance/static/")
frontend_dir =
Config.get([:instance, :frontends_dir], "instance/frontends/")
|> Path.join("pleroma-fe")
from =
if File.exists?(Path.join(static_dir, conn.request_path)) do
static_dir
else
frontend_dir
end
opts =
opts
|> Map.put(:from, from)
......
defmodule Mix.Tasks.Pleroma.FrontendTest do
use ExUnit.Case
import Tesla.Mock
@path Pleroma.Config.get([:instance, :frontends_dir])
setup do
Mix.shell(Mix.Shell.Process)
mock(fn
%{
method: :get,
url:
"https://git.pleroma.social/pleroma/pleroma-fe/-/jobs/artifacts/master/download?job=build"
} ->
%Tesla.Env{status: 200, body: File.read!("test/instance_static/dist.zip")}
%{
method: :get,
url:
"https://git.pleroma.social/pleroma/pleroma-fe/-/jobs/artifacts/develop/download?job=build"
} ->
%Tesla.Env{status: 200, body: File.read!("test/instance_static/dist.zip")}
end)
on_exit(fn ->
Mix.shell(Mix.Shell.IO)
{:ok, _} = File.rm_rf(@path)
end)
:ok
end
test "downloads pleroma-fe and master by default" do
Mix.Tasks.Pleroma.Frontend.run(["download"])
@path |> Path.expand() |> Path.join("pleroma-fe") |> check_assertions("master")
end
test "download special fe with reference" do
ref = "develop"
Mix.Tasks.Pleroma.Frontend.run(["download", "-r", ref])
@path |> Path.expand() |> Path.join("pleroma-fe") |> check_assertions(ref)
end
defp check_assertions(path, ref) do
assert_receive {:mix_shell, :info, [message]}
assert message == "Downloading reference #{ref}"
assert_receive {:mix_shell, :info, [message]}
assert message == "Cleaning #{path}"
assert_receive {:mix_shell, :info, [message]}
assert message == "Writing to #{path}"
assert_receive {:mix_shell, :info, ["Successfully downloaded and unpacked the frontend"]}
assert File.exists?(path <> "/1.png")
assert File.exists?(path <> "/2.css")
assert File.exists?(path <> "/3.js")
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment