Media interface
Asset fields, folder binding lifecycle, declared content and permission contracts for DMS consumers.
@antelopejs/interface-dms-media exports the asset field and folder binding declarations, and the functions that ship library folders and files as code. The Media runtime (@antelopejs/dms-media) implements them and provides the picker, the uploads and the provisioning. The HTTP routes and the delivery URLs are in the runtime guide.
Declare a field
This field goes in a DMS form. It stores up to four asset IDs and registers an upload folder in each tenant. The permission IDs belong to the application, and a role of the project grants them.
import { AssetType } from "@antelopejs/interface-dms-media";
export const articleImages = new AssetType({
multiple: true,
max: 4,
mimetypes: ["image/*"],
binding: {
id: "articles.images",
folderName: "Article images",
permissionMapping: {
read: ["articles.page"],
write: ["articles.page.table.edit"],
},
},
});
Selecting two images produces a value such as ["asset-a", "asset-b"], never two URLs. An empty selection is []. A fifth ID fails the field's shape validation. An unknown nonempty ID passes it, so the write path that consumes the value checks that the asset exists, belongs to the tenant and has the right type. The picker example shows the UI.
AssetType
import type { AssetBindingConfig } from "@antelopejs/interface-dms-media";
interface AssetTypeOptions extends Record<string, unknown> {
multiple?: boolean;
max?: number;
mimetypes?: string[];
binding?: AssetBindingConfig;
}
interface AssetPickerComponentOptions {
multiple?: boolean;
max?: number;
mimetypes?: string[];
bindingId?: string;
}
new AssetType(options?: AssetTypeOptions) returns a DMS DataType. With the default options, validation accepts one nonempty asset ID string. multiple: true accepts an array of nonempty strings, the empty array included. max caps the array length, and only in multiple mode. getValidation() returns a Zod schema that checks this shape. It does not check that the asset exists, who owns it or its MIME type.
mimetypes limits the picker to types such as image/* or application/pdf. The server also refuses an upload into a bound folder, or into one of its subfolders, whose type the binding's field does not accept. The runtime upload policy applies on top. Neither the form schema nor the filtering in the client is an authorization check.
Constructing a field with binding registers that binding and fills its field metadata from multiple, max and mimetypes. The picker receives AssetPickerComponentOptions with the binding's ID, never its ACL. The package exports two constants, ASSET_DATA_TYPE_ID = "asset" and ASSET_PICKER_COMPONENT = "dms-media-asset-picker". Importing the package registers the data type.
RegisterAssetBinding and UnregisterAssetBinding
import type { ControllerClass } from "@antelopejs/interface-api";
type AclRight = "read" | "write" | "manage";
type AclSubjectKind = "permission" | "role";
interface AclSubject {
kind: AclSubjectKind;
id: string;
}
interface AclEntry {
subject: AclSubject;
rights: AclRight[];
}
interface AssetPermissionMapping {
read?: string[];
write?: string[];
}
interface AssetBindingField {
multiple?: boolean;
max?: number;
mimetypes?: string[];
}
interface AssetBindingConfig {
id: string;
folderName?: string;
permissionMapping?: AssetPermissionMapping;
permissionsFromPage?: ControllerClass;
acl?: AclEntry[];
subfolders?: boolean;
field?: AssetBindingField;
}
declare function RegisterAssetBinding(config: AssetBindingConfig): void;
declare function UnregisterAssetBinding(id: string): void;
All these types and functions are available from the package root. The exported internal proxy namespace is not a consumer API.
A binding ID is stable and namespaced, such as articles.cover. The runtime keys registrations by ID, so a second registration with the same ID replaces the first one. It does not create a second binding. Registration returns at once and does not wait for any folder. The calling module owns the registration. Unloading that module releases it, and a reloaded implementation receives every active registration again.
The runtime provisions each tenant separately, under a private Content root, the first time a request or a declaration reads that tenant's folders after a registration changes. A new folder is named folderName, or the binding id when folderName is absent. An existing bound folder keeps its name and takes the current ACL. UnregisterAssetBinding removes the registration and leaves the provisioned folders and files in place.
A bound folder is locked. The library refuses to rename, move or delete it, and its ACL always comes from the binding. By default it holds no subfolders. subfolders: true lets it hold them, for a layout per record such as one folder per project under the field's folder. Owners then create the subfolders in the library, and backend code declares them with EnsureMediaFolder({ parent: { binding } }). The bound folder itself stays locked. A subfolder inherits the bound folder's ACL unless it has its own, and accepts only the types the field accepts. The field's picker opens on the bound folder and browses its subfolders. Its upload button always targets the bound folder.
Neither function returns an upload result or a provisioning error. A provisioning or storage failure surfaces later, in the runtime operation that provisions the tenant's folders.
Permission precedence and tenant access
A binding's ACL comes from the first rule that applies:
- An explicit
acl,[]included, replaces every derived entry. - Otherwise,
permissionMapping.readandpermissionMapping.writeeach take precedence over the permission derived from the page, one right at a time. An explicit empty array turns off the fallback for that right. - A right with no mapping falls back to the effective permission of
permissionsFromPage. The runtime resolves it at provisioning time, so a field can name the page class that contains it.
A read mapping grants read, and a write mapping grants write.
Access model
A right includes the weaker rights. manage includes write and read, and write includes read. A role grants the page permission explicitly. A page that a member holds only through memberAccess or publicAccess grants no folder right. The runtime's Media administration pages stay owner-only. Members reach the files they may access through asset fields on the project's pages, never through a Media page permission.
Owners hold every folder right. Uploading and changing files need the folder's write right. Changing the folder structure needs manage. Changing the visibility of a file or a folder is reserved to owners, and so is moving a file to a folder that changes its effective visibility. A member with write on a linked folder can change any of its files, whoever uploaded them. The default root ACL (rootAcl) grants members nothing. A folder without an ACL of its own takes its parent's rights, and a folder at the root takes those of rootAcl. A grant in rootAcl therefore reaches every folder whose ancestors set no ACL either, and can open the rest of the library.
EnsureMediaFolder and ImportMediaAsset
A project ships media content, such as templates, procedures and demo pictures, from its backend code with these two functions. The Media runtime creates the content itself. It checks the declaration with the dashboard's rules, writes the bytes to its configured file storage, computes the image dimensions and records the creation in the library activity. The runtime's tables are not part of the contract, and no module writes them directly.
import type { AclEntry } from "@antelopejs/interface-dms-media";
type MediaFolderVisibility = "private" | "public";
interface MediaFolderDeclaration {
key: string;
name: string;
parent?: string | MediaFolderTarget;
visibility?: MediaFolderVisibility;
acl?: AclEntry[];
tenantId: string;
}
interface DeclaredFolderTarget {
key: string;
}
interface BoundFolderTarget {
binding: string;
}
type MediaFolderTarget = DeclaredFolderTarget | BoundFolderTarget;
interface MediaBytesSource {
bytes: Uint8Array;
}
interface MediaFileSource {
filePath: string;
}
interface MediaLazySource {
load: () => Uint8Array | Promise<Uint8Array>;
}
type MediaAssetSource = MediaBytesSource | MediaFileSource | MediaLazySource;
interface MediaAssetDeclaration {
key: string;
folder: MediaFolderTarget;
name: string;
mimetype: string;
source: MediaAssetSource;
alt?: string;
tenantId: string;
}
type MediaDeclarationStatus = "created" | "kept";
interface MediaDeclarationResult {
status: MediaDeclarationStatus;
id: string;
}
declare function EnsureMediaFolder(
declaration: MediaFolderDeclaration,
): Promise<MediaDeclarationResult>;
declare function ImportMediaAsset(
declaration: MediaAssetDeclaration,
): Promise<MediaDeclarationResult>;
keyis a stable, namespaced ID that the caller chooses, such ashelio.procedures. The runtime trims it and accepts 1 to 255 characters. It keys declarations by tenant, kind and key, so it creates each folder and each file once.- A folder goes at the library root, under the declared folder that
parentnames (by its key, as a string or as{ key }), or under the folder bound to an asset field ({ binding }) when that binding setssubfolders: true.visibilitydefaults toprivate. Withoutacl, the folder inherits its parent's ACL, orrootAclat the root. - A file goes into a declared folder (
{ key }) or into the folder bound to an asset field ({ binding }). The binding is theidgiven toAssetTypeorRegisterAssetBinding, and the runtime provisions its folder first.sourceis the content in memory (bytes), a path on the backend's file system (filePath) or a function that produces it (load).nameis the file name shown in the library, extension included. - The runtime reads the source only when it creates the file. A key declared before returns
kept, and the runtime does not read its source. tenantIdis required in TypeScript and at runtime. An omitted, empty or whitespace-only value rejects before any database write or source read. There is no default. PassingDEFAULT_TENANT_IDexplicitly is valid.idin the result is the library ID of the folder or file. AnAssetTypevalue can store it, for example.
Written once
Declared content is written once, when the tenant is provisioned (see When to call). From then on it belongs to the tenant's members. Nothing replays a declaration on a later boot, so nothing compares the library with the code or takes content back. A declaration has one of two outcomes:
- No declaration of the key exists in this tenant. The runtime creates the folder or file and returns
created. It remembers the new ID under the key, so a later declaration can name the folder as aparentor a{ key }target. - An earlier or retried provisioning declared the key. The runtime writes nothing, does not read the source and returns
kept, with the ID of the row created then.
The activity records only creations, as folder.create and asset.upload, attributed to System. The queue that orders the declarations of one key is local to the process. Creation, storage, activity and declaration bookkeeping are not one transaction. One declaration writer per tenant and key is therefore the supported setup.
Validation and errors
Declarations follow the dashboard's rules. A refused declaration rejects with an error that names the kind, the key and the reason, such as Media folder 'helio.procedures': a folder named 'Procedures' already exists there. The rules are these:
- A folder name refuses
/ \ < > : " | ? *and control characters, has at most 255 characters and is unique among its siblings, ignoring case and Unicode form. - A file name refuses
/,\and control characters and has at most 255 characters. - A
parentor{ key }folder must be declared first and not deleted since. - A folder cannot name itself as its parent.
- A
{ binding }folder must be registered, and a{ binding }parent must allowsubfolders. - Each
aclentry grants at least one right. - A file is not empty and has at most 500 MiB. Its type is allowed by
allowedMimetypesand is never HTML, XHTML or PHP. The bound field accepts its type, and it fits within the storage quota. - A file whose mimetype names an image format that the runtime decodes must decode. The runtime stores the decoded format's mimetype, as it does for an upload.
- Alt text has at most 255 characters.
When to call
The functions belong in a Hook.TENANT_BEING_PROVISIONED handler registered in the module's construct(). The DMS fires that hook once per tenant, when the tenant is created. For the default tenant, it fires on the DMS start that creates the tenant, once every Hook.DATABASE_INITIALIZED handler has finished. For a workspace, the SaaS module fires it when it creates the workspace. The tenant's tables exist by then, and the runtime writes files through the storage's WriteFile, so nothing waits. start() and DATABASE_INITIALIZED handlers run on every boot, so they are the wrong place for these calls. A parent is declared before its children and a folder before its files, each call awaited.
Tenant scope and trusted backend context
tenantId selects the existing DMS tenant schema instance that holds the folders, the files and the declaration registry. Parent keys and bindings resolve within that tenant, and different tenants can use the same declaration key independently. Both functions require an explicit tenant that is not blank. Neither infers it from an HTTP session or a SaaS workspace context. The runtime trims surrounding whitespace.
These are trusted backend APIs, not authorization endpoints. They need the media implementation, the selected tenant's database instance and reachable file storage. They do not require a signed-in user, create a workspace, check a payment status or decide when the provisioning of a workspace is committed. Storage paths include the tenant ID, and the storage backend and the upload policy stay module configuration. The activity attributes the events to System.
import { Hook, RegisterHook } from "@antelopejs/interface-dms/hooks";
import {
EnsureMediaFolder,
ImportMediaAsset,
type MediaDeclarationResult,
} from "@antelopejs/interface-dms-media";
async function declareProcedures(
tenantId: string,
pdf: Uint8Array,
): Promise<MediaDeclarationResult[]> {
const root = await EnsureMediaFolder({
tenantId,
key: "helio.procedures",
name: "Procedures",
});
const safety = await EnsureMediaFolder({
tenantId,
key: "helio.procedures.safety",
name: "Safety",
parent: "helio.procedures",
acl: [
{
subject: { kind: "permission", id: "projects.page" },
rights: ["read"],
},
],
});
const file = await ImportMediaAsset({
tenantId,
key: "helio.procedures.safety.roof",
folder: { key: "helio.procedures.safety" },
name: "Roof safety checklist.pdf",
mimetype: "application/pdf",
source: { bytes: pdf },
});
return [root, safety, file];
}
export function construct(pdf: Uint8Array): void {
RegisterHook(Hook.TENANT_BEING_PROVISIONED, async ({ tenantId }) => {
await declareProcedures(tenantId, pdf);
});
}