Skip to main content
swift

Files

Uploading and managing files.

Info

File publicMeta and privateMeta fields support any kind of data formats encoded to byte arrays. Examples in this section use JSON format serialization which is available directly in Swift.

Uploading Files

Uploading file to a Store in the most basic way:

let storeID = "STORE_ID"
guard let fileContentData = "Text file content".data(using: .utf8 ) else {return}
guard let privMXEndpoint = endpointContainer.getEndpoint(nil) else {return}


guard let storeFileHandle = try? privMXEndpoint.storeApi?.createFile(
in: storeID,
withPublicMeta: Data(),
withPrivateMeta: Data(),
ofSize: Int64(fileContentData.count))
else {return}

try? privMXEndpoint.storeApi?.writeToFile(withHandle: storeFileHandle, uploading: fileContentData)

Getting Files

Fetching the most recent files in given Store:

var startIndex:Int64 = 0
var pageSize:Int64 = 100

let storeId = "STORE_ID"

guard let privMXEndpoint = endpointContainer.getEndpoint(nil) else {return}
guard let pagingList = try? privMXEndpoint.storeApi?
.listFiles(
from:storeId,
basedOn: .init(skip: startIndex, limit: pageSize, sortOrder: .desc)) else {return}

let files =
pagingList.readItems.map { $0 }

Managing Files

Define file privateMeta structure:

struct FilePrivateMeta:Codable{
let name: String
let mimetype: String
}
var fileID = "FILE_ID"

guard let privMXEndpoint = endpointContainer.getEndpoint(nil) else {return}
guard let file = try? privMXEndpoint.storeApi?
.getFile(fileID) else {return}

let newFilePrivateMeta = FilePrivateMeta(name: "New Filename.txt", mimetype: "text/plain")

guard let newFilePrivateMetaData = try? JSONEncoder().encode(newFilePrivateMeta) else {return}
guard let filePublicMetaData = file.publicMeta.getData() else {return}

try? privMXEndpoint.storeApi?
.updateFile(fileID,
replacingPublicMeta: filePublicMetaData,
replacingPrivateMeta: newFilePrivateMetaData,
replacingSize: file.size)