6f0b9d48864ffebaac49279043e5732f923a2c01
2 * @file File System Interface
3 * @desc Provides basic file commands for interacting with Strapp
4 * file system as well as storage (and backups) of file system
5 * @author Jordan Lavatai and Ken Grimes
11 import localforage
from "localforage"
12 import StrappPeerConnection
from "strappPeerConnection"
14 /* File constructor */
15 class File
extends Object
{
16 constructor(...props
) {
18 return Object
.assign(this, new.target
.defaults
, ...props
)
24 this.data
+= postedData
25 this.lastModified
= new Date()
29 this.lastModified
= new Date()
33 this.lastModified
= new Date()
39 return this.availPermissions(publicKey
)
42 /* TODO: Continue to flesh this out */
48 //lastModified: new Date()?
49 //lastAccessed: new Date()?
54 /* Filesystem maintains the current session memory for the strapp instance. Files can be
55 created and killed from the filesystem without leveraging localForage. Files that are
56 in the filesystem can be stored to localForage while files that are in localForage can be loaded
57 to the filesystem. When a Filesystem is first intialized, it attempts to get its strappID it populates itself from localForage and
58 overwrites any files in its current memory. Files that have restore() as a property (which will be some method needed to
59 make the file functions e.g. strappPeerConnections will restore() themselves lazily, i.e. when they are needed.*/
61 /* TODO: Should it be possible to create/preserve/destroy a file to both localForage and fileSystem? (same time) */
62 /* TODO: Should initFileSystem not overwrite files? */
64 /* These are the default files on all file systems */
65 let defaultFiles
= [ "..", ".", "accounts", "ice", "log", "run" ]
68 /* TODO: Protect data via closures? */
70 /* TODO: What if files are added to file system before init is is called? */
72 this.db
= localforage
.createInstance({ name
: "database" })
73 /* Iterate through all files on localforage, adding them to FileSystem
74 and calling their restore methods */
75 this.db
.iterate( (value
, key
, n
) => {
76 /* just btw, return !undefined to exit early */
77 this.loadFile(key
, true)
80 console
.log(`error: ${err} when iterating through localForage during initFileSystem`)
82 /* Add the hardcoded default files if they dont already exist */
83 /* Restore these files --> need the private/public key*/
84 initialFiles.map( (val, idx, array) => {
85 if (this.fileExists(val)) {
86 let file = this.getFile(val)
87 let restoreProp = file['restore']
88 if (restoreProp === undefined && typeof restoreFx === 'function') {
91 /* Else don't do anything, file exists in FS and doesnt need to be restored */
94 /* TODO: Remove checking for every file --> although its only for the default files which
95 will probably be a low number. Still, unnecessary. Could make initialFiles a
96 Map object with fileType in it and switch(val.fileType) */
98 let file
= new StrappPeerConnection()
99 /* Connect with host */
102 /* Each default file is going to have specific permissions, */
103 let filedata
= new File()
106 this.createFile(val
,)
114 fileExists(filename
) {
115 return this.files
[filename
] === undefined ? false : true
118 /* Create a file in the file system, if specified overwrites any file that is already there
120 createFile(filename
, filedata
, overwrite
= false){
121 filedata
.name
= filename
122 if (this.files
[filename
] === undefined) {
123 this.files
[filename
] = filedata
127 console
.log(`Overwriting ${filename}`)
128 this.files
[filename
] = filedata
131 console
.log(`Didn't overwrite file so nothing happened`)
136 /* Get a file from browser session memory */
137 /* TODO: Option to get from localForage? */
139 return this.files
[filename
]
142 /* Save a file to file system*/
143 saveFile(filename
, filedata
) {
144 /* TODO: Determine if file to be saved is of saveable nature e.g. SPC's cant really be saved */
145 this.db
.setItem(filename
, filedata
)
148 /* Delete file from localForage */
149 removeFile(filename
) {
150 this.db
.removeItem(filename
)
153 /* Delete a file from file system */
155 delete this.files
[filename
]
159 /* Store file in file system to localForage */
160 storeFile(filename
) {
161 this.db
.setItem(filename
, this.files
[filename
].filedata
)
164 /* Load file from localForage to file system */
165 loadFile(filename
, overwrite
= false) {
166 let filedata
= this.db
.getItem(filename
)
167 filedata
= filedata
.restore
=== undefined ? filedata
: filedata
.restore()
168 this.createFile(filename
, filedata
, overwrite
)
172 /* TODO: save all files in filesystem to localforage */
179 /* File System API */
182 /* Load file system from localStorage/indexedDB if strapp.js is running on a host */
183 function loadFileSystem(){
187 /* Store file system before shutting down if strapp.js is running on a host- */
188 function storeFileSystem(){}
192 /* addFile - adds a created file to a file */
193 //@arg fileType - what to set the type property
194 //@arg fileData - what to set the data property
195 //@arg filePos - where to create the file
197 /* Determine if a publicKey has permissions to execute methods
199 /* rm - Delete file */
201 /* ls - display file contents */
203 //return all of file files
205 /* cat - display file data */
208 /* open - Open file */
209 //traverse to file path
210 /* perm - display file permissions (if you have permissions) */
211 /* find - find a file */
213 /* stat - info about a file */
214 //@arg path - path to the file
215 /* exists - determine if a file contains a file */
216 //@arg {String} searchedFile - file to be searched
217 //@arg {String} fileName
218 //@arg {Number} Depth to look
219 //@return {Boolean} true if exists, false if doesn't