Skip to main content

SyncingService

Internal

Orchestrates attachment synchronization between local and remote storage. Handles uploads, downloads, deletions, and state transitions.

Constructors

new SyncingService()

new SyncingService(
attachmentService,
localStorage,
remoteStorage,
logger,
errorHandler?): SyncingService

Parameters

ParameterType
attachmentServiceAttachmentService
localStorageLocalStorageAdapter
remoteStorageRemoteStorageAdapter
loggerILogger
errorHandler?AttachmentErrorHandler

Returns

SyncingService

Methods

deleteArchivedAttachments()

deleteArchivedAttachments(context): Promise<boolean>

Performs cleanup of archived attachments by removing their local files and records. Errors during local file deletion are logged but do not prevent record deletion.

Parameters

ParameterType
contextAttachmentContext

Returns

Promise<boolean>


deleteAttachment()

deleteAttachment(attachment): Promise<AttachmentRecord>

Deletes an attachment from both remote and local storage. Removes the remote file, local file (if exists), and the attachment record. On failure, defers to error handler or archives.

Parameters

ParameterTypeDescription
attachmentAttachmentRecordThe attachment record to delete

Returns

Promise<AttachmentRecord>

Updated attachment record


downloadAttachment()

downloadAttachment(attachment): Promise<AttachmentRecord>

Downloads an attachment from remote storage to local storage. Retrieves the file, converts to base64, and saves locally. On success, marks as SYNCED. On failure, defers to error handler or archives.

Parameters

ParameterTypeDescription
attachmentAttachmentRecordThe attachment record to download

Returns

Promise<AttachmentRecord>

Updated attachment record with local URI and new state


processAttachments()

processAttachments(attachments, context): Promise<void>

Processes attachments based on their state (upload, download, or delete). All updates are saved in a single batch after processing.

Parameters

ParameterTypeDescription
attachmentsAttachmentRecord[]Array of attachment records to process
contextAttachmentContextAttachment context for database operations

Returns

Promise<void>

Promise that resolves when all attachments have been processed and saved


uploadAttachment()

uploadAttachment(attachment): Promise<AttachmentRecord>

Uploads an attachment from local storage to remote storage. On success, marks as SYNCED. On failure, defers to error handler or archives.

Parameters

ParameterTypeDescription
attachmentAttachmentRecordThe attachment record to upload

Returns

Promise<AttachmentRecord>

Updated attachment record with new state

Throws

Error if the attachment has no localUri