Player APIs (BrightScript & Ja...
JavaScript APIs
filesysteminfile
9min
the filesysteminfile allows you to create and update filesystems for use as storage devices filesysteminfile idl interface filesysteminfile { constructor(string path); promise\<string> getfilename(); promise\<long long> getsize(); promise\<void> format(string filesystem); promise\<void> mount(); promise\<void> unmount(); }; object creation to create a filesysteminfile object, load the @brightsign/filesysteminfile module using the node js® require() method var filesysteminfile = require('@brightsign/filesysteminfile'); var fsif = new filesysteminfile("/storage/sd/usbstore img"); // node's native 'fs' can create an img file filesysteminfile use this interface to configure a filesysteminfile getfilename() promise\<string> getfilename() gets the name of the file that was used to create filesysteminfile getsize() promise\<long long> getsize() retrieves the size (in bytes) of the filesystem file used to create the filesysteminfile class format() promise\<void> format(string filesystem) creates the specified filesystem in a file that already exists the supported filesystem types are "fat" and "exfat" mount() promise\<string> mount() mounts the filesystem there is no need to format the filesystem if it already exists and is formatted the return string is filepath and the return value is the path to where the filesystem has been mounted it cannot be mounted more than once unmount() promise\<void> unmount() unmounts the filesystem example the example below creates a file system, formats, mounts, and unmounts it using the npm package fs , const fs = require('fs'); const filesysteminfile = require('@brightsign/filesysteminfile'); var fsif; var fd; open() then(function(filedescriptor) { 	fd = filedescriptor; 	return write(fd); }) then(function(bytes) { 	console log(`${bytes} written to new file`); 	return close(fd); }) then(function() { 	// create filesysteminfile using the backing file 	fsif = new filesysteminfile('/storage/sd/usbstore'); 	return format(); }) then(function() { 	return mount(); }) then(function() { 	/ this is where you can actually do stuff with the mounted filesystem / 	// unmount the mounted file after 10 seconds 	settimeout(function() { 	 return unmount(); 	}, 10000); }) catch(function(error) { 	console log(json stringify(error)); }); // create a writable file for the file system function open() { 	return new promise(function(resolve, reject) { 	 fs open('/storage/sd/usbstore', 'w', function(error, fd) { 	 if (error) reject(error); 	 resolve(fd); 	 });	 	}); }; // write a buffer allocating 1gb of disk space for the file function write(fd) { 	return new promise(function(resolve, reject) { 	 fs write(fd, buffer alloc(1), 0, 1, (1024 1024 1024) 1, function(error, byteswritten) { 	 if (error) reject(error); 	 resolve(byteswritten); 	 }); 	}); }; // close the created file function close(fd) { 	return new promise(function(resolve, reject) { 	 fs close(fd, function(error) { 	 if (error) reject(error); 	 resolve(); 	 }); 	}); }; // format the file system function format() { 	return new promise(function(resolve, reject) { 	 fsif format("exfat") 	 then(function() { 	 console log('filesystem formatted'); 	 resolve(); 	 }) 	 catch(function(error) { 	 reject(error); 	 }); 	 	}); }; // mount the filesystem internally function mount() { 	return new promise(function(resolve, reject) { 	 fsif mount() 	 then(function(mount point) { 	 console log('filesystem mounted' + mount point); 	 resolve(); 	 }) 	 catch(function(error) { 	 reject(error); 	 }); 	}); }; // unmount filesystem from internal mount point only can unmount a mounted file function unmount() { 	return new promise(function(resolve, reject) { 	 fsif unmount() 	 then(function() { 	 console log('filesystem unmounted'); 	 resolve(); 	 }) 	 catch(function(error) { 	 reject(error); 	 }); 	}); }; you can also reference our public github example https //github com/brightsign/usb filesystem which uses a brightsign device as a filesystem over usb