From 3a4773c3c2bd0bbef244eb519b07208da9108e49 Mon Sep 17 00:00:00 2001 From: dcc Date: Sat, 2 Sep 2023 00:52:52 -0700 Subject: First --- lib/pleroma/uploaders/local.ex | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 lib/pleroma/uploaders/local.ex (limited to 'lib/pleroma/uploaders/local.ex') diff --git a/lib/pleroma/uploaders/local.ex b/lib/pleroma/uploaders/local.ex new file mode 100644 index 0000000..e4a309c --- /dev/null +++ b/lib/pleroma/uploaders/local.ex @@ -0,0 +1,49 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Uploaders.Local do + @behaviour Pleroma.Uploaders.Uploader + + @impl true + def get_file(_) do + {:ok, {:static_dir, upload_path()}} + end + + @impl true + def put_file(upload) do + {local_path, file} = + case Enum.reverse(Path.split(upload.path)) do + [file] -> + {upload_path(), file} + + [file | folders] -> + path = Path.join([upload_path()] ++ Enum.reverse(folders)) + File.mkdir_p!(path) + {path, file} + end + + result_file = Path.join(local_path, file) + + if not File.exists?(result_file) do + File.cp!(upload.tempfile, result_file) + end + + :ok + end + + def upload_path do + Pleroma.Config.get!([__MODULE__, :uploads]) + end + + @impl true + def delete_file(path) do + upload_path() + |> Path.join(path) + |> File.rm() + |> case do + :ok -> :ok + {:error, posix_error} -> {:error, to_string(posix_error)} + end + end +end -- cgit v1.2.3