1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Frontend do
10 def install(name, opts \\ []) do
13 "build_url" => opts[:build_url],
14 "build_dir" => opts[:build_dir]
18 [:frontends, :available, name]
20 |> Map.merge(frontend_info, fn _key, config, cmd ->
21 # This only overrides things that are actually set
25 ref = frontend_info["ref"]
28 raise "No ref given or configured"
31 dest = Path.join([dir(), name, ref])
33 label = "#{name} (#{ref})"
34 tmp_dir = Path.join(dir(), "tmp")
37 {:download_or_unzip, download_or_unzip(frontend_info, tmp_dir, opts[:file])},
38 Logger.info("Installing #{label} to #{dest}"),
39 :ok <- install_frontend(frontend_info, tmp_dir, dest) do
41 Logger.info("Frontend #{label} installed to #{dest}")
43 {:download_or_unzip, _} ->
44 Logger.info("Could not download or unzip the frontend")
45 {:error, "Could not download or unzip the frontend"}
48 Logger.info("Could not install the frontend")
49 {:error, "Could not install the frontend"}
53 def dir(opts \\ []) do
54 if is_nil(opts[:static_dir]) do
55 Pleroma.Config.get!([:instance, :static_dir])
59 |> Path.join("frontends")
62 defp download_or_unzip(frontend_info, temp_dir, nil),
63 do: download_build(frontend_info, temp_dir)
65 defp download_or_unzip(_frontend_info, temp_dir, file) do
66 with {:ok, zip} <- File.read(Path.expand(file)) do
71 def unzip(zip, dest) do
72 with {:ok, unzipped} <- :zip.unzip(zip, [:memory]) do
76 Enum.each(unzipped, fn {filename, data} ->
79 new_file_path = Path.join(dest, path)
85 File.write!(new_file_path, data)
90 defp download_build(frontend_info, dest) do
91 Logger.info("Downloading pre-built bundle for #{frontend_info["name"]}")
92 url = String.replace(frontend_info["build_url"], "${ref}", frontend_info["ref"])
94 with {:ok, %{status: 200, body: zip_body}} <-
95 Pleroma.HTTP.get(url, [], pool: :media, recv_timeout: 120_000) do
98 {:error, e} -> {:error, e}
103 defp install_frontend(frontend_info, source, dest) do
104 from = frontend_info["build_dir"] || "dist"
107 File.cp_r!(Path.join([source, from]), dest)