# `Alternera.Plugin.Provisional.Files`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_plugin_sdk/lib/alternera/plugin/provisional/files.ex#L1)

Provisional: added for the WebAssembly module plugin, the first plugin
that keeps a file a user uploads into a step, and kept here until a
second plugin uses it.

Files a user uploads into a step, kept by the application, one copy per
tenant, plugin and content: a file is named by the lower-case hex
SHA-256 of its bytes (its digest), so the same file uploaded twice is
stored once and a step pins exactly one file by keeping its digest in
its settings. A pipeline revision therefore pins the file, and the
revision history is the file's history. Stored files are never changed.

A plugin whose settings pin stored files implements `pinned_files/1`:
the application then refuses to save a pipeline whose step pins a file
the tenant has not stored, naming the digest (a plugin's `init/1` gets
no tenant, so it cannot).

## Export and import

An export carries the files its pipelines' steps pin next to them,
named by digest: `files/<plugin>/<digest>.<extension>` in an archive
(`file_extension/0`), a `files:` list in a single file. An import
checks each carried file against its digest and hands it to the
plugin: `check_file/1`, the checks that cost nothing, for the preview,
then `accept_file/4` just before Apply writes a pipeline that pins
it, which takes the file as an upload would (the same checks, compile
and limits) and stores it. A file the tenant stored before is taken as
it is.

## Removal

The application removes a stored file that nothing refers to: no
pipeline, version of one (every revision, and so every release),
editing draft or recipe holds its digest, and it was stored more than a
day ago. That runs with the tenant's hourly cleanup, and `reclaim/1`
runs it at once for a tenant whose new file does not fit its quota.

# `info`

```elixir
@type info() :: %{
  digest: String.t(),
  size: non_neg_integer(),
  meta: map(),
  uploaded_by: String.t() | nil,
  inserted_at: DateTime.t()
}
```

What the application knows of a stored file.

# `pinned`

```elixir
@type pinned() :: %{digest: String.t(), name: String.t() | nil}
```

A file a step pins: its digest, and the name the step gives it.

# `accept_file`

```elixir
@callback accept_file(
  tenant_id :: String.t(),
  user_id :: String.t() | nil,
  name :: String.t() | nil,
  bytes :: binary()
) :: {:ok, map()} | {:error, String.t()}
```

Takes a carried file as an upload by `user_id` into `tenant_id` would,
named `name` (the step's name for it, or nil), and stores it: an import
runs it just before it writes the first pipeline that pins the file.

# `check_file`

```elixir
@callback check_file(bytes :: binary()) :: :ok | {:error, String.t()}
```

The checks on a carried file that cost nothing (no compile, nothing
stored): `:ok`, or the refusal an upload of it would give. An import's
preview runs it.

# `file_extension`
*optional* 

```elixir
@callback file_extension() :: String.t()
```

The extension an archive gives the plugin's files (`bin` when not given).

# `pinned_files`

```elixir
@callback pinned_files(settings :: map()) :: [pinned()]
```

The stored files a step's settings pin.

# `any_bytes`

```elixir
@spec any_bytes(String.t(), String.t()) ::
  {:ok, binary()} | :not_found | {:error, String.t()}
```

The bytes of a stored file of `plugin_id` by digest alone, whichever
tenant stored it: for checking a step's settings where no tenant is
known (`init/1`). Running a stored file for a tenant goes through
`bytes/3`, after `info/3` showed that the tenant stored it.

# `bytes`

```elixir
@spec bytes(String.t(), String.t(), String.t()) ::
  {:ok, binary()} | :not_found | {:error, String.t()}
```

The bytes of the tenant's stored file.

# `info`

```elixir
@spec info(String.t(), String.t(), String.t()) ::
  {:ok, info()} | :not_found | {:error, String.t()}
```

What is known of the tenant's stored file, without its bytes.

# `list`

```elixir
@spec list(String.t(), String.t()) ::
  {:ok, [info() | %{used_in: [String.t()]}]} | {:error, String.t()}
```

The tenant's stored files of `plugin_id`, newest first, without their
bytes, each with `used_in`, the names of the pipelines whose steps pin
it now: for a step that takes a file stored before instead of an upload.

# `put`

```elixir
@spec put(String.t(), String.t(), binary(), map(), keyword()) ::
  {:ok, %{digest: String.t(), size: non_neg_integer(), new?: boolean()}}
  | {:error, :quota, %{used: non_neg_integer(), limit: non_neg_integer()}}
  | {:error, String.t()}
```

Stores `bytes` for `tenant_id` and `plugin_id` unless the same file is
stored already (the first upload is kept). `meta` is display data (a
JSON-ready map). Takes `:uploaded_by` (a user id) and
`:max_tenant_bytes`, the most the tenant's files of this plugin may
hold together: a new file past it is `{:error, :quota, %{used:,
limit:}}`.

# `reclaim`

```elixir
@spec reclaim(String.t()) ::
  {:ok, %{removed: non_neg_integer(), bytes: non_neg_integer()}}
  | {:error, String.t()}
```

Removes the tenant's stored files, of any plugin, that nothing refers
to (see "Removal") now rather than at the next hourly cleanup: for a
new file that does not fit the quota.

# `used_bytes`

```elixir
@spec used_bytes(String.t(), String.t()) ::
  {:ok, non_neg_integer()} | {:error, String.t()}
```

The bytes the tenant's stored files of `plugin_id` hold together: for
refusing a file that cannot fit before costly work on it. `put/5`
checks the quota again as it stores.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
