Packages

Tempel is a file attachment management for elixir web project

Current section

Files

Jump to
tempel lib tempel.ex
Raw

lib/tempel.ex

defmodule Tempel do
@moduledoc """
This module is used to manage file attachments. It accept a single or list of file
formatted to match file format of [Plug.Upload](https://hexdocs.pm/plug/Plug.Upload.html)
```
%{
filename :: binary(),
path :: Path.t,
content_type: binary()
}
```
# Get Started
define your own file uploader
```
defmodule MyUploader do
use Temple.Gate
# define service that will be used to handle file persistence and file variants
service(persistence: Tempel.Local, variant: Tempel.Variant)
def init(opts) do
# add important configuration here such as storage key and cridentials
%{}
end
# Define file pipeline
# This pipeline will only save the main image, no other variants will be saved
pipeline(:product)
# Define file pipeline with other variants
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
```
To call this uploader in you uploading process
```
# file from `Plug.Upload`
MyUploader.save(:avatar, file)
# => {:ok, %{"main" => object_key, "thumbnail" => object_key}}
Myuploader.save(:product, file)
# => {:ok, %{"main" => object_key}}
```
"""
end