X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=strappFileManager.js;fp=strappFileManager.js;h=6f0b9d48864ffebaac49279043e5732f923a2c01;hp=0000000000000000000000000000000000000000;hb=44e0b2d571c71ebd410b78366c8bc9dc463a96c4;hpb=d17ac76d8256b00eb253138a554c7d3d51139b9e diff --git a/strappFileManager.js b/strappFileManager.js new file mode 100644 index 0000000..6f0b9d4 --- /dev/null +++ b/strappFileManager.js @@ -0,0 +1,224 @@ +/** + * @file File System Interface + * @desc Provides basic file commands for interacting with Strapp + * file system as well as storage (and backups) of file system + * @author Jordan Lavatai and Ken Grimes + * @version 0.0.1 + * @license AGPL-3.0 + * @copyright Strapp.io + */ + +import localforage from "localforage" +import StrappPeerConnection from "strappPeerConnection" + +/* File constructor */ +class File extends Object { + constructor(...props) { + super() + return Object.assign(this, new.target.defaults, ...props) + } + get() { + return this.data + } + post(postedData) { + this.data += postedData + this.lastModified = new Date() + } + put(putData) { + this.data = putData + this.lastModified = new Date() + } + delete() { + this.data = '' + this.lastModified = new Date() + } + connect() { + + } + options(publicKey) { + return this.availPermissions(publicKey) + } +} +/* TODO: Continue to flesh this out */ +File.defaults = { + name: '', + data: '', + mode: {}, + size: 0 + //lastModified: new Date()? + //lastAccessed: new Date()? + +} + + +/* Filesystem maintains the current session memory for the strapp instance. Files can be + created and killed from the filesystem without leveraging localForage. Files that are + in the filesystem can be stored to localForage while files that are in localForage can be loaded + to the filesystem. When a Filesystem is first intialized, it attempts to get its strappID it populates itself from localForage and + overwrites any files in its current memory. Files that have restore() as a property (which will be some method needed to + make the file functions e.g. strappPeerConnections will restore() themselves lazily, i.e. when they are needed.*/ + + /* TODO: Should it be possible to create/preserve/destroy a file to both localForage and fileSystem? (same time) */ + /* TODO: Should initFileSystem not overwrite files? */ + +/* These are the default files on all file systems */ +let defaultFiles = [ "..", ".", "accounts", "ice", "log", "run" ] + + +/* TODO: Protect data via closures? */ +const fs = { + /* TODO: What if files are added to file system before init is is called? */ + initFileSystem(){ + this.db = localforage.createInstance({ name: "database" }) + /* Iterate through all files on localforage, adding them to FileSystem + and calling their restore methods */ + this.db.iterate( (value, key, n) => { + /* just btw, return !undefined to exit early */ + this.loadFile(key, true) + + }).catch( (err) => { + console.log(`error: ${err} when iterating through localForage during initFileSystem`) + }) + /* Add the hardcoded default files if they dont already exist */ + /* Restore these files --> need the private/public key*/ + initialFiles.map( (val, idx, array) => { + if (this.fileExists(val)) { + let file = this.getFile(val) + let restoreProp = file['restore'] + if (restoreProp === undefined && typeof restoreFx === 'function') { + //restore file + } + /* Else don't do anything, file exists in FS and doesnt need to be restored */ + } + else { + /* TODO: Remove checking for every file --> although its only for the default files which + will probably be a low number. Still, unnecessary. Could make initialFiles a + Map object with fileType in it and switch(val.fileType) */ + if (val === '..') { + let file = new StrappPeerConnection() + /* Connect with host */ + } + else { + /* Each default file is going to have specific permissions, */ + let filedata = new File() + filedata.name = val + filedata.mode = + this.createFile(val,) + } + } + + }) + + }, + + fileExists(filename) { + return this.files[filename] === undefined ? false : true + }, + + /* Create a file in the file system, if specified overwrites any file that is already there + else does nothing */ + createFile(filename, filedata, overwrite = false){ + filedata.name = filename + if (this.files[filename] === undefined) { + this.files[filename] = filedata + } + else { + if (overwrite) { + console.log(`Overwriting ${filename}`) + this.files[filename] = filedata + } + else { + console.log(`Didn't overwrite file so nothing happened`) + } + } + }, + + /* Get a file from browser session memory */ + /* TODO: Option to get from localForage? */ + getFile(filename) { + return this.files[filename] + }, + + /* Save a file to file system*/ + saveFile(filename, filedata) { + /* TODO: Determine if file to be saved is of saveable nature e.g. SPC's cant really be saved */ + this.db.setItem(filename, filedata) + }, + + /* Delete file from localForage */ + removeFile(filename) { + this.db.removeItem(filename) + }, + + /* Delete a file from file system */ + killFile(filename) { + delete this.files[filename] + + }, + + /* Store file in file system to localForage */ + storeFile(filename) { + this.db.setItem(filename, this.files[filename].filedata) + }, + + /* Load file from localForage to file system */ + loadFile(filename, overwrite = false) { + let filedata = this.db.getItem(filename) + filedata = filedata.restore === undefined ? filedata : filedata.restore() + this.createFile(filename, filedata, overwrite) + }, + + saveFileSystem() { + /* TODO: save all files in filesystem to localforage */ + }, + db: {}, + files: {} + +} + +/* File System API */ + + +/* Load file system from localStorage/indexedDB if strapp.js is running on a host */ +function loadFileSystem(){ + +} + +/* Store file system before shutting down if strapp.js is running on a host- */ +function storeFileSystem(){} + + + +/* addFile - adds a created file to a file */ +//@arg fileType - what to set the type property +//@arg fileData - what to set the data property +//@arg filePos - where to create the file + +/* Determine if a publicKey has permissions to execute methods + +/* rm - Delete file */ + +/* ls - display file contents */ + //open file +//return all of file files + +/* cat - display file data */ +//return file data + +/* open - Open file */ + //traverse to file path +/* perm - display file permissions (if you have permissions) */ +/* find - find a file */ +//@arg fileToFind +/* stat - info about a file */ +//@arg path - path to the file +/* exists - determine if a file contains a file */ +//@arg {String} searchedFile - file to be searched +//@arg {String} fileName +//@arg {Number} Depth to look +//@return {Boolean} true if exists, false if doesn't + + + + +