AttachmentQueue

open class AttachmentQueue(val db: PowerSyncDatabase, val remoteStorage: RemoteStorage, attachmentsDirectory: String, watchAttachments: () -> Flow<List<WatchedAttachmentItem>>, val localStorage: LocalStorage = IOLocalStorageAdapter(), attachmentsQueueTableName: String = DEFAULT_TABLE_NAME, errorHandler: SyncErrorHandler? = null, syncInterval: Duration = 30.seconds, archivedCacheLimit: Long = 100, syncThrottleDuration: Duration = 1.seconds, subdirectories: List<String>? = null, downloadAttachments: Boolean = true, val logger: Logger = Logger, coroutineScope: CoroutineScope? = null)

Class used to implement the attachment queue. Requires a PowerSyncDatabase, an implementation of AbstractRemoteStorageAdapter, and an attachment directory name which will determine which folder attachments are stored into.

Constructors

Link copied to clipboard
constructor(db: PowerSyncDatabase, remoteStorage: RemoteStorage, attachmentsDirectory: String, watchAttachments: () -> Flow<List<WatchedAttachmentItem>>, localStorage: LocalStorage = IOLocalStorageAdapter(), attachmentsQueueTableName: String = DEFAULT_TABLE_NAME, errorHandler: SyncErrorHandler? = null, syncInterval: Duration = 30.seconds, archivedCacheLimit: Long = 100, syncThrottleDuration: Duration = 1.seconds, subdirectories: List<String>? = null, downloadAttachments: Boolean = true, logger: Logger = Logger, coroutineScope: CoroutineScope? = null)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Service which provides access to attachment records. Use this to:

Link copied to clipboard
Link copied to clipboard

PowerSync database client.

Link copied to clipboard

Provides access to local filesystem storage methods.

Link copied to clipboard
val logger: Logger

Logging interface used for all log operations.

Link copied to clipboard

Adapter which interfaces with the remote storage backend.

Functions

Link copied to clipboard
suspend fun clearQueue()

Clears the attachment queue and deletes all attachment files.

Link copied to clipboard
suspend fun close()

Closes the queue. The queue cannot be used after closing. A new queue should be created.

Link copied to clipboard
open suspend fun deleteFile(attachmentId: String, updateHook: (context: ConnectionContext, attachment: Attachment) -> Unit): Attachment

A function which creates an attachment delete operation locally. This operation is queued for delete. The default implementation assumes the attachment record already exists locally. An exception is thrown if the record does not exist locally. This method can be overridden for custom behavior.

Link copied to clipboard
suspend fun expireCache()

Removes all archived items.

Link copied to clipboard
open fun getLocalUri(filename: String): String

Returns the user's storage directory with the attachment path used to load the file. Example: filePath: "attachment-1.jpg" returns "/data/user/0/com.yourdomain.app/files/attachments/attachment-1.jpg".

Link copied to clipboard

Processes attachment items returned from watchAttachments. The default implementation asserts the items returned from watchAttachments as the definitive state for local attachments.

Link copied to clipboard
open suspend fun resolveNewAttachmentFilename(attachmentId: String, fileExtension: String?): String

Resolves the filename for new attachment items. A new attachment from watchAttachments might not include a filename. Concatenates the attachment ID and extension by default. This method can be overridden for custom behavior.

Link copied to clipboard
open suspend fun saveFile(data: Flow<ByteArray>, mediaType: String, fileExtension: String? = null, metaData: String? = null, updateHook: (context: ConnectionContext, attachment: Attachment) -> Unit): Attachment

A function which creates a new attachment locally. This new attachment is queued for upload after creation.

Link copied to clipboard
suspend fun startSync()

Initialize the attachment queue by:

Link copied to clipboard
suspend fun stopSyncing()

Stops syncing. Syncing may be resumed with startSync.