Upload (upload v0.3.0)
An opinionated file uploader.
Link to this section Summary
Functions
Get the adapter from config.
Converts a Plug.Upload
to an Upload
.
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
transferred()
Specs
uploadable()
Specs
uploadable() :: Plug.Upload.t() | t()
uploadable_path()
Specs
Link to this section Functions
adapter()
Get the adapter from config.
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
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
generate_key(filename, opts \\ [])
Specs
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"
get_extension(filename)
Specs
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"
get_signed_url(upload, opts \\ [])
Specs
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..."}
get_url(key)
Specs
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"
transfer(upload)
Specs
transfer(t()) :: {:ok, transferred()} | {:error, String.t()}
Transfer the file to where it will be stored.