Skip to main content

LensStudio:ScopedStorage.js.ScopedStorage

Class

Provides 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

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

ParameterTypeDescription
path?string | PathOptional 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

Path

Gets the path of the storage directory

Implementation of

IStorage.path

Methods

createFile()

createFile(fileName, content): Path

Creates a new file in the storage directory.

Parameters

ParameterTypeDescription
fileNamestringThe name of the file to create
contentstring | Uint8Array<ArrayBufferLike>The file content as a string or byte array

Returns

Path

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

IStorage.createFile


readBytes()

readBytes(path): Uint8Array<ArrayBufferLike>

Reads a file as a byte array.

Parameters

ParameterTypeDescription
pathstring | PathPath 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

IStorage.readBytes


readFile()

readFile(path): string

Reads a file as a string.

Parameters

ParameterTypeDescription
pathstring | PathPath 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

IStorage.readFile


unpackContent()

unpackContent(archivePath): Path

Unpacks a ZIP archive into the storage directory.

Parameters

ParameterTypeDescription
archivePathstring | PathPath to the ZIP archive

Returns

Path

Path to the unpacked directory

Example

const unpackedPath = storage.unpackContent(storage.path.appended(new Editor.Path('archive.zip')));

Implementation of

IStorage.unpackContent