-defmodule Pleroma.Uploaders.Local do
- def put_file(name, uuid, file, content_type) do
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
- upload_path = get_upload_path(uuid, should_dedupe)
- url_path = get_url(name, uuid, should_dedupe)
+defmodule Pleroma.Uploaders.Local do
+ @behaviour Pleroma.Uploaders.Uploader
- File.mkdir_p!(upload_folder)
+ def get_file(_) do
+ {:ok, {:static_dir, upload_path()}}
+ end
- result_file = Path.join(upload_folder, name)
+ def put_file(upload) do
+ {local_path, file} =
+ case Enum.reverse(Path.split(upload.path)) do
+ [file] ->
+ {upload_path(), file}
- if File.exists?(result_file) do
- File.rm!(file.path)
- else
- File.cp!(file.path, result_file)
- end
+ [file | folders] ->
+ path = Path.join([upload_path()] ++ Enum.reverse(folders))
+ File.mkdir_p!(path)
+ {path, file}
+ end
- url_path
- end
+ result_file = Path.join(local_path, file)
- def upload_path do
- settings = Application.get_env(:pleroma, Pleroma.Uploaders.Local)
- Keyword.fetch!(settings, :uploads)
- end
-
- defp get_upload_path(uuid, should_dedupe) do
- if should_dedupe do
- upload_path()
- else
- Path.join(upload_path(), uuid)
+ if not File.exists?(result_file) do
+ File.cp!(upload.tempfile, result_file)
end
- end
- defp get_url(name, uuid, should_dedupe) do
- if should_dedupe do
- url_for(:cow_uri.urlencode(name))
- else
- url_for(Path.join(uuid, :cow_uri.urlencode(name)))
- end
+ :ok
end
- defp url_for(file) do
- "#{Web.base_url()}/media/#{file}"
+ def upload_path do
+ Pleroma.Config.get!([__MODULE__, :uploads])
end
end