Fix Mastodon API when actor's nickname is null
[akkoma] / lib / pleroma / upload.ex
index dd2bbab9f44625166c29a3ded1e102c214770295..a744e6fd4bbeea082fb6d9c80dde831ea6fc7bb6 100644 (file)
@@ -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,10 +23,15 @@ 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" => "Image",
+      "type" => "Document",
       "url" => [
         %{
           "type" => "Link",
@@ -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" => [
@@ -124,20 +145,20 @@ defmodule Pleroma.Upload do
     if should_dedupe do
       create_name(uuid, List.last(String.split(file.filename, ".")), type)
     else
-      unless String.contains?(file.filename, ".") do
-        case type do
-          "image/png" -> file.filename <> ".png"
-          "image/jpeg" -> file.filename <> ".jpg"
-          "image/gif" -> file.filename <> ".gif"
-          "video/webm" -> file.filename <> ".webm"
-          "video/mp4" -> file.filename <> ".mp4"
-          "audio/mpeg" -> file.filename <> ".mp3"
-          "audio/ogg" -> file.filename <> ".ogg"
-          "audio/wav" -> file.filename <> ".wav"
-          _ -> file.filename
+      parts = String.split(file.filename, ".")
+
+      new_filename =
+        if length(parts) > 1 do
+          Enum.drop(parts, -1) |> Enum.join(".")
+        else
+          Enum.join(parts)
         end
-      else
-        file.filename
+
+      case type do
+        "application/octet-stream" -> file.filename
+        "audio/mpeg" -> new_filename <> ".mp3"
+        "image/jpeg" -> new_filename <> ".jpg"
+        _ -> Enum.join([new_filename, String.split(type, "/") |> List.last()], ".")
       end
     end
   end
@@ -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