LensStudio:ScopedStorage.js.ScopedStorage
ClassProvides safe file system operations within a specified directory.
Provides safe file system operations within a specified directory. All operations are scoped to the storage directory to prevent unauthorized file access.
Index
Properties
Methods
Examples
// Create storage in a temporary directory
const tempStorage = new ScopedStorage();
// Create storage in a specific directory
const storage = new ScopedStorage('/path/to/directory');
// Write and read data
const storage = new ScopedStorage();
const configPath = storage.createFile('config.json', JSON.stringify({
version: '1.0',
enabled: true
}));
const configData = storage.readFile(configPath);
const config = JSON.parse(configData);
Implements
Constructors
Constructor
new ScopedStorage(
path?):ScopedStorage
Creates a new ScopedStorage instance.
Parameters
| Parameter | Type | Description |
|---|---|---|
path? | string | Path | Optional base directory path for storage operations. If not provided, a temporary directory is created. |
Returns
ScopedStorage
Example
const tempStorage = new ScopedStorage();
const customStorage = new ScopedStorage('/path/to/directory');
Properties
directory
directory:
Directory
Accessors
path
Get Signature
get path():
Path
Gets the path of the storage directory
Returns
Gets the path of the storage directory
Implementation of
Methods
createFile()
createFile(
fileName,content):Path
Creates a new file in the storage directory.
Parameters
| Parameter | Type | Description |
|---|---|---|
fileName | string | The name of the file to create |
content | string | Uint8Array<ArrayBufferLike> | The file content as a string or byte array |
Returns
The full path to the created file
Throws
If the resolved path is outside the storage directory
Example
const filePath = storage.createFile('config.json', '{"key": "value"}');
const imagePath = storage.createFile('image.png', imageBytes);
Implementation of
readBytes()
readBytes(
path):Uint8Array<ArrayBufferLike>
Reads a file as a byte array.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | Path | Path to the file (relative to storage directory or absolute) |
Returns
Uint8Array<ArrayBufferLike>
File content as Uint8Array, or null if the file is outside the storage directory
Example
const bytes = storage.readBytes(storage.path.appended(new Editor.Path('image.png')));
Implementation of
readFile()
readFile(
path):string
Reads a file as a string.
Parameters
| Parameter | Type | Description |
|---|---|---|
path | string | Path | Path to the file (relative to storage directory or absolute) |
Returns
string
File content as string, or null if the file is outside the storage directory
Example
const content = storage.readFile(storage.path.appended(new Editor.Path('config.json')));
const config = JSON.parse(content);
Implementation of
unpackContent()
unpackContent(
archivePath):Path
Unpacks a ZIP archive into the storage directory.
Parameters
| Parameter | Type | Description |
|---|---|---|
archivePath | string | Path | Path to the ZIP archive |
Returns
Path to the unpacked directory
Example
const unpackedPath = storage.unpackContent(storage.path.appended(new Editor.Path('archive.zip')));