Upload (upload v0.3.0)

An opinionated file uploader.

Link to this section Summary

Functions

Get the adapter from config.

Cast a file path to an Upload.

Converts a filename to a unique key.

Gets the extension from a filename.

Get the URL for a given key. It will behave differently based on the adapter you're using.

Get the URL for a given key. It will behave differently based on the adapter you're using.

Transfer the file to where it will be stored.

Link to this section Types

Specs

t() :: %Upload{
  filename: String.t(),
  key: String.t(),
  path: String.t(),
  status: term()
}
Link to this type

transferred()

Specs

transferred() :: %Upload{
  filename: String.t(),
  key: String.t(),
  path: String.t(),
  status: :transferred
}
Link to this type

uploadable()

Specs

uploadable() :: Plug.Upload.t() | t()
Link to this type

uploadable_path()

Specs

uploadable_path() :: String.t() | t()

Link to this section Functions

Get the adapter from config.

Link to this function

cast(uploadable, opts \\ [])

Specs

cast(uploadable(), list()) :: {:ok, t()} | :error

Converts a Plug.Upload to an Upload.

Examples

iex> Upload.cast(%Plug.Upload{path: "/path/to/foo.png", filename: "foo.png"})
{:ok, %Upload{path: "/path/to/foo.png", filename: "foo.png", key: "123456.png"}}

iex> Upload.cast(100)
:error
Link to this function

cast_path(path, opts \\ [])

Specs

cast_path(uploadable_path(), list()) :: {:ok, t()} | :error

Cast a file path to an Upload.

Warning: Do not use cast_path with unsanitized user input.

Examples

iex> Upload.cast_path("/path/to/foo.png")
{:ok, %Upload{path: "/path/to/foo.png", filename: "foo.png", key: "123456.png"}}

iex> Upload.cast_path(100)
:error
Link to this function

generate_key(filename, opts \\ [])

Specs

generate_key(String.t(), [{:prefix, list()}]) :: String.t()

Converts a filename to a unique key.

Examples

iex> Upload.generate_key("phoenix.png")
"b9452178-9a54-5e99-8e64-a059b01b88cf.png"

iex> Upload.generate_key("phoenix.png", prefix: ["logos"])
"logos/b9452178-9a54-5e99-8e64-a059b01b88cf.png"
Link to this function

get_extension(filename)

Specs

get_extension(String.t() | t()) :: String.t()

Gets the extension from a filename.

Examples

iex> Upload.get_extension("foo.png")
".png"

iex> Upload.get_extension("foo.PNG")
".png"

iex> Upload.get_extension("foo")
""

iex> {:ok, upload} = Upload.cast_path("/path/to/foo.png")
...> Upload.get_extension(upload)
".png"
Link to this function

get_signed_url(upload, opts \\ [])

Specs

get_signed_url(t() | String.t(), Keyword.t()) ::
  {:ok, String.t()} | {:error, String.t()}

Get the URL for a given key. It will behave differently based on the adapter you're using.

Examples

iex> Upload.get_signed_url("123456.png")
{:ok, "http://yoururl.com/123456.png?X-Amz-Expires=3600..."}

iex> Upload.get_signed_url("123456.png", expires_in: 4200)
{:ok, "http://yoururl.com/123456.png?X-Amz-Expires=4200..."}

Specs

get_url(t() | String.t()) :: String.t()

Get the URL for a given key. It will behave differently based on the adapter you're using.

Local

iex> Upload.get_url("123456.png")
"/uploads/123456.png"

S3

iex> Upload.get_url("123456.png")
"https://my_bucket_name.s3.amazonaws.com/123456.png"

Fake / Test

iex> Upload.get_url("123456.png")
"123456.png"
Link to this function

transfer(upload)

Specs

transfer(t()) :: {:ok, transferred()} | {:error, String.t()}

Transfer the file to where it will be stored.