View Source Tempel.Gate behaviour (Tempel v0.1.1)
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.Uploadfileio()
is structured likefile()
. 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
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
@type pipeline() :: %{ 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() | Enumerable.t() | File.Stream.t() | :memory, option: [{:suffix, binary()}] ] ] } }
This type is the one will be consumed to Tempel.Local
This type is the retun after save from Tempel.Local
through Tempel.Persistence
Link to this section Callbacks
This function is used to initialize the settings, could be storage path, aws secret key, etc.
Link to this section Functions
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"]]
]
}
})
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