akkoma/lib/pleroma/uploaders/local.ex

46 lines
945 B
Elixir
Raw Normal View History

2018-08-28 01:20:54 +00:00
defmodule Pleroma.Uploaders.Local do
@behaviour Pleroma.Uploaders.Uploader
2018-08-28 01:20:54 +00:00
2018-08-28 01:45:53 +00:00
alias Pleroma.Web
2018-11-23 16:40:45 +00:00
def get_file(_) do
{:ok, {:static_dir, upload_path()}}
end
def put_file(name, uuid, tmpfile, _content_type, opts) do
upload_folder = get_upload_path(uuid, opts.dedupe)
2018-08-28 01:20:54 +00:00
File.mkdir_p!(upload_folder)
result_file = Path.join(upload_folder, name)
if File.exists?(result_file) do
File.rm!(tmpfile)
2018-08-28 01:20:54 +00:00
else
File.cp!(tmpfile, result_file)
2018-08-28 01:20:54 +00:00
end
2018-11-23 16:40:45 +00:00
{:ok, {:file, get_url(name, uuid, opts.dedupe)}}
2018-08-28 01:20:54 +00:00
end
def upload_path do
2018-11-23 16:40:45 +00:00
Pleroma.Config.get!([__MODULE__, :uploads])
2018-08-28 01:20:54 +00:00
end
defp get_upload_path(uuid, should_dedupe) do
if should_dedupe do
upload_path()
else
Path.join(upload_path(), uuid)
end
end
defp get_url(name, uuid, should_dedupe) do
if should_dedupe do
2018-11-23 16:40:45 +00:00
:cow_uri.urlencode(name)
2018-08-28 01:20:54 +00:00
else
2018-11-23 16:40:45 +00:00
Path.join(uuid, :cow_uri.urlencode(name))
2018-08-28 01:20:54 +00:00
end
end
end