logo

pleroma

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

instance_document.ex (1997B)


  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.Web.InstanceDocument do
  5. alias Pleroma.Config
  6. alias Pleroma.Web.Endpoint
  7. @instance_documents %{
  8. "terms-of-service" => "/static/terms-of-service.html",
  9. "instance-panel" => "/instance/panel.html"
  10. }
  11. @spec get(String.t()) :: {:ok, String.t()} | {:error, atom()}
  12. def get(document_name) do
  13. case Map.fetch(@instance_documents, document_name) do
  14. {:ok, path} -> {:ok, path}
  15. _ -> {:error, :not_found}
  16. end
  17. end
  18. @spec put(String.t(), String.t()) :: {:ok, String.t()} | {:error, atom()}
  19. def put(document_name, origin_path) do
  20. with {_, {:ok, destination_path}} <-
  21. {:instance_document, Map.fetch(@instance_documents, document_name)},
  22. :ok <- put_file(origin_path, destination_path) do
  23. {:ok, Path.join(Endpoint.url(), destination_path)}
  24. else
  25. {:instance_document, :error} -> {:error, :not_found}
  26. error -> error
  27. end
  28. end
  29. @spec delete(String.t()) :: :ok | {:error, atom()}
  30. def delete(document_name) do
  31. with {_, {:ok, path}} <- {:instance_document, Map.fetch(@instance_documents, document_name)},
  32. instance_static_dir_path <- instance_static_dir(path),
  33. :ok <- File.rm(instance_static_dir_path) do
  34. :ok
  35. else
  36. {:instance_document, :error} -> {:error, :not_found}
  37. {:error, :enoent} -> {:error, :not_found}
  38. error -> error
  39. end
  40. end
  41. defp put_file(origin_path, destination_path) do
  42. with destination <- instance_static_dir(destination_path),
  43. {_, :ok} <- {:mkdir_p, File.mkdir_p(Path.dirname(destination))},
  44. {_, {:ok, _}} <- {:copy, File.copy(origin_path, destination)} do
  45. :ok
  46. else
  47. {error, _} -> {:error, error}
  48. end
  49. end
  50. defp instance_static_dir(filename) do
  51. [:instance, :static_dir]
  52. |> Config.get!()
  53. |> Path.join(filename)
  54. end
  55. end