Assets service¶
consortium.server.services.assets_service
¶
AssetsService(events_service, repository_service, user_accounts_service)
¶
repository_directory_path = repository_service.repository_directory_path
instance-attribute
¶
load_repository_metadata()
¶
save_repository_metadata()
¶
reserve_resource_id()
¶
Reserves and returns a new resource ID without creating any asset.
The returned ID can later be passed as resource_id to one of the asset
creation methods to claim it. Reserving an ID up front lets a caller learn the
asset's ID before its file or directory exists on disk (for example to embed the
ID inside the content that will be stored).
Returns:
| Type | Description |
|---|---|
UUID
|
The freshly reserved resource ID, unique across the repository. |
create_asset_file(content, name=None, description='', resource_id=None, user_account_id=None)
async
¶
Creates a new asset file from in-memory or streamed content.
The content is written to a new file on disk in the assets repository and an
ASSET_CREATED event is emitted. When a user_account_id is supplied the
uploading user account is resolved and recorded against the asset for
attribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str | bytes | TextIO | BinaryIO
|
The content to write into the new asset file, supplied either directly as text/bytes or as an open text/binary stream to read the content from. |
required |
name
|
str | None
|
A human-readable display name for the asset. When |
None
|
description
|
str
|
A short human-readable description of the asset. Defaults to an empty string when omitted. |
''
|
resource_id
|
str | UUID | None
|
A previously reserved resource ID to claim for this asset. When
|
None
|
user_account_id
|
str | UUID | None
|
The ID of the user account that uploaded this asset,
recorded for attribution. When |
None
|
Returns:
| Type | Description |
|---|---|
Asset
|
The newly created asset file resource. |
Raises:
| Type | Description |
|---|---|
UserAccountIDNotFoundError
|
If |
ResourceIDReservationNotFoundError
|
If |
RepositoryResourceFileSystemError
|
If the file cannot be written to disk. |
RepositoryMetadataFileSystemError
|
If the asset metadata cannot be written to disk after the file is written. |
UnicodeDecodeError
|
If |
add_asset_file(path, name=None, description='', resource_id=None, copy=False, user_account_id=None)
async
¶
Registers an existing file on disk as an asset.
Unlike create_asset_file, no new content is written: the file at path is
moved (or copied when copy=True) into the assets repository, registered as a
resource, and an ASSET_CREATED event is emitted. When a user_account_id is
supplied the uploading user account is resolved and recorded for attribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Filesystem path to the existing file to ingest as an asset. |
required |
name
|
str | None
|
A human-readable display name for the asset. When |
None
|
description
|
str
|
A short human-readable description of the asset. Defaults to an empty string when omitted. |
''
|
resource_id
|
str | UUID | None
|
A previously reserved resource ID to claim for this asset. When
|
None
|
copy
|
bool
|
When |
False
|
user_account_id
|
str | UUID | None
|
The ID of the user account that uploaded this asset,
recorded for attribution. When |
None
|
Returns:
| Type | Description |
|---|---|
Asset
|
The newly registered asset file resource. |
Raises:
| Type | Description |
|---|---|
UserAccountIDNotFoundError
|
If |
ResourceIDReservationNotFoundError
|
If |
RepositoryResourceFileSystemError
|
If no file exists at |
RepositoryMetadataFileSystemError
|
If the asset metadata cannot be written to disk afterwards. |
create_asset_directory(content=None, archive_file_format=None, name=None, description='', resource_id=None, user_account_id=None)
async
¶
Creates a new asset directory, optionally populated from existing content.
An empty directory is created on disk in the assets repository, or, when
content is provided, it is populated from that content. How content is
interpreted depends on its type: raw bytes and open binary streams are unpacked
as archive content using archive_file_format, while a str or pathlib.Path
is treated as a path to an existing source directory whose tree is copied in. An
ASSET_CREATED event is emitted. When a user_account_id is supplied the
uploading user account is resolved and recorded for attribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
bytes | BinaryIO | str | Path | None
|
Archive content to unpack into the new directory, supplied as raw
bytes or an open binary stream, or a path to an existing source directory
to copy in. When |
None
|
archive_file_format
|
Literal['zip', 'tar', 'gztar', 'bztar', 'xztar'] | None
|
The archive format used to interpret |
None
|
name
|
str | None
|
A human-readable display name for the asset. When |
None
|
description
|
str
|
A short human-readable description of the asset. Defaults to an empty string when omitted. |
''
|
resource_id
|
str | UUID | None
|
A previously reserved resource ID to claim for this asset. When
|
None
|
user_account_id
|
str | UUID | None
|
The ID of the user account that uploaded this asset,
recorded for attribution. When |
None
|
Returns:
| Type | Description |
|---|---|
Asset
|
The newly created asset directory resource. |
Raises:
| Type | Description |
|---|---|
UserAccountIDNotFoundError
|
If |
ResourceIDReservationNotFoundError
|
If |
InvalidRepositoryDirectoryArchiveFileFormatError
|
If |
RepositoryResourceFileSystemError
|
If the directory cannot be created or the source directory or archive cannot be unpacked into it. |
RepositoryMetadataFileSystemError
|
If the asset metadata cannot be written to disk after the directory is created. |
add_asset_directory(path, name=None, description='', resource_id=None, copy=False, user_account_id=None)
async
¶
Registers an existing directory on disk as an asset.
Unlike create_asset_directory, no new directory is created: the directory at
path is moved (or copied when copy=True) into the assets repository,
registered as a resource, and an ASSET_CREATED event is emitted. When a
user_account_id is supplied the uploading user account is resolved and recorded
for attribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Filesystem path to the existing directory to ingest as an asset. |
required |
name
|
str | None
|
A human-readable display name for the asset. When |
None
|
description
|
str
|
A short human-readable description of the asset. Defaults to an empty string when omitted. |
''
|
resource_id
|
str | UUID | None
|
A previously reserved resource ID to claim for this asset. When
|
None
|
copy
|
bool
|
When |
False
|
user_account_id
|
str | UUID | None
|
The ID of the user account that uploaded this asset,
recorded for attribution. When |
None
|
Returns:
| Type | Description |
|---|---|
Asset
|
The newly registered asset directory resource. |
Raises:
| Type | Description |
|---|---|
UserAccountIDNotFoundError
|
If |
ResourceIDReservationNotFoundError
|
If |
RepositoryResourceFileSystemError
|
If no directory exists at |
RepositoryMetadataFileSystemError
|
If the asset metadata cannot be written to disk afterwards. |
update_asset_by_resource_id(resource_id, name=None, description=None, user_account_id=_UNSET)
async
¶
Updates an asset's mutable metadata.
name and description are updated in place when provided. The asset's stored
data is only rebuilt when user_account_id is passed, mirroring the attribution
parameter of the asset creation methods: the given user account is resolved into a
stored reference exactly as it would be on creation. When user_account_id is
omitted the existing data is left untouched. An ASSET_UPDATED event is emitted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_id
|
str | UUID
|
The ID of the asset to update. |
required |
name
|
str | None
|
A new human-readable display name for the asset. When |
None
|
description
|
str | None
|
A new description for the asset. When |
None
|
user_account_id
|
str | UUID | None
|
When provided, rebuilds the asset's attribution data from
this user account ID (or clears attribution when |
_UNSET
|
Returns:
| Type | Description |
|---|---|
Asset
|
The updated asset resource. |
Raises:
| Type | Description |
|---|---|
ResourceNotFoundError
|
If no asset with the given ID exists. |
UserAccountIDNotFoundError
|
If |
RepositoryMetadataFileSystemError
|
If the updated metadata cannot be written to disk. |
delete_asset_by_resource_id(resource_id)
async
¶
Deletes an asset from disk and the repository.
The asset's metadata is snapshotted before removal so it can be carried on the
emitted ASSET_DELETED event, then the resource is deleted from disk and
deregistered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_id
|
str | UUID
|
The ID of the asset to delete. |
required |
An asset whose file or directory is already missing from the repository directory is deleted successfully: the record is deregistered and the event is still emitted.
Raises:
| Type | Description |
|---|---|
ResourceNotFoundError
|
If no asset with the given ID exists. |
RepositoryResourceFileSystemError
|
If the asset's file or directory exists on disk but cannot be deleted. |
RepositoryMetadataFileSystemError
|
If the metadata file cannot be written to disk after deletion. |
get_all_assets()
¶
Returns every asset currently tracked by the assets service.
Returns:
| Type | Description |
|---|---|
list[Asset]
|
A list of all assets, covering both file and directory assets, each wrapping its repository resource. Empty if no assets exist. |
get_asset_by_resource_id(resource_id)
¶
Returns a single asset by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resource_id
|
str | UUID
|
The ID of the asset to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Asset
|
The requested asset resource, either a file or a directory depending on how it was created. |
Raises:
| Type | Description |
|---|---|
ResourceNotFoundError
|
If no asset with the given ID exists. |