View Source IVCU.Definition behaviour (IVCU v0.1.1)

An interface for file processing.

example

Example

Suppose we have defined storage and converter modules as MyApp.FileStorage and MyApp.ImageConverter respectively. Then we can provide a specific definition for some images.

defmodule MyApp.Image do
  @behaviour IVCU.Definition

  def versions, do: [:thumb, :original]
  def storage, do: MyApp.FileStorage
  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

Using that definition you get two file formats: :thumb and :original and pass only files with .png, .jpg, or .jpeg extensions.

Also you stored filenames will be looking like <original base filename>_<version>.<original extension>.

Link to this section Summary

Types

A name for the version of the processed image.

Callbacks

Return Converter module.

Get a new filename for the provided version.

Return Storage module.

Check if the file is allowed to be processed and put into a storage.

Return a list of versions.

Link to this section Types

Specs

version() :: atom()

A name for the version of the processed image.

Link to this section Callbacks

Specs

converter() :: module()

Return Converter module.

Specs

filename(version(), String.t()) :: String.t()

Get a new filename for the provided version.

Specs

storage() :: module()

Return Storage module.

Specs

validate(IVCU.File.t()) :: :ok | {:error, term()}

Check if the file is allowed to be processed and put into a storage.

Specs

versions() :: [version()]

Return a list of versions.

Note

If you provide :original among versions, the file with this version won't be modified by the converter.