Storage & Files
PAM Desktop gives every application three places to keep data, each run by the host on its own thread: a small key/value store for preferences, the system keychain for secrets, and a private file sandbox for documents and downloads. Like every module, they are asynchronous: a call returns at once and its result arrives in a callback, after which the component renders again. For structured data, use SQLite.
Key/value storage
Section titled “Key/value storage”Pam\Desktop\Storage\Storage is a durable store for strings: preferences, the last opened document, a cached response.
use Pam\Desktop\Storage\Storage;
Storage::set('theme', 'dark');
Storage::get('theme', function (?string $value): void { $this->theme = $value ?? 'light';});Store structured values as JSON:
Storage::set('recent', json_encode($this->recent));
Storage::get('recent', function (?string $json): void { $this->recent = $json === null ? [] : json_decode($json, true);});- A value holds at most 262,144 bytes.
StorageBudget::encodeList()andencodeObject()trim JSON to fit. - Keys match
[A-Za-z0-9_.-]{1,128}; other identifiers are mapped byStorage::safeKey(), whichgetandsetapply for you. get($key, $callback, $onError)andset($key, $value, $callback, $onError)pass failures to$onError, or throw without one.
Values live in storage.json in the data directory: .pam-desktop/data/ during development.
Secure storage
Section titled “Secure storage”Pam\Desktop\Storage\SecureStorage keeps small secrets (tokens, keys, PIN hashes) in the system keychain: the Secret Service on Linux, the Keychain on macOS and the Credential Manager on Windows.
use Pam\Desktop\Storage\SecureStorage;
SecureStorage::set('api-token', $token, function (bool $stored): void { $this->signedIn = $stored;});
SecureStorage::get('api-token', function (?string $token): void { $this->token = $token; // null: no token stored}, function (string $error): void { $this->error = 'The system keychain is unavailable.';});
SecureStorage::delete('api-token', fn (bool $deleted) => $this->signedIn = false);- Keys hold 1 to 256 characters and values at most 65,536.
- A value too large for a keychain entry is stored encrypted with AES-256-GCM under
secure/in the data directory, with a random key that is itself the keychain entry. - Nothing is ever stored in the clear. When no keychain can be reached (a Linux session without a Secret Service), every call fails. Pass the third argument to
get()to tell “unavailable” apart from “absent”.
Pam\Desktop\System\Files is the application’s private file sandbox. Every path is relative to pam-files/ in the data directory, and a path that would leave it, through .. or a symbolic link, is refused.
use Pam\Desktop\FileReference;use Pam\Desktop\System\Files;
Files::write('notes/today.txt', $this->text, fn () => $this->saved = true);
Files::read('notes/today.txt', function (string $contents): void { $this->text = $contents;});
Files::list('notes', function (array $files): void { $this->names = array_map(static fn (FileReference $file): string => $file->name, $files);});File contents cross to PHP only in read and write, one mebibyte at most. Everything else (copies, downloads, imports) moves bytes inside the host and hands PHP a FileReference with path, name, mimeType and size. FileReference::uri() returns a pam-file:/// URI that Image and MediaPlayer accept as a source.
| Call | Success | Failure |
|---|---|---|
read, write, list, delete |
contents, nothing, list<FileReference>, nothing |
Thrown |
exists($path, $callback, $failure) |
bool |
Failure closure |
find($path, $callback, $failure) |
?FileReference |
Failure closure |
stat($path, $callback, $missing, $failure) |
FileReference |
$missing for a missing file |
move, copy($from, $to, $callback, $failure, $overwrite) |
FileReference |
Failure closure |
makeDirectory($path, $callback, $failure) |
The normalized path | Failure closure |
sha256($path, $callback, $failure) |
Hex digest | Failure closure |
copyAsset($assetPath, $destination, $callback) |
FileReference of a copy of a bundled file |
Thrown |
open($path, $mimeType, $callback, $failure) |
Opened in the default application | Failure closure |
Saving a file the user chooses
Section titled “Saving a file the user chooses”Files::save() opens the native save dialog and writes the contents (at most 1 MiB) where the user chose. It is the one call that writes outside the sandbox, and only to the path picked in the dialog:
Files::save($csv, 'report.csv', function (bool $saved): void { $this->status = $saved ? 'Saved.' : 'Cancelled.';}, function (string $error): void { $this->status = $error;});The file is written to a temporary file next to the destination and moved over it, so a failed write never leaves half a file.
Downloads
Section titled “Downloads”use Pam\Desktop\FileDownloadProgress;
$this->download = Files::downloadWithProgress( 'https://example.com/report.pdf', 'downloads/report.pdf', fn (FileReference $file) => $this->report = $file->path, fn (FileDownloadProgress $progress) => $this->received = $progress->bytesWritten, failure: fn (string $message) => $this->error = $message,);
// Later, if the user leaves the screen:Files::cancelDownload($this->download);Downloads go straight from the network to the sandbox without passing through PHP. The URL must be absolute HTTPS without credentials; the limit is 64 MiB by default and 256 MiB at most.
Importing the user’s files
Section titled “Importing the user’s files”Files::pick() and Files::pickMany() open the file dialog and copy what the user chose into the sandbox, off the UI thread:
use Pam\Desktop\MediaPickerType;
Files::pick(MediaPickerType::Image, function (?FileReference $file): void { if ($file !== null) { $this->avatar = $file->uri(); }}, failure: fn (string $message) => $this->error = $message);MediaPickerType is Image, Video, Audio, Media or Any. A cancelled dialog reports null (or an empty list). To work with the user’s file in place instead of a copy, use Dialog.
Sharing
Section titled “Sharing”Share::text() and Share::files() open the system share sheet on macOS and Windows (neither run on a real machine yet). Linux has no share sheet, so the host opens a menu with what the desktop can do: open a link, copy, send by e-mail, open with another application, or show in the file manager.
Choosing the right store
Section titled “Choosing the right store”| Data | Store |
|---|---|
| Preferences, small UI state | Storage |
| Tokens, passwords, keys | SecureStorage |
| Documents, downloads, imported media | Files |
| Records you query, sort or paginate | SQLite |
| Anything you can download again | The cache directory; images and media cache themselves |
Next steps
Section titled “Next steps”- SQLite for structured data and migrations.
- HTTP & WebSockets for the network.