diff options
Diffstat (limited to 'lib/pleroma')
-rw-r--r-- | lib/pleroma/upload.ex | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index e0cb545b0..a744e6fd4 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -3,12 +3,17 @@ defmodule Pleroma.Upload do alias Pleroma.Web 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) uuid = get_uuid(file, should_dedupe) name = get_name(file, uuid, content_type, should_dedupe) upload_folder = get_upload_path(uuid, should_dedupe) url_path = get_url(name, uuid, should_dedupe) + strip_exif_data(content_type, file.path) + File.mkdir_p!(upload_folder) result_file = Path.join(upload_folder, name) @@ -18,7 +23,12 @@ defmodule Pleroma.Upload do File.cp!(file.path, result_file) end - 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" => "Document", @@ -33,7 +43,11 @@ defmodule Pleroma.Upload do } end + # XXX: does this code actually work? i am skeptical. --kaniini 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) data = Base.decode64!(parsed["data"], ignore: :whitespace) uuid = UUID.generate() @@ -71,6 +85,13 @@ defmodule Pleroma.Upload do 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", "url" => [ @@ -203,4 +224,25 @@ defmodule Pleroma.Upload do _e -> "application/octet-stream" 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 |