example of flexible storage backends
This commit is contained in:
parent
49b165ddc6
commit
709816a0f8
5 changed files with 77 additions and 67 deletions
44
lib/pleroma/uploaders/local.ex
Normal file
44
lib/pleroma/uploaders/local.ex
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
defmodule Pleroma.Uploaders.Local do
|
||||
def put_file(name, uuid, file, content_type) do
|
||||
|
||||
upload_path = get_upload_path(uuid, should_dedupe)
|
||||
url_path = get_url(name, uuid, should_dedupe)
|
||||
|
||||
File.mkdir_p!(upload_folder)
|
||||
|
||||
result_file = Path.join(upload_folder, name)
|
||||
|
||||
if File.exists?(result_file) do
|
||||
File.rm!(file.path)
|
||||
else
|
||||
File.cp!(file.path, result_file)
|
||||
end
|
||||
|
||||
url_path
|
||||
end
|
||||
|
||||
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)
|
||||
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
|
||||
end
|
||||
|
||||
defp url_for(file) do
|
||||
"#{Web.base_url()}/media/#{file}"
|
||||
end
|
||||
end
|
||||
24
lib/pleroma/uploaders/s3.ex
Normal file
24
lib/pleroma/uploaders/s3.ex
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
defmodule Pleroma.Uploaders.S3 do
|
||||
|
||||
def put_file(name, uuid, path, content_type) do
|
||||
|
||||
settings = Application.get_env(:pleroma, Pleroma.Uploaders.S3)
|
||||
bucket = Keyword.fetch!(settings, :bucket)
|
||||
public_endpoint = Keyword.fetch!(settings, :public_endpoint)
|
||||
|
||||
{:ok, file_data} = File.read(path)
|
||||
|
||||
File.rm!(path)
|
||||
|
||||
s3_name = "#{uuid}/#{name}"
|
||||
|
||||
{:ok, result} =
|
||||
ExAws.S3.put_object(bucket, s3_name, file_data, [
|
||||
{:acl, :public_read},
|
||||
{:content_type, content_type}
|
||||
])
|
||||
|> ExAws.request()
|
||||
|
||||
"#{public_endpoint}/#{bucket}/#{s3_name}"
|
||||
end
|
||||
end
|
||||
0
lib/pleroma/uploaders/swift.ex
Normal file
0
lib/pleroma/uploaders/swift.ex
Normal file
Loading…
Add table
Add a link
Reference in a new issue