akkoma/lib/pleroma/uploaders/uploader.ex

45 lines
1.5 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2018-12-31 08:41:47 -07:00
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Uploaders.Uploader do
@moduledoc """
2018-11-23 09:40:45 -07:00
Defines the contract to put and get an uploaded file to any backend.
"""
2018-11-23 09:40:45 -07:00
@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.
2018-11-23 09:40:45 -07:00
Returns:
2018-11-29 13:11:45 -07:00
* `:ok` which assumes `{:ok, upload.path}`
2018-11-23 09:40:45 -07:00
* `{:ok, spec}` where spec is:
* `{:file, filename :: String.t}` to handle reads with `get_file/1` (recommended)
2018-11-29 13:11:45 -07:00
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.
2018-11-23 09:40:45 -07:00
* `{:error, String.t}` error information if the file failed to be saved to the backend.
2018-11-29 13:11:45 -07:00
"""
2018-11-29 13:11:45 -07:00
@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