1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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")
35 IO.puts("Downloading #{label}...")
38 {:download_or_unzip, download_or_unzip(frontend_info, tmp_dir, opts[:file])},
39 IO.puts("Installing #{label} to #{dest}"),
40 :ok <- install_frontend(frontend_info, tmp_dir, dest) do
42 IO.puts("Frontend #{label} installed to #{dest}")
44 {:download_or_unzip, _} ->
45 IO.puts("Could not download or unzip the frontend")
46 {:error, "Could not download or unzip the frontend"}
49 IO.puts("Could not install the frontend")
50 {:error, "Could not install the frontend"}
54 def dir(opts \\ []) do
55 if is_nil(opts[:static_dir]) do
56 Pleroma.Config.get!([:instance, :static_dir])
60 |> Path.join("frontends")
63 defp download_or_unzip(frontend_info, temp_dir, nil),
64 do: download_build(frontend_info, temp_dir)
66 defp download_or_unzip(_frontend_info, temp_dir, file) do
67 with {:ok, zip} <- File.read(Path.expand(file)) do
72 def unzip(zip, dest) do
73 with {:ok, unzipped} <- :zip.unzip(zip, [:memory]) do
77 Enum.each(unzipped, fn {filename, data} ->
80 new_file_path = Path.join(dest, path)
86 File.write!(new_file_path, data)
91 defp download_build(frontend_info, dest) do
92 Logger.info("Downloading pre-built bundle for #{frontend_info["name"]}")
93 url = String.replace(frontend_info["build_url"], "${ref}", frontend_info["ref"])
95 with {:ok, %{status: 200, body: zip_body}} <-
96 Pleroma.HTTP.get(url, [], receive_timeout: 120_000) do
99 {:error, e} -> {:error, e}
104 defp install_frontend(frontend_info, source, dest) do
105 from = frontend_info["build_dir"] || "dist"
108 File.cp_r!(Path.join([source, from]), dest)