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

Summary

Provides macros to creates uploader modules

Use macro service/1 to declare persistance service and variant service By default Tempel comes with Tempel.Local persistence module and Tempel.Variant for file variants service

Use macro pipeline/2 to create file pipeline, this macro will handle file vairants and uploader file variant Example:

defmodule MyUploader do
  service(persistence: Tempel.Local, variant: Tempel.Variant)

  pipeline(:avatar, %{
    filename: fn name, setting ->
      user_id = Map.get(setting, :user_id)
      "main" <> "-" <> user_id <> "-" <> name
    end,
    variants: %{
      thumbnail: [
        operation: :thumbnail,
        resize_options: [size: "300x400", options: [fit: :cover]],
        write_options: [path: :memory, options: [suffix: ".jpeg"]]
      ]
    }
  })
end

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.

Functions

This is the macro that is used to create file pipeline, from variants

This macro is used to assign service module to be used in the pipeline persistence servie and variant generation service

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 section Functions

Link to this function

add_main_image(variants, main_image, options, setting)

View Source
Link to this function

create_variants(images, setting, options, mod)

View Source
Link to this function

filename(file, variant_option, data, setting)

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

pipeline(name, opts)

View Source (macro)
@spec pipeline(
  name :: atom(),
  opts :: %{
    optional(filename :: atom()) =>
      (name :: binary(), opts :: term() -> binary()),
    optional(variants :: atom()) => %{
      required(variant_name :: atom()) => [
        operation: :thumbnail | :avatar,
        resize_options: [
          size: integer() | binary(),
          options: [{:fit, :cover | :center}]
        ],
        write_options: [
          path:
            Path.t()
            | Plug.Conn.t()
            | Enumerable.t()
            | File.Stream.t()
            | :memory,
          option: [{:suffix, binary()}]
        ]
      ]
    }
  }
) :: Macro.t()

This is the macro that is used to create file pipeline, from variants

this macro will generate function save/3 for each pipeline name

example

Example

defmodule MyUploader do
  use Tempel.Gate

  pipeline(:avatar, %{
    filename: fn name, setting ->
      user_id = Map.get(setting, :user_id)
      "main" <> "-" <> user_id <> "-" <> name
    end,
    variants: %{
      thumbnail: [
        operation: :thumbnail,
        resize_options: [size: "300x400", options: [fit: :cover]],
        write_options: [path: :memory, options: [suffix: ".jpeg"]]
      ]
    }
  })

  # will produce function

  @spec save(name :: atom(), files :: list(file()), options :: term()) :: {:ok, term()} | {:error, term()}
  def save(:avatar, files, options) do
    # write procedures goes here
  end
end

This macro is used to assign service module to be used in the pipeline persistence servie and variant generation service

You can define your custom persistence service, please refer to module Tempel.Local in lib/tempel/local.ex as an example

You can define your custom variant generation service, the service need to implement Tempel.VariantBehaviour. please refer to module Tempel.Variant in lib/tempel/variant.ex as an example

defmodule MyUploader do
  use Tempel.Gate

  service(persistence: Tempel.Local, variant: Tempel.Variant)
end
Link to this function

write(setting, variants, mod)

View Source
Link to this function

write_option(variant_option, file)

View Source