2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-08-27 19:20:54 -06:00
|
|
|
defmodule Pleroma.Uploaders.Local do
|
2018-08-28 06:57:41 -06:00
|
|
|
@behaviour Pleroma.Uploaders.Uploader
|
2018-08-27 19:20:54 -06:00
|
|
|
|
2020-01-12 11:48:58 -07:00
|
|
|
@impl true
|
2018-11-23 09:40:45 -07:00
|
|
|
def get_file(_) do
|
|
|
|
{:ok, {:static_dir, upload_path()}}
|
|
|
|
end
|
|
|
|
|
2020-01-12 11:48:58 -07:00
|
|
|
@impl true
|
2018-11-29 13:11:45 -07:00
|
|
|
def put_file(upload) do
|
|
|
|
{local_path, file} =
|
2019-08-10 12:46:26 -06:00
|
|
|
case Enum.reverse(Path.split(upload.path)) do
|
2018-11-29 13:11:45 -07:00
|
|
|
[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)
|
|
|
|
|
2019-08-10 12:46:26 -06:00
|
|
|
if not File.exists?(result_file) do
|
2018-11-29 13:11:45 -07:00
|
|
|
File.cp!(upload.tempfile, result_file)
|
2018-08-27 19:20:54 -06:00
|
|
|
end
|
|
|
|
|
2018-11-29 13:11:45 -07:00
|
|
|
:ok
|
2018-08-27 19:20:54 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def upload_path do
|
2018-11-23 09:40:45 -07:00
|
|
|
Pleroma.Config.get!([__MODULE__, :uploads])
|
2018-08-27 19:20:54 -06:00
|
|
|
end
|
2020-01-12 11:48:58 -07:00
|
|
|
|
|
|
|
@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
|
2018-08-27 19:20:54 -06:00
|
|
|
end
|