Filesystem: Mobile Filesystem Access
The fs
module provides many interfaces for applications to access mobile disk files.
Developers can determine whether these interfaces work in the EdgerOS mobile App environment through the following code:
edger.env().then(data => {
if (data.env === 'edgerapp') {
// We work in EdgerOS mobile App environment.
}
}).catch(error => {
console.error(error);
});
Functions
edger.fs.open(id)
id
{String} File ID.- Returns: {Promise} Promise object.
The returned file
object can contain the following member:
success
{String} Result status.
Example
const fileId = 'fileId';
edger.filesystem.open(fileId).then((data) => {
const { success } = data;
console.log('Open information:', success);
}).catch(error => {
console.error(error);
});
async / await
async function fileSystemOpen() {
try {
await edger.filesystem.open(fileId);
const { success } = data;
console.log('Open information:', success);
} catch (error) {
console.error(error);
}
}
edger.fs.showFileSelector(options)
Open a file selector to select either a single file or multiple files. After successful selection, retrieve detailed information about the selected file.
options
{Object} File options.- Returns: {Promise} Promise array, each item of the array is the information of the selected file.
options
is an object, it can contain the following members:
multiple
{Boolean} Enable file multiple selection.channelType
{'system' | 'edger'} The channel type of file selector. Default:system
. It only takes effect in Android. Optional.accepts
: {Array<String>} The file types to be accepted. Default['*']
. Only effective whenchannelType
isedger
and on Android. Optional.confirmText
: {String} Confirm button text. Only effective whenchannelType
isedger
and on Android. Optional.cancelText
: {String} Cancel button text. Only effective whenchannelType
isedger
and on Android. Optional.maxFileNum
: {Number} The maximum number of files can be selected. Only effective whenchannelType
isedger
and on Android. Optional.
accepts
is an array, it can contain the following values:
*
: All files.video/*
: All video files.image/*
: All image files.audio/*
: All audio files.pdf/*
: All PDF files.doc/*
: All DOC files.ppt/*
: All PPT files.xls/*
: All XLS files.txt/*
: All Text files.other/*
: Files that are not of the above types.
Get an array of file objects, each file object includes:
id
{String} File ID.name
{String} The name of file.path
{String} The path of file.size
{Number} The size of file.type
{String} The type of file (optional values: 'video' | 'livephoto' | 'livephoto#mov' | 'picture' | 'file' | 'directory').mtime
{Number} The modification timestamp of file (millisecond from 1970-01-01 00:00:00 UTC).birthtime
{Number} The creation timestamp of file (millisecond from 1970-01-01 00:00:00 UTC).url
{String} The url of file.
Example
const options = {
multiple: true,
channelType: 'edger',
accepts: ['video/*']
};
edger.fs.showFileSelector(options).then((payload) => {
payload.forEach((file) => {
const { id, name, path, size, type, mtime, birthtime } = file;
console.log("File info:", id, name, path, size, type, mtime, birthtime);
});
}).catch((error) => {
console.error(error);
});
async / await
async function showFileSelector() {
try {
const payload = await edger.fs.showFileSelector(options);
payload.forEach((file) => {
const { id, name, path, size, type, mtime, birthtime } = file;
console.log("File info:", id, name, path, size, type, mtime, birthtime);
});
} catch (error) {
console.error(error);
}
}
edger.fs.showMediaSelector(options)
Open a media file selector. After successful selection, retrieve detailed information about the selected file.
options
{Object} Media File options.accept
{String} Media File acceptance type. Optional values:image/*
|video/*
|*
.multiple
{Boolean} Enable media file multiple selection.
- Returns: {Promise} Promise array, each item of the array is the information of the selected media file.
Get an array of media file objects, each media file object includes:
id
{String} Media file ID.name
{String} The name of media file.size
{Number} The size of media file.type
{String} The type of media file (optional values: 'video' | 'livephoto' | 'livephoto#mov' | 'picture' | 'file' | 'directory').mtime
{Number} The modification timestamp of media file (millisecond from 1970-01-01 00:00:00 UTC).birthtime
{Number} The creation timestamp of media file (millisecond from 1970-01-01 00:00:00 UTC).media
{Object} Media file information object.
media
is media file information object, it can contain the following members:
url
{String} The url of media file.mov
{Object} The live picture object of media file, only when the iOS system and the file is a live picture. Optional.thumbnail
{Object} The thumbnail object of media file.detail
{Object} The details object of media file.
mov
is live picture object of media file, only when the iOS system and the file is a live picture, it can contain the following members:
type
{String} The type of live picture.size
{Number} The size of live picture.path
{String} The path of live picture.url
{String} The url of live picture.
thumbnail
is thumbnail object of media file, it can contain the following members:
type
{String} The type of thumbnail.size
{Number} The size of thumbnail. Optional.path
{String} The path of thumbnail.url
{String} The url of thumbnail.
detail
is details of media file, it can contain the following members:
latitude
{Number} The latitude of the media file location information. Optional.longitude
{Number} The longitude of the media file location information. Optional.altitude
{Number} The altitude of the media file location information. Optional.width
{Number} The width of media file.height
{Number} The height of media file.duration
{Number} The duration of video type media files. Optional.
Example
const options = {
accept: 'image/*',
multiple: true
};
edger.fs.showMediaSelector(options).then((payload) => {
payload.forEach((file) => {
const { name, birthtime, media } = file;
console.log("File info:", name, birthtime, media, media.url);
});
}).catch((error) => {
console.error(error);
});
async / await
async function showMediaSelector() {
try {
const payload = await edger.fs.showMediaSelector(options);
payload.forEach((file) => {
const { name, birthtime, media } = file;
console.log("File info:", name, birthtime, media, media.url);
});
} catch (error) {
console.error(error);
}
}
edger.fs.releaseMediaSelector()
Release the media file selector.
- Returns: {Promise}.
Example
edger.fs.releaseMediaSelector().then(() => {
}).catch((error) => {
console.error(error);
});
async / await
async function releaseMediaSelector() {
try {
await edger.fs.releaseMediaSelector();
} catch (error) {
console.error(error);
}
}
edger.fs.dumpDir(path)
Query the directory of files under a specific path.
path
{String} File dump directory path.- Returns: {Promise} Promise object.
Get an object of result, the object includes:
data
{Object} The result of the file to be dumped.
The data
is an array of file, each item of the array is the information of the dump file, the file dump object includes:
id
{String} File ID.name
{String} The name of file.path
{String} The path of file.size
{Number} The size of file.type
{String} The type of file (optional values: 'video' | 'livephoto' | 'livephoto#mov' | 'picture' | 'file' | 'directory').mtime
{Number} The modification timestamp of file (millisecond from 1970-01-01 00:00:00 UTC).birthtime
{Number} The creation timestamp of file (millisecond from 1970-01-01 00:00:00 UTC).
Example
const path = "/system";
edger.fs.dumpDir(path).then((payload) => {
payload.data.forEach((file) => {
const { name, path, size, type } = file;
console.log("File info:", name, path, size, type);
});
}).catch((error) => {
console.error(error);
});
async / await
async function dumpDir() {
try {
const payload = await edger.fs.dumpDir(options);
payload.data.forEach((file) => {
const { name, path, size, type } = file;
console.log("File info:", name, path, size, type);
});
} catch (error) {
console.error(error);
}
}
edger.fs.readAsBase64(path)
Convert files under a specific path to base64-encoded strings.
path
{String} File path.- Returns: {Promise} Promise object.
Get an object of result, the object includes:
data
{String} The result of converting the file to base64.
Example
const path = "/path";
edger.fs.readAsBase64(path).then((payload) => {
const base64 = payload.data;
console.log("File base64 string:", base64);
}).catch((error) => {
console.error(error);
});
async / await
async function readAsBase64() {
try {
const payload = await edger.fs.readAsBase64(path);
const base64 = payload.data;
console.log("File base64 string:", base64);
} catch (error) {
console.error(error);
}
}
edger.fs.stat(path)
Retrieve detailed information about files under a specific path.
path
{String} File path.- Returns: {Promise} Promise object.
Get an object of file information, the object includes:
id
{String} File ID.name
{String} The name of file.path
{String} The path of file.size
{Number} The size of file.type
{String} The type of file (optional values: 'video' | 'livephoto' | 'livephoto#mov' | 'picture' | 'file' | 'directory').mtime
{Number} The modification timestamp of file (millisecond from 1970-01-01 00:00:00 UTC).birthtime
{Number} The creation timestamp of file (millisecond from 1970-01-01 00:00:00 UTC).
Example
const path = "/system";
edger.fs.stat(path).then((payload) => {
const { id, name, path, size, type, mtime, birthtime } = payload;
console.log("File info:", id, name, path, size, type, mtime, birthtime);
}).catch((error) => {
console.error(error);
});
async / await
async function stat() {
try {
const payload = await edger.fs.stat(path);
const { id, name, path, size, type, mtime, birthtime } = payload;
console.log("File info:", id, name, path, size, type, mtime, birthtime);
} catch (error) {
console.error(error);
}
}
edger.fs.save(params)
Move files and save them to a specified directory.
params
{Object} Media player parameters.- Returns: {Promise} Fulfill with undefined upon success.
The params
is an object, it can contain the following members:
path
{String} Source path to save.destPath
{String} Destination path to save to.fileName
{String} The name of the saved file. Optional.
Example
const params = {
path: '/system',
destPath: '/destPath'
};
edger.fs.save(params).then(() => {
console.log("File saved successfully!");
}).catch((error) => {
console.error(error);
});
async / await
async function save() {
try {
await edger.fs.save(params);
console.log("File saved successfully!");
} catch (error) {
console.error(error);
}
}