Merge branch 'master' of github.com:Jlavatai/strapp
[henge/kiak.git] / strappFileManager.js
1 /**
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
6 * @version 0.0.1
7 * @license AGPL-3.0
8 * @copyright Strapp.io
9 */
10
11 import localforage from "localforage"
12 import StrappPeerConnection from "strappPeerConnection"
13
14 /* File constructor */
15 class File extends Object {
16 constructor(...props) {
17 super()
18 return Object.assign(this, new.target.defaults, ...props)
19 }
20 get() {
21 return this.data
22 }
23 post(postedData) {
24 this.data += postedData
25 this.lastModified = new Date()
26 }
27 put(putData) {
28 this.data = putData
29 this.lastModified = new Date()
30 }
31 delete() {
32 this.data = ''
33 this.lastModified = new Date()
34 }
35 connect() {
36
37 }
38 options(publicKey) {
39 return this.availPermissions(publicKey)
40 }
41 }
42 /* TODO: Continue to flesh this out */
43 File.defaults = {
44 name: '',
45 data: '',
46 mode: {},
47 size: 0
48 //lastModified: new Date()?
49 //lastAccessed: new Date()?
50
51 }
52
53
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.*/
60
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? */
63
64 /* These are the default files on all file systems */
65 let defaultFiles = [ "..", ".", "accounts", "ice", "log", "run" ]
66
67
68 /* TODO: Protect data via closures? */
69 const fs = {
70 /* TODO: What if files are added to file system before init is is called? */
71 initFileSystem(){
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)
78
79 }).catch( (err) => {
80 console.log(`error: ${err} when iterating through localForage during initFileSystem`)
81 })
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') {
89 //restore file
90 }
91 /* Else don't do anything, file exists in FS and doesnt need to be restored */
92 }
93 else {
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) */
97 if (val === '..') {
98 let file = new StrappPeerConnection()
99 /* Connect with host */
100 }
101 else {
102 /* Each default file is going to have specific permissions, */
103 let filedata = new File()
104 filedata.name = val
105 filedata.mode =
106 this.createFile(val,)
107 }
108 }
109
110 })
111
112 },
113
114 fileExists(filename) {
115 return this.files[filename] === undefined ? false : true
116 },
117
118 /* Create a file in the file system, if specified overwrites any file that is already there
119 else does nothing */
120 createFile(filename, filedata, overwrite = false){
121 filedata.name = filename
122 if (this.files[filename] === undefined) {
123 this.files[filename] = filedata
124 }
125 else {
126 if (overwrite) {
127 console.log(`Overwriting ${filename}`)
128 this.files[filename] = filedata
129 }
130 else {
131 console.log(`Didn't overwrite file so nothing happened`)
132 }
133 }
134 },
135
136 /* Get a file from browser session memory */
137 /* TODO: Option to get from localForage? */
138 getFile(filename) {
139 return this.files[filename]
140 },
141
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)
146 },
147
148 /* Delete file from localForage */
149 removeFile(filename) {
150 this.db.removeItem(filename)
151 },
152
153 /* Delete a file from file system */
154 killFile(filename) {
155 delete this.files[filename]
156
157 },
158
159 /* Store file in file system to localForage */
160 storeFile(filename) {
161 this.db.setItem(filename, this.files[filename].filedata)
162 },
163
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)
169 },
170
171 saveFileSystem() {
172 /* TODO: save all files in filesystem to localforage */
173 },
174 db: {},
175 files: {}
176
177 }
178
179 /* File System API */
180
181
182 /* Load file system from localStorage/indexedDB if strapp.js is running on a host */
183 function loadFileSystem(){
184
185 }
186
187 /* Store file system before shutting down if strapp.js is running on a host- */
188 function storeFileSystem(){}
189
190
191
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
196
197 /* Determine if a publicKey has permissions to execute methods
198
199 /* rm - Delete file */
200
201 /* ls - display file contents */
202 //open file
203 //return all of file files
204
205 /* cat - display file data */
206 //return file data
207
208 /* open - Open file */
209 //traverse to file path
210 /* perm - display file permissions (if you have permissions) */
211 /* find - find a file */
212 //@arg fileToFind
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
220
221
222
223
224