File System
File System API allows low level manipulation with files and directories in the device internal storage. File System API works with both in internal and external storages (USB flash drives, etc.).
File System directory structure is PERSISTENT and is NOT automatically deleted through Applet Reload
power action! Applet Reload only deletes /data
directory which is reserved for simplified Offline Cache API. Use deleteFile()
to clear the device storage file system.
All methods
Methods | Description | Supported since |
---|---|---|
listStorageUnits() | Return list of storage units /internal, external/ from the device | 2.1.0 |
listFiles() | Return list of files of existing directory | 2.1.0 |
getFile() | Returns local uri of existing file | 2.1.0 |
exists() | Returns true of existing directory or file | 2.1.0 |
downloadFile() | Download file to path within existing directory or Overrides existing file in path when successfully downloaded | 2.1.0 |
deleteFile() | Delete file from path within existing directory | 2.1.0 |
moveFile() | Move file to destination path or Move directory to destination path | 2.1.0 |
getFileChecksum() | Returns checksum of existing file in path | 2.1.0 |
extractFile() | Extract ZIPed all files (recursively) to destination path and override existing files or merge existing directories | 2.1.0 |
createArchive() | Creates archive in selected path | 5.12.0 |
createDirectory() | Creates directory in path | 2.1.0 |
isDirectory() | Returns true if existing path is directory | 2.1.0 |
copyFile() | Copy file or directory to a new location | 3.3.0 |
writeFile() | Write string to a file | 3.2.0 |
appendFile() | Write string to a file | 4.13.0 |
readFile() | Read a text file | 3.3.0 |
listStorageUnits()
Return list of storage units /internal, external/ from the device. Capacity values are in bytes.
This is a mandatory method that is required for all the other File System APIs. The other APIs require a storageUnit
object that is retrieved from this method to manipulate with files on a correct storage location (internal/external).
storageUnit
is a dynamic object! It has to be always generated and retrieved by this JS API, as the values in type
differ platform by platform. Never generate the object manually. {"type":"internal"}
is for demonstration only.
Javascript Example
// Storage units are equivalent to disk volumes (C:, D: etc on Windows; /mnt/disc1, /mnt/disc2 on Unix)
const storageUnits = await sos.fileSystem.listStorageUnits();
// Every platform has at least one not removable storage unit (internal storage unit)
const internalStorageUnit = storageUnits.find((storageUnit) => !storageUnit.removable);
console.log(storageUnits);
Returns
[
{
"type":"internal",
"capacity":4124856000, // in bytes
"freeSpace":3764700000, // in bytes
"usableSpace":3764700000, // in bytes
"removable":false
},
{
"type":"external",
"capacity":2224856000, // in bytes
"freeSpace":764700000, // in bytes
"usableSpace":764700000, // in bytes
"removable":true
}
]
listFiles()
List files and folders located in the internal/external storage.
- SUCCESS: Return list of files of existing directory
- FAIL: When listing not existing directory
- FAIL: When listing existing file (not a directory)
Javascript Example
// Storage units are equivalent to disk volumes (C:, D: etc on Windows; /mnt/disc1, /mnt/disc2 on Unix)
const storageUnits = await sos.fileSystem.listStorageUnits();
// Every platform has at least one not removable storage unit (internal storage unit)
const internalStorageUnit = storageUnits.find((storageUnit) => !storageUnit.removable);
// Empty string '' refers to root directory. Here is root directory of internal storage
const rootDirectoryFilePaths = await sos.fileSystem.listFiles({ filePath: '', storageUnit: internalStorageUnit });
console.log(rootDirectoryFilePaths);
// get files from directory 'test-dir'
const testDirDirectoryFilePaths = await sos.fileSystem.listFiles({ filePath: 'test-dir', storageUnit: internalStorageUnit });
console.log(testDirDirectoryFilePaths);
Returns
// there are 2 directories in the internal storage
[
{
"storageUnit":{
"type":"internal",
"capacity":4124856000,
"freeSpace":3336336000,
"usableSpace":3336336000,
"removable":false
},
"filePath":"data"
},
{
"storageUnit":{
"type":"internal",
"capacity":4124856000,
"freeSpace":3336336000,
"usableSpace":3336336000,
"removable":false
},
"filePath":"test-dir"
}
]
// there are 3 files in the 'test-dir' directory
[
{
"storageUnit":{
"type":"internal",
"capacity":4124856000,
"freeSpace":3764696000,
"usableSpace":3764696000,
"removable":false
},
"filePath":"test-dir/downloaded-file.png"
},
{
"storageUnit":{
"type":"internal",
"capacity":4124856000,
"freeSpace":3764696000,
"usableSpace":3764696000,
"removable":false
},
"filePath":"test-dir/log.1.txt"
},
{
"storageUnit":{
"type":"internal",
"capacity":4124856000,
"freeSpace":3764696000,
"usableSpace":3764696000,
"removable":false
},
"filePath":"test-dir/log.1.txt.backup"
}
]
getFile()
Return a file from an internal storage
- OK: Returns local uri of existing file
- OK: Returns NULL of not existing file
Parameters
Param | Type | Required | Description |
---|---|---|---|
filePath | FilePath | Yes | Object contains storageUnit object returned by listStorageUnits() and filePath if the file or directory |
Javascript Example
// always pass storageUnit to tell the JS API where to look
// get properties of file (at least localUri) or null when not exist
const fileProperties = await sos.fileSystem.getFile({ storageUnit: internalStorageUnit, filePath: 'test-dir/downloaded-file.png' });
// Complete file details
console.log(fileProperties);
// To access file from HTML applet, use localUri
console.log(fileProperties.localUri);
Returns
Return statement is a dynamic object! It has to be always generated and retrieved by this JS API, as the values in localUri
differ platform by platform. Never generate the object manually. {"localUri":"file://internal/test-dir/downloaded-file.png"}
is for demonstration only.
{
"localUri": "file://internal/test-dir/downloaded-file.png",
// Other optional properties when supported
"createdAt": "2019-05-03T12:00:00.000Z",
"lastModifiedAt": "2019-05-03T12:00:00.000Z",
"sizeBytes": 2048,
"mimeType": "image/png"
}
exists()
- OK: Returns true of existing directory or file
- OK: Returns false of not existing directory nor file
Parameters
Param | Type | Required | Description |
---|---|---|---|
filePath | FilePath | Yes | Object contains storageUnit object returned by listStorageUnits() and filePath if the file or directory |
Javascript Example
// always pass storageUnit to tell the JS API where to look
// check if there is such a file
const fileExists = await sos.fileSystem.exists({ storageUnit: internalStorageUnit, filePath: 'test-dir/downloaded-file.png' });
console.log(fileExists);
Returns
Boolean value