The package can be installed by adding ivcu
to your list of
dependencies in mix.exs
:
def deps do
[
{:ivcu, "~> 0.1.1"}
]
end
Storage is a module that implements IVCU.Storage
behaviour.
The easiest option is to use IVCU.Storage.Local
. If you want to
upload your files to S3, you may want to use ivcu_s3_storage
.
defmodule MyApp.LocalFileStorage do
use IVCU.Storage.Local, otp_app: :my_app
end
Converter is a module that implements IVCU.Converter
behaviour.
IVCU provides IVCU.Converter.CMD
that can be used to define
a converter that uses imagemagick's
convert
binary.
defmodule MyApp.ImageConverter do
use IVCU.Converter.CMD,
args: %{
thumb: [
"convert",
:input,
"-thumbnail",
"100x100^",
"-gravity",
"center",
"-extent",
"100x100",
:output
]
}
end
To process a file in specific way you need to define a definition --
a module that implements IVCU.Definition
behaviour.
defmodule MyApp.Image do
@behaviour IVCU.Definition
def versions, do: [:thumb, :original]
def storage, do: MyApp.LocalFileStorage
def converter, do: MyApp.ImageConverter
def validate(%{filename: filename}) do
if Path.extname(filename) in ~w(.png .jpg .jpeg) do
:ok
else
{:error, :invalid_image_extension}
end
end
def filename(version, filename) do
extname = Path.extname(filename)
base = filename |> String.replace(extname, "")
"#{base}_#{version}#{extname}"
end
end
def save_upload(%Plug.Upload{path: path}) do
path
|> IVCU.File.from_path()
|> IVCU.save(MyApp.Image)
end
def thumb_url(filename) do
%{thumb: url} =
filename
|> IVCU.File.from_path()
|> IVCU.urls()
url
end
def delete_old_file(filename) do
filename
|> IVCU.File.from_path()
|> IVCU.delete()
end