@idpass/data-collect-core / AttachmentService
Class: AttachmentService
Defined in: services/AttachmentService.ts:59
Service for managing file attachments associated with entities.
Wraps an AttachmentStore with business logic including:
- SHA-256 hash computation for content integrity verification
- File size validation against configurable limits
- MIME type validation against configurable allow-lists
- Sync status tracking for offline-first upload workflows
Constructors
Constructor
new AttachmentService(
store,options?):AttachmentService
Defined in: services/AttachmentService.ts:64
Parameters
store
options?
Returns
AttachmentService
Methods
computeHash()
computeHash(
data):Promise<string>
Defined in: services/AttachmentService.ts:76
Compute a SHA-256 hash of the given data.
Uses the Web Crypto API (SubtleCrypto) when available (browser and Node 22+), falling back to Node.js crypto module for server environments.
Parameters
data
ArrayBuffer
Returns
Promise<string>
validateFileSize()
validateFileSize(
sizeBytes):void
Defined in: services/AttachmentService.ts:96
Validate file size against the configured maximum.
Parameters
sizeBytes
number
Returns
void
Throws
If the file exceeds the maximum allowed size.
validateMimeType()
validateMimeType(
mimeType):void
Defined in: services/AttachmentService.ts:111
Validate MIME type against the configured allow-list.
Parameters
mimeType
string
Returns
void
Throws
If the MIME type is not in the allowed list.
saveAttachment()
saveAttachment(
params,data):Promise<AttachmentMetadata>
Defined in: services/AttachmentService.ts:127
Save an attachment with automatic hash computation and validation.
Parameters
params
Attachment parameters (guid, entityGuid, filename, mimeType, tenantId)
guid
string
entityGuid
string
filename
string
mimeType
string
tenantId
string
data
ArrayBuffer
Binary content of the attachment
Returns
Promise<AttachmentMetadata>
The saved AttachmentMetadata with computed hash
Throws
If file size or MIME type validation fails
getAttachment()
getAttachment(
guid):Promise<{metadata:AttachmentMetadata;data:ArrayBuffer; } |null>
Defined in: services/AttachmentService.ts:165
Get an attachment's metadata and binary data by GUID.
Parameters
guid
string
Returns
Promise<{ metadata: AttachmentMetadata; data: ArrayBuffer; } | null>
getAttachmentMetadata()
getAttachmentMetadata(
guid):Promise<AttachmentMetadata|null>
Defined in: services/AttachmentService.ts:172
Get only the metadata for an attachment (without loading binary data).
Parameters
guid
string
Returns
Promise<AttachmentMetadata | null>
listAttachments()
listAttachments(
entityGuid):Promise<AttachmentMetadata[]>
Defined in: services/AttachmentService.ts:179
List all attachments for a specific entity.
Parameters
entityGuid
string
Returns
Promise<AttachmentMetadata[]>
deleteAttachment()
deleteAttachment(
guid):Promise<void>
Defined in: services/AttachmentService.ts:186
Delete an attachment and its binary data.
Parameters
guid
string
Returns
Promise<void>
getPendingAttachments()
getPendingAttachments(
tenantId):Promise<AttachmentMetadata[]>
Defined in: services/AttachmentService.ts:194
Get all attachments with 'pending' sync status for a tenant.
Parameters
tenantId
string
Returns
Promise<AttachmentMetadata[]>
updateSyncStatus()
updateSyncStatus(
guid,status):Promise<void>
Defined in: services/AttachmentService.ts:201
Update the sync status of an attachment.
Parameters
guid
string
status
"pending" | "uploaded" | "failed"
Returns
Promise<void>