Alternera. Plugin. Provisional. Files behaviour
(alternera_plugin_sdk v0.9.0)
Copy Markdown
View Source
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.
Summary
Types
What the application knows of a stored file.
A file a step pins: its digest, and the name the step gives it.
Callbacks
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.
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.
The extension an archive gives the plugin's files (bin when not given).
The stored files a step's settings pin.
Functions
The bytes of the tenant's stored file.
What is known of the tenant's stored file, without its bytes.
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.
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:}}.
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.
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.
Types
@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.
A file a step pins: its digest, and the name the step gives it.
Callbacks
@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.
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.
@callback file_extension() :: String.t()
The extension an archive gives the plugin's files (bin when not given).
The stored files a step's settings pin.
Functions
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.
@spec bytes(String.t(), String.t(), String.t()) :: {:ok, binary()} | :not_found | {:error, String.t()}
The bytes of the tenant's stored file.
What is known of the tenant's stored file, without its bytes.
@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.
@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:}}.
@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.
@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.