trunk v0.0.10 Trunk.State View Source
This module defines a Trunk.State struct and provides some helper functions for working with that state.
Fields
The following fields are available in the state object. Some values are filled in during processing.
filename- The base filename of the file being processed. (e.g."photo.jpg")rootname- The root of the filen being processed. (e.g."photo")extname- The file extension of the file being processed. (e.g.".jpg")lower_extname- The file extension of the file being processed forced to lower case (e.g."*.jpg", even if the file is"PHOTO.JPG")path- The full path to the file being processed. If the file was passed in as a binary, it is a path to the temporary file created with that binary.versions- A map of the versions and their respectiveTrunk.VersionState(e.g.%{original: %Trunk.VersionState{}, thumbnail: %Trunk.VersionState{}})scope- A user struct/map passed in useful for determining storage locations and file naming.async- A boolean indicator of whether processing will be done in parallel.timeout- The timeout after which each processing process will be terminated. (Only applies withasync: true)storage- The module to use for storage processing. (e.g.Trunk.Storage.FilesystemorTrunk.Storage.S3)storage_opts- A keyword list of options for thestoragemoduleerrors- a place to record errors encountered during processing. (nliif no errors, otherwise a map of errors)opts- All the options merged together (see Options inTrunkmodule documentation).assigns- shared user data as a map (Same as assigns inPlug.Conn)
Link to this section Summary
Functions
Assigns a value to a key on the state
Retrieves an assign value for a specific version
Puts an error into the error map
Restore a saved state from a filename, JSON, or a map
Extracts the data needed from the state in order to reconstruct the file paths in future
Link to this section Types
assign_keys()
View Source
assign_keys() :: [atom()]
assign_keys() :: [atom()]
file_info() View Source
opts()
View Source
opts() :: Keyword.t()
opts() :: Keyword.t()
save_opts()
View Source
save_opts() :: [{:assigns, :all | assign_keys()}]
save_opts() :: [{:assigns, :all | assign_keys()}]
t()
View Source
t() :: %Trunk.State{
assigns: map(),
async: boolean(),
errors: Keyword.t(),
extname: String.t(),
filename: String.t(),
lower_extname: String.t(),
module: atom(),
opts: opts(),
path: String.t(),
rootname: String.t(),
scope: map() | struct(),
storage: atom(),
storage_opts: Keyword.t(),
timeout: integer(),
versions: map()
}
t() :: %Trunk.State{
assigns: map(),
async: boolean(),
errors: Keyword.t(),
extname: String.t(),
filename: String.t(),
lower_extname: String.t(),
module: atom(),
opts: opts(),
path: String.t(),
rootname: String.t(),
scope: map() | struct(),
storage: atom(),
storage_opts: Keyword.t(),
timeout: integer(),
versions: map()
}
version()
View Source
version() :: atom()
version() :: atom()
Link to this section Functions
assign(state, key, value)
View Source
assign(state :: Trunk.State.t(), key :: any(), value :: any()) :: map()
assign(state :: Trunk.State.t(), key :: any(), value :: any()) :: map()
Assigns a value to a key on the state.
Example:
iex> state.assigns[:hello]
nil
iex> state = Trunk.State.assign(state, :hello, :world)
iex> state.assigns[:hello]
:world
get_version_assign(map, version, assign)
View Source
get_version_assign(state :: Trunk.State.t(), version(), assign :: atom()) ::
any() | nil
get_version_assign(state :: Trunk.State.t(), version(), assign :: atom()) :: any() | nil
Retrieves an assign value for a specific version.
Example:
iex> state = %Trunk.State{versions: %{thumbnail: %Trunk.VersionState{assigns: %{hello: :world}}}}
iex> %Trunk.State.get_version_assign(state, :thumbnail, :hello)
:world
iex> %Trunk.State.get_version_assign(state, :thumbnail, :unknown)
nil
init(info, scope, opts) View Source
put_error(state, version, stage, error) View Source
Puts an error into the error map.
Example:
iex> state.errors
nil
iex> state = Trunk.State.put_error(state, :thumb, :transform, "Error with convert blah blah")
iex> state.errors
%{thumb: [transform: "Error with convert blah blah"]}
restore(file_info, opts \\ [])
View Source
restore(file_info(), opts()) :: Trunk.State.t()
restore(file_info(), opts()) :: Trunk.State.t()
Restore a saved state from a filename, JSON, or a map
Example:
iex> Trunk.State.restore("photo.jpg")
%Trunk.State{filename: "photo.jpg"}
iex> Trunk.State.restore(%{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
iex> Trunk.State.restore(%{"filename" => "photo.jpg", "assigns" => %{"hash" => "abcdef"}}
%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
iex> Trunk.State.restore("{\"filename\": \"photo.jpg\", \"assigns\": {\"hash\": \"abcdef\"}}")
%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
save(state, opts \\ [])
View Source
save(Trunk.State.t(), [{:as, :string} | save_opts()]) :: String.t()
save(Trunk.State.t(), [{:as, :json} | save_opts()]) :: String.t()
save(Trunk.State.t(), [{:as, :map} | save_opts()]) :: map()
save(Trunk.State.t(), [{:as, :string} | save_opts()]) :: String.t()
save(Trunk.State.t(), [{:as, :json} | save_opts()]) :: String.t()
save(Trunk.State.t(), [{:as, :map} | save_opts()]) :: map()
Extracts the data needed from the state in order to reconstruct the file paths in future.
Options:
:as- How to save the state.:string- Default, will just save the file name. An error will be raised if there are any assigns unless:ignore_assignsis set to tru:map- will save a map with keys:filename,:assigns, and:version_assigns:json- will save a map encoded as JSON (Requires Poison library to be included in deps)
:ignore_assignsboolean, default false. Use this to save as string and ignore any assigns (Make sure you’re not using assigns forTrunk.storage_dir/2orTrunk.filename/2):assigns- a list of keys to save from the assigns hashes
Example:
iex> Trunk.State.save(%Trunk.State{filename: "photo.jpg"})
"photo.jpg"
iex> Trunk.State.save(%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef"}}, as: :map)
%{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
iex> Trunk.State.save(%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef", file_size: 12345}}, as: :map, assigns: [:hash])
%{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
iex> Trunk.State.save(%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef"}}, as: :json)
"{\"filename\": \"photo.jpg\", \"assigns\": {\"hash\": \"abcdef\"}}"