DOCS

File System

The object wa.fs provides wrappers to perform basic file system operations.

File Information

Use ws.fs.file(absolutePath, callback) to get information on a particular file or directory.

var path = "/Users/ctk/Desktop/test.txt";
wa.fs.file(path, function(o){
	if(!o.file){
		console.log("Unable to get file info...\n");
		return;
	}
	console.log("file=%o", o.file)
});

Upon success the response object in the callback has a file object with relevant information. The file object looks like this:

{
	exists: true,
	name: "test.txt",
	parent: "/Users/ctk/Desktop",
	path: "/Users/ctk/Desktop/test.txt",
	urlpath: "file:///Users/ctk/Desktop/test.txt",
	permissions: 420,
	readable: true,
	writable: true,
	size: 10,
	type: "file", //"file" or "fldr"
}

When a path points to a folder the file object will have an items key with an array of file objects representing the childrens of the directory.

Execute

Use ws.fs.execute(cmd, callback) to execute a shell command or a execute a binary in synchronous mode.

var cmd = "env";
wa.fs.execute(cmd, function(o){
	console.log("execute done\n");
	if(o.errorcode == 0 ) console.log("completed ok\n");
	console.log("out=" + o.output)
});

The callback will recieve a response object with an errorcode(number) an the output(string) with the results of the command.

Write to a file

The function wa.fs.putContents(path, callback) write data to a file. If the file does not exists it will be created.

var path = "/Users/ctk/Desktop/test.txt"
var data = "Hello Jose\nThis came from javascript...";
wa.fs.putContents(path, data, function(o){
	if( o.status == 200 ){
		console.log("write completed...")
		console.log("file=%o", o.file)
	}else if(o.status == 505){
		console.log("Unable to write")
		return;
	}
});

On success the response object also returns a file object under the file key.

Open dialog

var f = "/Users/ctk/Programming/Projects/";
wa.fs.dialogOpenFile( { path: f, title: "Open", prompt: "Select the configuration file:" }, function(o){
	if( o.status != 200 ) return;
	wa.fs.getContents( o.file.path , function(o){
		if( o.status != 200 ) return;
		console.log( "--------------\n" + o.data );
	});
});

Save dialog

var initialPath = "/Users/ctk/Programming/Projects/";

wa.fs.dialogSaveFile( { name : "jose.txt", title: "Where to save", prompt: "Enter a FILENAME:" , path: initialPath}, function(o){
  if( o.status != 200 ) return;
  wa.fs.putContents( o.file.path, "hello world" );
});

Select a folder

The function wa.fs.dialogSelectDirectory(callback) display a dialog to allow the user to select a folder.

wa.fs.dialogSelectDirectory(function(o){
	if( o.status != 200 ) return;
	console.log("Folder selected %o", o.file);
});

Create a folder

Use wa.fs.createDirectory(path, callback) to create a folder.

	var path = "/Users/ctk/Desktop/test_dir"
	wa.fs.createDirectory(path, function(o){
		if( o.status == 201 ){
			console.log("directory already exists");
			console.log("Directory %o", o.file);
			return;
		}
		if( o.status != 200 ) return;
		console.log("Directory created %o", o.file);
	});

Paths

The object ws.fs.paths has a collection of usefull paths to key filesystem locations.

	console.log("Im at: " + wa.fs.paths.base );
	console.log("Home folder: " + wa.fs.paths.user );
	console.log("Documents folder: " + wa.fs.paths.documents );
var path = "/Users/ctk/Desktop/test.txt";
wa.fs.getContents(path, function(o){
	if( o.status == 200 ){
		console.log("File Contents\n" + o.data );
	}else if(o.status == 501){
		console.log("Path not found")
		return;
	}else if(o.status == 502){
		console.log("Path not readable")
		return;
	}
});

On success the response object also returns a file object under the file key.

Examples

Example writting base64 data. In this case convert a canvas to a PNG and save it.

function drawSomething() {
	var canvas = document.getElementById("acanvas");
	if(!canvas.getContext) return;
	var ctx = canvas.getContext("2d");

	ctx.fillStyle = "rgb(200,0,0)";
	ctx.fillRect (10, 10, 50, 50);

	ctx.fillStyle = "rgba(0, 0, 200, 0.8)";
	ctx.fillRect (30, 30, 50, 50);

}

function saveCanvas(){

	drawSomething(); //draw something...

	var data = canvas.toDataURL("image/png");

	//remove dataurl stuff, leave only base64 data
	data = data.replace(/^data:image\/png\;base64\,/, '');

	var path = wa.fs.paths.documents + "/test.png"
	console.log("save to:" + path);
	var ops = {
		path: path,
		data: data,
		base64: true
	};
	
	wa.fs.putContents(ops, function(o){
		if( o.status != 200 ) return;
		console.log( "write completed..." );
	});
}


Fork me on GitHub