Attempt to resolve merge conflict
[akkoma] / lib / pleroma / uploaders / uploader.ex
index 7380320af41cd2f5b4e71f1feb140a61a1abe714..afda5609e506bb6f055f093bfecd38d807c41c0d 100644 (file)
@@ -1,26 +1,40 @@
 defmodule Pleroma.Uploaders.Uploader do
   @moduledoc """
-  Defines the contract to put an uploaded file to any backend.
+  Defines the contract to put and get an uploaded file to any backend.
   """
 
+  @doc """
+  Instructs how to get the file from the backend.
+
+  Used by `Pleroma.Plugs.UploadedMedia`.
+  """
+  @type get_method :: {:static_dir, directory :: String.t()} | {:url, url :: String.t()}
+  @callback get_file(file :: String.t()) :: {:ok, get_method()}
+
   @doc """
   Put a file to the backend.
 
-  Returns a `String.t` containing the path of the uploaded file.
+  Returns:
+
+  * `:ok` which assumes `{:ok, upload.path}`
+  * `{:ok, spec}` where spec is:
+    * `{:file, filename :: String.t}` to handle reads with `get_file/1` (recommended)
+
+    This allows to correctly proxy or redirect requests to the backend, while allowing to migrate backends without breaking any URL.
+  * `{url, url :: String.t}` to bypass `get_file/2` and use the `url` directly in the activity.
+  * `{:error, String.t}` error information if the file failed to be saved to the backend.
+
+
   """
-  @callback put_file(
-              name :: String.t(),
-              uuid :: String.t(),
-              file :: File.t(),
-              content_type :: String.t(),
-              should_dedupe :: Boolean.t()
-            ) :: String.t()
-
-  @callback put_file(
-              name :: String.t(),
-              uuid :: String.t(),
-              image_data :: String.t(),
-              content_type :: String.t(),
-              should_dedupe :: String.t()
-            ) :: String.t()
+  @callback put_file(Pleroma.Upload.t()) ::
+              :ok | {:ok, {:file | :url, String.t()}} | {:error, String.t()}
+
+  @spec put_file(module(), Pleroma.Upload.t()) ::
+          {:ok, {:file | :url, String.t()}} | {:error, String.t()}
+  def put_file(uploader, upload) do
+    case uploader.put_file(upload) do
+      :ok -> {:ok, {:file, upload.path}}
+      other -> other
+    end
+  end
 end