Files
Uploading and managing files.
Stores allow you to exchange and save files. In this section, we'll take a closer look at file structures and how to manage them effectively. We'll also cover some best practices when working with Stores.
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.\
Sample code on this page is based on the initial assumptions.
FilePrivateMeta structure used in the code below is defined as such:
About Files
Along with a file's main content, additional metadata is stored to allow easy file management. Below is the structure of the metadata associated with each file:
| field | type | encrypted | description |
|---|---|---|---|
info | ServerFileInfo | yes | additional information assigned by the server e.g. author, creationDate, storeID and fileID |
publicMeta | binary | no | additional public information about the message, also accessible through PrivMX Bridge API |
privateMeta | binary | yes | additional information about the message |
size | number | yes | The size of file in getBytes |
authorPubKey | string | yes | The public key of the author of the file |
statusCode | number | no | 0 if the file was decrypted successfully |
Uploading Files
The uploading process involves both uploading the file content and managing its associated metadata. Storing metadata, such as the file's name, and type, allows for easier identification and retrieval of files when needed.
We highly recommend storing the file's metadata along with the file content to make it easier to manage and interact with the uploaded file in the future.
The most basic way of uploading a File to a Store:
Retrieving Files
Downloading information about Files
To retrieve information about a singular File call getFile(_:) method:
Downloading a File
You can simply call the startDownloadingToFile(_:from:) method on the PrivMXEndpoint instance.
This utilizes the PrivMXStoreFileHandler under the hood.
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:):
Modifying 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:):
Deleting Files
To delete a File call deleteFile(_:) method.
Working with Random Write Files
Working with a Random Write File starts from acquiring a handle.
Once you have a handle you can read the File you can Read from it...
... move the handle to any byte of the File...
... or overwrite parts of the File.
The size of a File with Random Write functionality adjusts dynamically.
Once you finish working with the File you should close it.
Notes
- Metadata: helps you identify files later on by storing additional information about them. You can use both private metadata (which might contain sensitive or internal data) and public metadata (which can be shared publicly).
- Error Handling: During the streaming process, it's important to handle potential errors, such as network interruptions, by using try-catch blocks and the ability to abort the upload.
- Progress Monitoring: Streaming enables you to monitor the progress of the file upload, allowing you to display the current upload percentage to the user.
We use cookies on our website. We use them to ensure proper functioning of the site and, if you agree, for purposes such as analytics, marketing, and targeting ads.
PrivMX Endpoint Swift v2.6