Bump Copyright to 2021
[akkoma] / lib / pleroma / web / instance_document.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.InstanceDocument do
6 alias Pleroma.Config
7 alias Pleroma.Web.Endpoint
8
9 @instance_documents %{
10 "terms-of-service" => "/static/terms-of-service.html",
11 "instance-panel" => "/instance/panel.html"
12 }
13
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}
19 end
20 end
21
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)}
28 else
29 {:instance_document, :error} -> {:error, :not_found}
30 error -> error
31 end
32 end
33
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
39 :ok
40 else
41 {:instance_document, :error} -> {:error, :not_found}
42 {:error, :enoent} -> {:error, :not_found}
43 error -> error
44 end
45 end
46
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
51 :ok
52 else
53 {error, _} -> {:error, error}
54 end
55 end
56
57 defp instance_static_dir(filename) do
58 [:instance, :static_dir]
59 |> Config.get!()
60 |> Path.join(filename)
61 end
62 end