e4a309cea680d392143169805eafe83a41920d3b
[anni] / lib / pleroma / uploaders / local.ex
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
5 defmodule Pleroma.Uploaders.Local do
6   @behaviour Pleroma.Uploaders.Uploader
7
8   @impl true
9   def get_file(_) do
10     {:ok, {:static_dir, upload_path()}}
11   end
12
13   @impl true
14   def put_file(upload) do
15     {local_path, file} =
16       case Enum.reverse(Path.split(upload.path)) do
17         [file] ->
18           {upload_path(), file}
19
20         [file | folders] ->
21           path = Path.join([upload_path()] ++ Enum.reverse(folders))
22           File.mkdir_p!(path)
23           {path, file}
24       end
25
26     result_file = Path.join(local_path, file)
27
28     if not File.exists?(result_file) do
29       File.cp!(upload.tempfile, result_file)
30     end
31
32     :ok
33   end
34
35   def upload_path do
36     Pleroma.Config.get!([__MODULE__, :uploads])
37   end
38
39   @impl true
40   def delete_file(path) do
41     upload_path()
42     |> Path.join(path)
43     |> File.rm()
44     |> case do
45       :ok -> :ok
46       {:error, posix_error} -> {:error, to_string(posix_error)}
47     end
48   end
49 end