FileSystem

AgentDriveManager~ FileSystem

The FileSystem object helps to organise the contents of the associated virtual drive. It provides the methods to manipulate folders and files as well as obtain the file data in various formats. This object cannot be created, it can only be provided by the Drive object methods.

Methods

appendFile(filePath, data) → {Promise.<module:DriveManager~FileSystemEntry>}

Append the data to the end of the existing file

Parameters:
Name Type Description
filePath String

The file path relative to the virtual drive

data Uint8Array | ArrayBuffer | Blob | String

The file contents, can be one of the following: Uint8Array, ArrayBuffer, Blob or Base64 String

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const fileContents1 = new TextEncoder('utf8').encode("part1 ").buffer;
     const fileContents2 = new TextEncoder('utf8').encode("+ part2").buffer;
     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});

     await drive1.fileSystem.createFile('file.txt', fileContents1, 'text/plain');
     const result1 = await drive1.fileSystem.readFileAsString('file.txt');

     console.log(result1);

     //
     //    Will Print
     //
     //    "part1 "
     //

     await drive1.fileSystem.appendFile('file.txt', fileContents2);
     const result2 = await drive1.fileSystem.readFileAsString('file.txt');

     console.log(result2);

     //
     //    Will Print
     //
     //    "part1 + part2"
     //
};

init();

createDirectory(directoryPath) → {Promise.<module:DriveManager~FileSystemEntry>}

Create the new directory(s) from path

Parameters:
Name Type Description
directoryPath String

The directory path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});
     await drive1.fileSystem.createDirectory('/folder1/folder2');

     const result = await drive1.fileSystem.getDirectory('/');

     console.log(result);

     //
     //    Will Print
     //    {
     //         "isFile": false,
     //         "isDirectory": true,
     //         "name": "ed6a4862-e6a9-4f74-a9d2-04e24d42ad5a",
     //         "path": "drive://drive1/",
     //         "list": [
     //             {
     //                 "isFile": false,
     //                 "isDirectory": true,
     //                 "name": "folder1",
     //                 "path": "drive://drive1/folder1",
     //                 "list": [
     //                     {
     //                         "isFile": false,
     //                         "isDirectory": true,
     //                         "name": "folder2",
     //                         "path": "drive://drive1/folder1/folder2",
     //                         "list": []
     //                     }
     //                 ]
     //             }
     //         ]
     //    }
     //
};

init();

createFile(filePath, data, type) → {Promise.<module:DriveManager~FileSystemEntry>}

Create a new file or override the existing one

Parameters:
Name Type Default Description
filePath String

The file path relative to the virtual drive

data Uint8Array | ArrayBuffer | Blob | String

The file contents, can be one of the following: Uint8Array, ArrayBuffer, Blob or Base64 String

type String application/octet-stream

The file MIME type

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const fileContents = new TextEncoder('utf8').encode(JSON.stringify({test: 'file'})).buffer;
     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});
     const result = await drive1.fileSystem.createFile('/folder1/folder2/file.json', fileContents, 'application/json');

     console.log(result);

     //
     //    Will Print
     //
     //    {
     //        "url": "http://127.0.0.1:9090/filesystem/html5/virtual-drives/ed6a4862-e6a9-4f74-a9d2-04e24d42ad5a/folder1/folder2/file.json",
     //        "size": 15,
     //        "type": "application/json",
     //        "isFile": true,
     //        "isDirectory": false,
     //        "name": "file.json",
     //        "filePath": "drive://drive1/folder1/folder2/file.json",
     //    }
     //
};

init();

deleteDirectory(directoryPath)

Delete the directory and all of its contents

Parameters:
Name Type Description
directoryPath String

The directory path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});
     await drive1.fileSystem.createDirectory('/folder1');

     const result1 = await drive1.fileSystem.exists('/folder1');

     console.log(result1);

     //
     //    Will Print
     //
     //    true
     //

     await drive1.fileSystem.deleteDirectory('/folder1');

     const result2 = await drive1.fileSystem.exists('/folder1');

     console.log(result2);

     //
     //    Will Print
     //
     //    false
     //
};

init();

deleteFile(filePath)

Delete the file

Parameters:
Name Type Description
filePath String

The file path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const fileContents = new TextEncoder('utf8').encode(JSON.stringify({test: 'file'})).buffer;
     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});
     await drive1.fileSystem.createFile('file.json', fileContents, 'application/json');

     const result1 = await drive1.fileSystem.exists('file.json');

     console.log(result1);

     //
     //    Will Print
     //
     //    true
     //

     await drive1.fileSystem.deleteFile('file.json');

     const result2 = await drive1.fileSystem.exists('file.json');

     console.log(result2);

     //
     //    Will Print
     //
     //    false
     //
};

init();

exists(path)

Check if the directory or file exists

Parameters:
Name Type Description
path String

The directory or file path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});

     const result1 = await drive1.fileSystem.exists('/folder1');

     console.log(result1);

     //
     //    Will Print
     //
     //    false
     //

     const result2 = await drive1.fileSystem.exists('file.txt');

     console.log(result2);

     //
     //    Will Print
     //
     //    false
     //
};

init();

getDirectory(directoryPath) → {Promise.<module:DriveManager~FileSystemEntry>}

Get the directory metadata

Parameters:
Name Type Description
directoryPath String

The directory path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});
     await drive1.fileSystem.createDirectory('/folder1/folder2');

     const result = await drive1.fileSystem.getDirectory('/folder1/folder2');

     console.log(result);

     //
     //    Will Print
     //
     //    {
     //        "isFile": false,
     //        "isDirectory": true,
     //        "name": "folder2",
     //        "path": "drive://drive1/folder1/folder2",
     //        "list": []
     //    }
     //
};

init();

getFile(filePath) → {Promise.<module:DriveManager~FileSystemEntry>}

Get the file metadata

Parameters:
Name Type Description
filePath String

The file path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const fileContents = new TextEncoder('utf8').encode(JSON.stringify({test: 'file'})).buffer;
     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});
     await drive1.fileSystem.createFile('/folder1/folder2/file.json', fileContents, 'application/json');

     const result = await drive1.fileSystem.getFile('/folder1/folder2/file.json');

     console.log(result);

     //
     //    Will Print
     //
     //    {
     //        "url": "http://127.0.0.1:9090/filesystem/html5/virtual-drives/ed6a4862-e6a9-4f74-a9d2-04e24d42ad5a/folder1/folder2/file.json",
     //        "size": 15,
     //        "type": "application/json",
     //        "isFile": true,
     //        "isDirectory": false,
     //        "name": "file.json",
     //        "filePath": "drive://drive1/folder1/folder2/file.json",
     //    }
     //
};

init();

getFileMimeType(filePath) → {Promise.<String>}

Get the file MIME type

Parameters:
Name Type Description
filePath String

The file path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const fileContents = new TextEncoder('utf8').encode(JSON.stringify({test: 'file'})).buffer;
     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});
     await drive1.fileSystem.createFile('/folder1/folder2/file.json', fileContents, 'application/json');

     const result = await drive1.fileSystem.getFgetFileMimeTypeile('/folder1/folder2/file.json');

     console.log(result);

     //
     //    Will Print
     //
     //    "application/json"
     //
};

init();

readFileAsArrayBuffer(filePath) → {Promise.<ArrayBuffer>}

Read the file and return the file data as the ArrayBuffer object

Parameters:
Name Type Description
filePath String

The file path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const fileContents = new TextEncoder('utf8').encode(JSON.stringify({test: 'file'})).buffer;
     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});

     await drive1.fileSystem.createFile('file.json', fileContents, 'application/json');
     const result = await drive1.fileSystem.readFileAsArrayBuffer('file.json');

     console.log(result);

     //
     //    Will Print
     //
     //    ArrayBuffer(15) {}
     //
};

init();

readFileAsBase64(filePath) → {Promise.<ArrayBuffer>}

Read the file and return the file data as the base64 string

Parameters:
Name Type Description
filePath String

The file path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const fileContents = new TextEncoder('utf8').encode(JSON.stringify({test: 'file'})).buffer;
     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});

     await drive1.fileSystem.createFile('file.json', fileContents, 'application/json');
     const result = await drive1.fileSystem.readFileAsBase64('file.json');

     console.log(result);

     //
     //    Will Print
     //
     //    "data:application/json;base64,eyJ0ZXN0IjoiZmlsZSJ9"
     //
};

init();

readFileAsBinaryString(filePath) → {Promise.<ArrayBuffer>}

Read the file and return the file data as the binary string

Parameters:
Name Type Description
filePath String

The file path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const fileContents = new TextEncoder('utf8').encode(JSON.stringify({test: 'file'})).buffer;
     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});

     await drive1.fileSystem.createFile('file.json', fileContents, 'application/json');
     const result = await drive1.fileSystem.readFileAsBinaryString('file.json');

     console.log(result);

     //
     //    Will Print
     //
     //    {"test": "file"}
     //
};

init();

readFileAsString(filePath) → {Promise.<String>}

Read the file and return the file data as the plain text

Parameters:
Name Type Description
filePath String

The file path relative to the virtual drive

Example
import Agent from '@meldcx/agent';

const agent = new Agent();

const init = async() => {
     await agent.onReadyAsync();

     const fileContents = new TextEncoder('utf8').encode(JSON.stringify({test: 'file'})).buffer;
     const drive1 = await agent.DriveManager.getDrive('drive1', {createIfNotFound: true});

     await drive1.fileSystem.createFile('file.json', fileContents, 'application/json');
     const result = await drive1.fileSystem.readFileAsString('file.json');

     console.log(result);

     //
     //    Will Print
     //
     //    {"test": "file"}
     //
};

init();