2018-08-28 01:20:54 +00:00
|
|
|
defmodule Pleroma.Uploaders.Local do
|
2018-08-28 12:57:41 +00:00
|
|
|
@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
|
2018-08-28 22:48:03 +00:00
|
|
|
File.rm!(tmpfile)
|
2018-08-28 01:20:54 +00:00
|
|
|
else
|
2018-08-28 22:48:03 +00:00
|
|
|
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
|