logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

frontend.ex (2976B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Frontend do
  5. alias Pleroma.Config
  6. require Logger
  7. def install(name, opts \\ []) do
  8. frontend_info = %{
  9. "ref" => opts[:ref],
  10. "build_url" => opts[:build_url],
  11. "build_dir" => opts[:build_dir]
  12. }
  13. frontend_info =
  14. [:frontends, :available, name]
  15. |> Config.get(%{})
  16. |> Map.merge(frontend_info, fn _key, config, cmd ->
  17. # This only overrides things that are actually set
  18. cmd || config
  19. end)
  20. ref = frontend_info["ref"]
  21. unless ref do
  22. raise "No ref given or configured"
  23. end
  24. dest = Path.join([dir(), name, ref])
  25. label = "#{name} (#{ref})"
  26. tmp_dir = Path.join(dir(), "tmp")
  27. with {_, :ok} <-
  28. {:download_or_unzip, download_or_unzip(frontend_info, tmp_dir, opts[:file])},
  29. Logger.info("Installing #{label} to #{dest}"),
  30. :ok <- install_frontend(frontend_info, tmp_dir, dest) do
  31. File.rm_rf!(tmp_dir)
  32. Logger.info("Frontend #{label} installed to #{dest}")
  33. else
  34. {:download_or_unzip, _} ->
  35. Logger.info("Could not download or unzip the frontend")
  36. {:error, "Could not download or unzip the frontend"}
  37. _e ->
  38. Logger.info("Could not install the frontend")
  39. {:error, "Could not install the frontend"}
  40. end
  41. end
  42. def dir(opts \\ []) do
  43. if is_nil(opts[:static_dir]) do
  44. Pleroma.Config.get!([:instance, :static_dir])
  45. else
  46. opts[:static_dir]
  47. end
  48. |> Path.join("frontends")
  49. end
  50. defp download_or_unzip(frontend_info, temp_dir, nil),
  51. do: download_build(frontend_info, temp_dir)
  52. defp download_or_unzip(_frontend_info, temp_dir, file) do
  53. with {:ok, zip} <- File.read(Path.expand(file)) do
  54. unzip(zip, temp_dir)
  55. end
  56. end
  57. def unzip(zip, dest) do
  58. with {:ok, unzipped} <- :zip.unzip(zip, [:memory]) do
  59. File.rm_rf!(dest)
  60. File.mkdir_p!(dest)
  61. Enum.each(unzipped, fn {filename, data} ->
  62. path = filename
  63. new_file_path = Path.join(dest, path)
  64. new_file_path
  65. |> Path.dirname()
  66. |> File.mkdir_p!()
  67. File.write!(new_file_path, data)
  68. end)
  69. end
  70. end
  71. defp download_build(frontend_info, dest) do
  72. Logger.info("Downloading pre-built bundle for #{frontend_info["name"]}")
  73. url = String.replace(frontend_info["build_url"], "${ref}", frontend_info["ref"])
  74. with {:ok, %{status: 200, body: zip_body}} <-
  75. Pleroma.HTTP.get(url, [], pool: :media, recv_timeout: 120_000) do
  76. unzip(zip_body, dest)
  77. else
  78. {:error, e} -> {:error, e}
  79. e -> {:error, e}
  80. end
  81. end
  82. defp install_frontend(frontend_info, source, dest) do
  83. from = frontend_info["build_dir"] || "dist"
  84. File.rm_rf!(dest)
  85. File.mkdir_p!(dest)
  86. File.cp_r!(Path.join([source, from]), dest)
  87. :ok
  88. end
  89. end