View Source Tempel.Gate behaviour (Tempel v0.2.2)

Summary

Provides functionality to creates uploader modules

Examples

defmodule MyUploader do
  use Tempel.Gate

  def init(opts \\ %{}) do
    # do something to warmup the uploader and return a map
    opts
  end

  @impl true
  def pipeline(:profile, files, options) do
    create_variants(files, variants: [
      thumbnail: [
        operation: :thumbnail,
        resize_options: [size: "300x400", options: [fit: :cover]],
        filename: name ->
          "thumbnails/" <> options[:user_id] <>"/" <> name
        end
      ]
    ], save_original: true)
  end
end

this uploader is fully functional, cann be invoked from different modules.

MyUploader.save(:profile, file, %{"user_id": 10})
# => {:ok, %{"thumbnail": "thumbnails/10/file.jpg",  "main" => "file.jpg"}}

For files without variants such as document/pdf we can use no_variants/2

defmodule MyUploader do
  # all other code
  def pipeline(:document, files, options) do
    no_variants(files, filename: name -> "document/" <> options["user_id"] <> "/" <> name end)
  end
end

MyUploader.save(:document, file, %{"user_id": 10})
# => {:ok, %{"main" => "document/10/file.pdf"}}

Available types:

  • file() is structured to match the format of Plug.Upload
  • fileio() is structured like file(). But instead of the file path it contains the IO data in key :iodata

Link to this section Summary

Types

This type is stuctured like the format of Plug.Upload

fileio() is structured like file(). But instead of the file path it contains the IO data in key :iodata mainly the result from variant generation in Tempel.Variant

This type is the one will be consumed to Tempel.Local

This type is the retun after save from Tempel.Local through Tempel.Persistence

Callbacks

This function is used to initialize the settings, could be storage path, aws secret key, etc.

This function is used to create variants from files.

Link to this section Types

@type file() :: %{filename: String.t(), path: String.t(), content_type: String.t()}

This type is stuctured like the format of Plug.Upload

@type fileio() :: %{iodata: binary(), path: String.t(), content_type: String.t()}

fileio() is structured like file(). But instead of the file path it contains the IO data in key :iodata mainly the result from variant generation in Tempel.Variant

@type raw_variant() :: %{required(name :: String.t()) => file() | fileio()}

This type is the one will be consumed to Tempel.Local

@type saved_variant() :: %{required(name :: String.t()) => object_key :: String.t()}

This type is the retun after save from Tempel.Local through Tempel.Persistence

Link to this section Callbacks

@callback init(opts :: term()) :: term()

This function is used to initialize the settings, could be storage path, aws secret key, etc.

Link to this callback

pipeline(name, files, opts)

View Source
@callback pipeline(name :: atom(), files :: [file()], opts :: keyword()) :: raw_variant()

This function is used to create variants from files.

Link to this section Functions

Link to this function

create_variants(formatted_images, options \\ [])

View Source
Link to this function

delete_file(caller, file, opts)

View Source
@spec delete_file(module(), [binary() | map()] | map() | binary(), map()) ::
  :ok | {:error, any()}
Link to this function

delete_file(caller, stored_file, files_to_delete, opts)

View Source
@spec format_files(files :: [file()]) :: [file()]
@spec format_files(file :: file()) :: term()
Link to this function

no_variants(files, options \\ [])

View Source
Link to this function

object_url(mod, file, opts)

View Source
@spec object_url(module(), binary(), map()) :: binary()
Link to this function

save_file(mod, name, files, opts)

View Source
@spec save_file(
  module(),
  binary(),
  [raw_variant()] | raw_variant(),
  map()
) :: {:error, reason :: any()} | {:ok, [saved_variant()] | saved_variant()}
Link to this function

write(files, opts \\ %{})

View Source
@spec write(opts :: map(), [raw_variant()] | raw_variant()) ::
  {:error, reason :: any()} | {:ok, [saved_variant()]}
Link to this function

write_option(variant_option, file)

View Source