PrivMX DOCS
Swift

Files

Working with large data.

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

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

FilePrivateMeta structure used in the code below is defined as such:

struct FilePrivateMeta:Codable{
    let name: String
    let mimetype: String
}

Uploading Files

The most basic way of uploading a File to a Store:

let storeID = "STORE_ID"
let fileContentData = Data("Text file content".utf8)

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

try? endpointSession?.storeApi?.writeToFile(
	withHandle: storeFileHandle,
	uploading: fileContentData,
	truncate: false)
let storeFileId = try? endpointSession?.storeApi?.closeFile(withHandle: storeFileHandle)

Retrieving Files

Downloading information about Files

To retrieve information about a singular File call getFile(_:) method:

let fileID = "FILE_ID"
guard let file = try? endpointSession?.storeApi?
    .getFile(fileID) else {return}

Downloading a File

You can simply call the startDownloadingToFile(_:from:) method on the PrivMXEndpoint instance.
This utilizes the PrivMXStoreFileHandler under the hood.

let fileID = "FILE_ID"

let URL = URL(fileURLWithPath: "/path/to/file/Filename.txt")

guard let fileHandle =  try? FileHandle(forWritingTo: URL) else { return }

Task{
    try? await endpointSession?.startDownloadingToFile(fileHandle, from: fileID)
}

Seeking in a File

Once you acquire a handle to the File by calling openFile(_:), you can move the handle to point at any byte of the file by calling seekInFile(withHandle:toPosition:):

let fileId = "FILE_ID"
guard let readHandle = try? endpointSession?.storeApi?.openFile(fileId)
else {return}
 
try? endpointSession?.storeApi?.seekInFile(
    withHandle: readHandle,
    toPosition: 128)

Managing Files

Updating a File

There are two ways to update a File. You can either only update its metadata, or you can reupload a new version of the File. The latter can be done in three ways similar to uploading a new file.

To only update the File's metadata call updateFileMeta(of:replacingPublicMeta:replacingPrivateMeta:):

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

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

try? endpointSession?.storeApi?
    .updateFileMeta(
        of: fileID,
        replacingPublicMeta:  filePublicMetaData,
        replacingPrivateMeta: newFilePrivateMetaData)

Deleting Files

To delete a File call deleteFile(_:) method.

let fileID = "FILE_ID"
try? endpointSession?.storeApi?.deleteFile(fileID)

Working with Random Write Files

Working with a Random Write File starts from acquiring a handle.

let fileId = "FILE_ID_WITH_RW_SUPPORT"
guard let rwHandle = try? endpointSession?.storeApi?.openFile(fileId) 
else {return}

Once you have a handle you can read the File you can Read from it...

var readData = try? endpointSession?.storeApi?.readFromFile(withHandle: rwHandle, length: 128)

... move the handle to any byte of the File...

try? endpointSession?.storeApi?.seekInFile(
    withHandle: rwHandle,
    toPosition: 64)

... or overwrite parts of the File.

try? endpointSession?.storeApi?.writeToFile(
	withHandle: rwHandle,
	uploading: Data("some new data".utf8),
	truncate: true)

The size of a File with Random Write functionality adjusts dynamically.

Once you finish working with the File you should close it.

try? endpointSession?.storeApi?.closeFile(withHandle: rwHandle)

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.

On this page