PrivMX DOCS
Kotlin

Files

Uploading and managing files.

The sample code on this page is based on the initial assumptions.

File publicMeta and privateMeta fields support any kind of data formats encoded to byte arrays. Examples in this section use kotlinx-serialization-json dependency to serialize object to JSON format.

Uploading Files

Uploading file to a Store in the most basic way:

val storeID = "STORE_ID"
val fileContent = "Text file content".encodeToByteArray()
val publicMeta = ByteArray(0)
val privateMeta = ByteArray(0)

val fileId = StoreFileStreamWriter.createFile(
    storeApi,
    storeID,
    publicMeta,
    privateMeta,
    fileContent.size.toLong(),
).also {
    it.write(fileContent)
}.close()

Getting Files

Define file item class with decoded privateMeta.

data class FileItem(
    val file: File,
    val decodedPrivateMeta: FilePrivateMeta
)

Fetching the most recent files in given Store:

val storeId = "STORE_ID"
val startIndex = 0L
val pageSize = 100L

val filesPagingList = storeApi.listFiles(
    storeId,
    startIndex,
    pageSize,
    SortOrder.DESC
)

val files = filesPagingList.readItems.map {
    FileItem(
        it,
        Json.decodeFromString(it.privateMeta.decodeToString())
    )
}

Managing Files

val fileID = "FILE_ID"
val file = storeApi.getFile(fileID)
var filePrivateMeta: FilePrivateMeta = Json.decodeFromString(file.privateMeta.decodeToString())
filePrivateMeta = filePrivateMeta.copy(
    name = "New file name"
)

storeApi.updateFileMeta(
    fileID,
    file.publicMeta,
    Json.encodeToString(filePrivateMeta).encodeToByteArray(),
)

We use cookies on our website. We use them to ensure the proper functioning of the site and, if you agree, for purposes we set, such as analytics or marketing.

PrivMX Endpoint Kotlin v2.2

This package is not up to date with the core documentation. Some of the features you've seen described in other parts of the documentation might not be mentioned here. Those changes do not influence compatibility, however

On this page