filesysteminfile
9 min
el filesysteminfile le permite crear y actualizar sistemas de archivos para usarlos como dispositivos de almacenamiento idl de filesysteminfile interface filesysteminfile { constructor(string path); promise\<string> getfilename(); promise\<long long> getsize(); promise\<void> format(string filesystem); promise\<void> mount(); promise\<void> unmount(); }; creación de objetos para crear un objeto filesysteminfile , cargue el módulo @brightsign/filesysteminfile usando el método require() de node js® var filesysteminfile = require('@brightsign/filesysteminfile'); var fsif = new filesysteminfile("/storage/sd/usbstore img"); // node's native 'fs' can create an img file filesysteminfile use esta interfaz para configurar un filesysteminfile getfilename() promise\<string> getfilename() obtiene el nombre del archivo que se utilizó para crear filesysteminfile getsize() promise\<long long> getsize() recupera el tamaño (en bytes) del archivo del sistema de archivos utilizado para crear la clase filesysteminfile format() promise\<void> format(string filesystem) crea el sistema de archivos especificado en un archivo que ya existe los tipos de sistemas de archivos compatibles son "fat" y "exfat" mount() promise\<string> mount() monta el sistema de archivos no es necesario formatear el sistema de archivos si ya existe y está formateado la cadena devuelta es filepath y el valor devuelto es la ruta donde se ha montado el sistema de archivos no puede montarse más de una vez unmount() promise\<void> unmount() desmonta el sistema de archivos ejemplo el siguiente ejemplo crea un sistema de archivos, lo formatea, lo monta y lo desmonta usando el paquete npm 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); 	 }); 	}); }; también puede consultar nuestro ejemplo de github https //github com/brightsign/usb filesystem público que utiliza un dispositivo brightsign como sistema de archivos a través de usb