forked from AkkomaGang/akkoma
upload: add the S3 support itself
This commit is contained in:
parent
03c35e579b
commit
1596185ac6
1 changed files with 41 additions and 0 deletions
|
@ -3,6 +3,9 @@ defmodule Pleroma.Upload do
|
||||||
alias Pleroma.Web
|
alias Pleroma.Web
|
||||||
|
|
||||||
def store(%Plug.Upload{} = file, should_dedupe) do
|
def store(%Plug.Upload{} = file, should_dedupe) do
|
||||||
|
settings = Application.get_env(:pleroma, Pleroma.Upload)
|
||||||
|
use_s3 = Keyword.fetch!(settings, :use_s3)
|
||||||
|
|
||||||
content_type = get_content_type(file.path)
|
content_type = get_content_type(file.path)
|
||||||
uuid = get_uuid(file, should_dedupe)
|
uuid = get_uuid(file, should_dedupe)
|
||||||
name = get_name(file, uuid, content_type, should_dedupe)
|
name = get_name(file, uuid, content_type, should_dedupe)
|
||||||
|
@ -20,6 +23,13 @@ def store(%Plug.Upload{} = file, should_dedupe) do
|
||||||
File.cp!(file.path, result_file)
|
File.cp!(file.path, result_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
url_path =
|
||||||
|
if use_s3 do
|
||||||
|
put_s3_file(name, uuid, result_file, content_type)
|
||||||
|
else
|
||||||
|
url_path
|
||||||
|
end
|
||||||
|
|
||||||
%{
|
%{
|
||||||
"type" => "Document",
|
"type" => "Document",
|
||||||
"url" => [
|
"url" => [
|
||||||
|
@ -33,7 +43,11 @@ def store(%Plug.Upload{} = file, should_dedupe) do
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# XXX: does this code actually work? i am skeptical. --kaniini
|
||||||
def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
|
def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
|
||||||
|
settings = Application.get_env(:pleroma, Pleroma.Upload)
|
||||||
|
use_s3 = Keyword.fetch!(settings, :use_s3)
|
||||||
|
|
||||||
parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
|
parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
|
||||||
data = Base.decode64!(parsed["data"], ignore: :whitespace)
|
data = Base.decode64!(parsed["data"], ignore: :whitespace)
|
||||||
uuid = UUID.generate()
|
uuid = UUID.generate()
|
||||||
|
@ -71,6 +85,13 @@ def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
|
||||||
|
|
||||||
strip_exif_data(content_type, result_file)
|
strip_exif_data(content_type, result_file)
|
||||||
|
|
||||||
|
url_path =
|
||||||
|
if use_s3 do
|
||||||
|
put_s3_file(name, uuid, result_file, content_type)
|
||||||
|
else
|
||||||
|
url_path
|
||||||
|
end
|
||||||
|
|
||||||
%{
|
%{
|
||||||
"type" => "Image",
|
"type" => "Image",
|
||||||
"url" => [
|
"url" => [
|
||||||
|
@ -203,4 +224,24 @@ def get_content_type(file) do
|
||||||
_e -> "application/octet-stream"
|
_e -> "application/octet-stream"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp put_s3_file(name, uuid, path, content_type) do
|
||||||
|
settings = Application.get_env(:pleroma, Pleroma.Upload)
|
||||||
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue