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
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
@impl true
def pipeline(:product, files, _opts) do
no_variants(files, [])
end
# Define file pipeline with other variants
def pipeline(:avatar, files, opts) do
create_variants(files,
variants: [
avatar: [
operation: :avatar,
resize_options: [size: 100],
filename: fn name ->
user_id = Access.get(opts, :user_id, "")
"radom" <> "-" <> user_id <> "-" <> name
end
],
thumbnail: [
operation: :thumbnail,
resize_options: [size: "300x400", options: [fit: :cover]],
write_options: [path: :memory, options: [suffix: ".jpeg"]],
filename: fn name -> name end
]
],
save_original: true,
filename: fn name ->
user_id = Access.get(opts, :user_id, "")
"main" <> "-" <> user_id <> "-" <> name
end
)
end
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