Below is the source-code for a sample file-system extension, which create a virtual file-system.
function getFileInfos(path, pattern, session, node) {
var fileInfos = [];
getDB(session, node).readTransaction(function(transaction) {
var res = transaction.executeSql("SELECT name, length, modifiedTime, createdTime FROM Files");
for (var i=0; i < res.rows.length; i ++) {
var row = res.rows[i];
fileInfos.push({
name: row.name,
isFolder: false,
length: row.length,
modifiedTime: row.modifiedTime,
createdTime: row.createdTime
});
}
});
return fileInfos;
}
function onWriteBegin() {
return { type: 'binary' };
}
function write(path, data, length, session, node) {
getDB(session, node).transaction(function(tx) {
tx.executeSql("INSERT INTO Files(name, data, length, modifiedTime, createdTime) VALUES (?, ?, ?, ?, ?)",
[path, data, length, new Date(), new Date()]);
});
}
function read(path, session, node) {
var data = null;
getDB(session, node).readTransaction(function(transaction) {
var res = transaction.executeSql("SELECT * FROM Files WHERE name=?", [path]);
if (res.rows.length > 0)
data = res.rows[0].data;
});
return { binary: data };
}
function deleteFile(path, session, node) {
getDB(session, node).transaction(function(tx) {
tx.executeSql("DELETE FROM Files WHERE name=?", [path]);
});
}
function moveFile(fromPath, toPath, session, fromNode, toNode) {
var success = false;
getDB(fromNode).transaction(function(tx) {
var res = tx.executeSql("UPDATE Files SET name=? WHERE name=?", [toPath, fromPath]);
success = res.rowsAffected > 0;
});
return success;
}
var databases = {};
function getDB(session, node) {
var db = databases[node.path];
if (!db) {
var dbFolder = system.getFile(session.fullHomePath + "/fileDBs");
if (!dbFolder.exists())
dbFolder.createFolder();
var folderName = node.path.substring(node.path.lastIndexOf('/') + 1);
db = databases[node.path] = system.openDatabaseSync(dbFolder.fullPath + "/" + folderName + ".sdf");
}
// create the Files table if it doesn't already exist
if (!tableExists(db, "Files"))
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE [Files] ([name] NVARCHAR(1024), [data] IMAGE, [length] INT, [modifiedTime] DATETIME, [createdTime] DATETIME)");
});
return db;
}
function tableExists(db, tableName) {
var rowCount = 0;
db.readTransaction(function(tx) {
var res = tx.executeSql("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ?", [tableName]);
rowCount = res.rows.length;
});
return rowCount > 0;
}
Read file information from database.
function getFileInfos(path, pattern, session, node) {
var fileInfos = [];
getDB(session, node).readTransaction(function(transaction) {
var res = transaction.executeSql("SELECT name, length, modifiedTime, createdTime FROM Files");
for (var i=0; i < res.rows.length; i++) {
var row = res.rows[i];
fileInfos.push({
name: row.name,
isFoldesr: false,
length: row.length,
modifiedTime: row.modifiedTime,
createdTime: row.createdTime
});
}
});
return fileInfos;
}
Write file to database database.
function write(path, data, length, session, node) {
getDB(session, node).transaction(function(tx) {
tx.executeSql("INSERT INTO Files(name, data, length, modifiedTime, createdTime) VALUES (?, ?, ?, ?, ?)",
[path, data, length, new Date(), new Date()]);
});
}
Read file from database and return data as binary.
function read(path, session, node) {
var data = null;
getDB(session, node).readTransaction(function(transaction) {
var res = transaction.executeSql("SELECT * FROM Files WHERE name=?", [path]);
if (res.rows.length > 0)
data = res.rows[0].data;
});
return { binary: data };
}
Delete file out of database
function deleteFile(path, session, node) {
getDB(session, node).transaction(function(tx) {
tx.executeSql("DELETE FROM Files WHERE name=?", [path]);
});
}
Move file
function moveFile(fromPath, toPath, session, fromNode, toNode) {
var success = false;
getDB(fromNode).transaction(function(tx) {
var res = tx.executeSql("UPDATE Files SET name=? WHERE name=?", [toPath, fromPath]);
success = res.rowsAffected > 0;
});
return success;
}
Returns database object for the given node.
function getDB(session, node) {
var db = databases[node.path];
if (!db) {
var dbFolder = system.getFile(session.fullHomePath + "/fileDBs");
if (!dbFolder.exists())
dbFolder.createFolder();
var folderName = node.path.substring(node.path.lastIndexOf('/') + 1);
db = databases[node.path] = system.openDatabaseSync(dbFolder.fullPath + "/" + folderName + ".sdf");
}
// create the Files table if it doesn't already exist
if (!tableExists(db, "Files"))
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE [Files] ([name] NVARCHAR(1024), [data] IMAGE, [length] INT, [modifiedTime] DATETIME, [createdTime] DATETIME)");
});
return db;
}
Returns true if a table with the given name exists in the database.
function tableExists(db, tableName) {
var rowCount = 0;
db.readTransaction(function(tx) {
var res = tx.executeSql("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ?", [tableName]);
rowCount = res.rows.length;
});
return rowCount > 0;
}
Run the read transaction contained in the given callback function. A SQLTransactionSync object is passed as an argument to the callback. This may be used execute queries. The transaction is committed if the callback returns normally and rolled back if it throws an exception.
[callback]
Function
optional
callback function
[transaction]
SQLTransactionSync
optional
Transaction object which can be used to execute SQL queries.
Run the non-read transaction contained in the given callback function. A SQLTransactionSync object is passed as an argument to the callback. This may be used execute queries. The transaction is committed if the callback returns normally and rolled back if it throws an exception.
[callback]
Function
optional
callback function
[transaction]
SQLTransactionSync
optional
Transaction object which can be used to execute SQL queries.
Execute the given SQL and return the result as an SQLResultSet object.
sqlStatement
String
SQL statement.
arguments
Array
Array containing the arguments to substitute into the statement.
Returns a File object for the given path.
The File object may or may not represent an existing file or directory.
path
String
Path of file