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.Web.InstanceDocument do
7 alias Pleroma.Web.Endpoint
10 "terms-of-service" => "/static/terms-of-service.html",
11 "instance-panel" => "/instance/panel.html"
14 @spec get(String.t()) :: {:ok, String.t()} | {:error, atom()}
15 def get(document_name) do
16 case Map.fetch(@instance_documents, document_name) do
17 {:ok, path} -> {:ok, path}
18 _ -> {:error, :not_found}
22 @spec put(String.t(), String.t()) :: {:ok, String.t()} | {:error, atom()}
23 def put(document_name, origin_path) do
24 with {_, {:ok, destination_path}} <-
25 {:instance_document, Map.fetch(@instance_documents, document_name)},
26 :ok <- put_file(origin_path, destination_path) do
27 {:ok, Path.join(Endpoint.url(), destination_path)}
29 {:instance_document, :error} -> {:error, :not_found}
34 @spec delete(String.t()) :: :ok | {:error, atom()}
35 def delete(document_name) do
36 with {_, {:ok, path}} <- {:instance_document, Map.fetch(@instance_documents, document_name)},
37 instance_static_dir_path <- instance_static_dir(path),
38 :ok <- File.rm(instance_static_dir_path) do
41 {:instance_document, :error} -> {:error, :not_found}
42 {:error, :enoent} -> {:error, :not_found}
47 defp put_file(origin_path, destination_path) do
48 with destination <- instance_static_dir(destination_path),
49 {_, :ok} <- {:mkdir_p, File.mkdir_p(Path.dirname(destination))},
50 {_, {:ok, _}} <- {:copy, File.copy(origin_path, destination)} do
53 {error, _} -> {:error, error}
57 defp instance_static_dir(filename) do
58 [:instance, :static_dir]
60 |> Path.join(filename)