View Source Briefly (briefly v0.4.1)

Simple, robust temporary file support for Elixir.

highlighted-features

Highlighted Features

  • Create temporary files with prefix and extname options.
  • Files are removed after the requesting process exits.
  • File creation is based on Plug.Upload's robust retry logic.
  • Configurable; built-in support for system environment variables and fallbacks.

usage

Usage

The fastest way to use Briefly is with Mix.install/2 (requires Elixir v1.12+):

Mix.install([
  {:briefly, "~> 0.4.0"}
])

{:ok, path} = Briefly.create()
File.write!(path, "My temp file contents")
File.read!(path)
# => "My temp file contents"

# When this process exits, the file at `path` is removed

If you want to use Briefly in a Mix project, you can add the above dependency to your list of dependencies in mix.exs.

Briefly can also create a temporary directory:

{:ok, dir} = Briefly.create(directory: true)
File.write!(Path.join(dir, "test.txt"), "Some Text")
# When this process exits, the directory and file are removed

Refer to the documentation for a list of options that are available to Briefly.create/1 and Briefly.create!/1.

configuration

Configuration

The default, out-of-the-box settings for Briefly are equivalent to the following Mix config:

# config/config.exs
config :briefly,
  directory: [{:system, "TMPDIR"}, {:system, "TMP"}, {:system, "TEMP"}, "/tmp"],
  default_prefix: "briefly",
  default_extname: ""

directory here declares an ordered list of possible directory definitions that Briefly will check in order.

The {:system, env_var} tuples point to system environment variables to be checked. If none of these are defined, Briefly will use the final entry: /tmp.

You can override the settings with your own candidates in your application Mix config (and pass prefix and extname to Briefly.create to override default_prefix and default_extname on a case-by-case basis).

Link to this section Summary

Functions

Removes the temporary files and directories created by the current process and return their paths.

Requests a temporary file to be created with the given options.

Requests a temporary file to be created with the given options and raises on failure.

Link to this section Types

@type create_opts() :: [
  prefix: binary(),
  extname: binary(),
  directory: boolean(),
  monitor_pid: pid()
]

Link to this section Functions

Removes the temporary files and directories created by the current process and return their paths.

@spec cleanup(pid() | nil) :: [binary()]
@spec create(create_opts()) ::
  {:ok, binary()}
  | {:too_many_attempts, binary(), pos_integer()}
  | {:no_tmp, [binary()]}

Requests a temporary file to be created with the given options.

@spec create!(create_opts()) :: binary() | no_return()

Requests a temporary file to be created with the given options and raises on failure.