Comparing version 1.1.1 to 1.1.2
{ | ||
"name": "fsgod", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Secure file sharing, hosting, and transfer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
172
README.md
## FSGOD (Alpha) | ||
# FSGOD | ||
A service for securely managing, transfering, collaborating, and hosting content on your local network | ||
A library for omnipotent control over your file system with ease | ||
The only component thus far is the Virtual Directory Tree (VDT). | ||
The purpose of this library is to make dynamic managing of any size of file system easy and quick. | ||
## API | ||
@@ -14,3 +12,3 @@ | ||
Recursively step through a file tree to create a 'Virtual File Tree' which allows for quicker data processing and greater control over your file system | ||
The VDT is a recursively created JavaScript object generated from a path you designate in your file system. The VDT makes it much easier to search files for content, write/append/prepend/delete files, create files and directories, and perform a multitude of other file system oriented tasks that would typically be repetitive and tricky to do dynamically and efficiently. | ||
@@ -21,7 +19,5 @@ ```javascript | ||
// fsgod.VDT and fsgod.VirtualDirectoryTree are aliases of the same function. | ||
fsgod.VDT('./', (vdt) => { | ||
var myDirectory = vdt; | ||
for ( var i = 0; i < myDirectory.content.length; i++ ){ | ||
console.log(myDirectory.content[i].name); | ||
for ( var i = 0; i < vdt.content.length; i++ ){ | ||
console.log(vdt.content[i].name); | ||
} | ||
@@ -32,14 +28,15 @@ }); | ||
The above code should log the names of all the items in the directory it's currently in | ||
The above code should log the names of all the items in the directory it's currently in to the console | ||
The `vdt` object that gets passed to the callback in `fsgod.VDT` is the Virtual Directory Tree. `fsgod.VDT` recursively walks through a specified folder and builds each item into a JavaScript object and passes it back to the user in the callback. For the meantime, `fsgod.VDT` can only be used to read and search file system data and content. If you'd like to help, email me at maui.wowie@tuta.io. | ||
The `vdt` object that gets passed to the callback in `fsgod.VDT` is the Virtual Directory Tree. `fsgod.VDT` recursively walks all the way through the specified folder and builds each item into a JavaScript object and passes it back to the user in the callback. | ||
### VDT.search | ||
## VDT Directory Methods | ||
Every directory in the VDT (including the root/target directory) has a search method. The search method uses regular expressions to locate any item containing the string passed in the first argument. | ||
### vdt.search | ||
Every directory in the VDT (including the root/target directory) has a `search` method. The `search` method uses regular expressions to locate any item containing the string passed in the first argument. The `search` method only searches in and under the directory your are performing the search on | ||
```javascript | ||
fsgod.VDT('./', (vdt) => { | ||
var myDirectory = vdt; | ||
myDirectory.search('test', { content: false }, (results) => { | ||
vdt.search('test', { content: false }, (results) => { | ||
console.log('Found ' + results.length + ' items'); | ||
@@ -53,3 +50,3 @@ for (var i = 0; i < results.length; i++){ | ||
``` | ||
The above code should find file names and directory names that contain the phrase 'test' and print their full paths. | ||
The above code should find file and directory names that contain the phrase 'test' and print their full paths. | ||
@@ -59,7 +56,9 @@ You should notice the second argument takes an object which specifies search options; in the case of the above example, we don't want to look through file content for our search string, just names. This argument is optional and simply calling: | ||
```javascript | ||
vdt.search('test', (results) => { | ||
console.log('Found ' + results.length + ' items'); | ||
for (var i = 0; i < results.length; i++){ | ||
console.log(results[i].fullPath); | ||
} | ||
fsgod.VDT('./', (vdt) => { | ||
vdt.search('test', (results) => { | ||
console.log('Found ' + results.length + ' items'); | ||
for (var i = 0; i < results.length; i++){ | ||
console.log(results[i].fullPath); | ||
} | ||
}); | ||
}); | ||
@@ -79,2 +78,127 @@ ``` | ||
### vdt.get | ||
Because dynamically getting objects at variable depths can be very tricky, you can use the `get` method to easily navigate the VDT. For example, if you had a directory named `Foo` and a file named `bar.txt` inside it, you could use the following code to retrieve `bar.txt` | ||
```javascript | ||
fsgod.VDT('./Foo', (vdt) => { | ||
var bar = vdt.get('bar.txt'); | ||
}); | ||
``` | ||
#### Chaining vdt.get | ||
The `get` method can be chained either in the url passed into the first argument, or together in sequence. Lets say directory `Foo` has 3 directories underneath it, all named `Foo` and `bar.txt` is at the bottom of this tree. i.e. the local path to `bar.txt` is `Foo/Foo/Foo/Foo/bar.txt`. You could `get` it in two ways | ||
```javascript | ||
// The static way | ||
fsgod.VDT('./Foo', (vdt) => { | ||
var bar = vdt.get('Foo').get('Foo').get('Foo').get('bar.txt'); | ||
}); | ||
// The dynamic (right) way | ||
fsgod.VDT('./Foo', (vdt) => { | ||
var bar = vdt.get('Foo/Foo/Foo/bar.txt'); | ||
}); | ||
``` | ||
### vdt.mkdir | ||
The `mkdir` method will create a directory for the vdt object and in the appropriate place in your file system. Lets say we wanted to create a directory named `Foo`, and then create a directory inside `Foo` named `Bar`. This operation can be made easy using the `get` method along side `mkdir` | ||
```javascript | ||
fsgod.VDT('./', (vdt) => { | ||
vdt.mkdir('Foo', (err) => { | ||
if (err) throw err; | ||
vdt.get('Foo').mkdir('Bar', (err) => { | ||
if (err) throw err; | ||
// Foo/Bar now exists! | ||
}); | ||
}); | ||
}); | ||
``` | ||
### vdt.mkfile | ||
The `mkfile` method will create a file for the vdt object and in the appropriate place in your file system. Lets say we wanted to create a directory named `Foo`, and then create a text file inside `Foo` named `bar.txt` | ||
```javascript | ||
fsgod.VDT('./', (vdt) => { | ||
vdt.mkdir('Foo', (err) => { | ||
if (err) throw err; | ||
vdt.get('Foo').mkfile('bar.txt', (err) => { | ||
if (err) throw err; | ||
// Foo/bar.txt now exists! | ||
}); | ||
}); | ||
}); | ||
``` | ||
You can also give the file content when you create it like so | ||
```javascript | ||
fsgod.VDT('./', (vdt) => { | ||
vdt.mkfile('bar.txt', 'Hello World', (err) => { | ||
// vdt.get(bar.txt).content = Hello World | ||
}); | ||
}); | ||
``` | ||
## VDT File Methods | ||
### vdt.write | ||
The `write` method overwrites the content of the source file and VDT file object | ||
Note: `write` and `overwrite` reference the same function | ||
```javascript | ||
fsgod.VDT('./Foo', (vdt) => { | ||
var bar = vdt.get('bar.txt'); | ||
// bar.content = 'Hello World' | ||
bar.write('Goodbye world', (err) => { | ||
if (err) throw err; | ||
// bar.content = 'Goodbye World' | ||
}); | ||
}); | ||
``` | ||
### vdt.append | ||
The `append` method adds content to the end of the source file content and VDT file object | ||
```javascript | ||
fsgod.VDT('./Foo', (vdt) => { | ||
var bar = vdt.get('bar.txt'); | ||
// bar.content = 'Hello ' | ||
bar.append('World', (err) => { | ||
if (err) throw err; | ||
// bar.content = 'Hello World' | ||
}); | ||
}); | ||
``` | ||
### vdt.prepend | ||
The `prepend` method adds content to the begining of the source file content and VDT file object | ||
```javascript | ||
fsgod.VDT('./Foo', (vdt) => { | ||
var bar = vdt.get('bar.txt'); | ||
// bar.content = 'World' | ||
bar.prepend('Hello ', (err) => { | ||
if (err) throw err; | ||
// bar.content = 'Hello World' | ||
}); | ||
}); | ||
``` | ||
## Virtual Directory | ||
@@ -92,2 +216,5 @@ | ||
| search | Function | Search in and under the directory for a search string | | ||
| get | Function | Gets the specified object and makes it's methods easily available | | ||
| mkdir | Function | Creates a directory in the VDT and your file system | | ||
| mkfile | Function | Creates a file in the VDT and your file system | | ||
@@ -103,1 +230,4 @@ ## Virtual File | ||
| size | Integer | Size of the item in bytes | | ||
| write/overwrite | Function | Overwrites the content of the file | | ||
| append | Function | Adds new content to the end of the file | | ||
| prepend | Function | Adds new content to the beginning of the file | |
@@ -47,6 +47,6 @@ (function () { | ||
fo.__getContentFromSrc = function (done) { | ||
fs.readFile(fo.fullPath, 'utf-8', (err, data) => { | ||
if (err) throw err; | ||
fs.readFile(fo.fullPath, 'utf8', (err, data) => { | ||
if (err) done(err); | ||
fo.__updateFOContent(data, () => { | ||
done(); | ||
done(null); | ||
}); | ||
@@ -119,3 +119,99 @@ }); | ||
FileObject.prototype.json = function(){ | ||
var fo = this; | ||
if (fo.name.split(".").reverse()[0] !== 'json') throw 'The .json() method is only for files with the .json file extension!'; | ||
return fs.readFile(fo.name, 'utf8', (err, data) => { | ||
if (err) throw err; | ||
return JSON.parse(data); | ||
}); | ||
}; | ||
FileObject.prototype.append = function($newContent, $done){ | ||
var fo = this; | ||
fo.__updateFileSrc(fo.content + $newContent, (err) => { | ||
$done(); | ||
}); | ||
}; | ||
FileObject.prototype.prepend = function($newContent, $done){ | ||
var fo = this; | ||
fo.__updateFileSrc($newContent + fo.content, (err) => { | ||
$done(); | ||
}); | ||
}; | ||
FileObject.prototype.overwrite = function($newContent, $done){ | ||
var fo = this; | ||
fo.__updateFileSrc($newContent, (err) => { | ||
$done(err); | ||
}); | ||
}; | ||
// Alias for overwrite | ||
FileObject.prototype.write = FileObject.prototype.overwrite; | ||
VirtualDirectory.prototype.get = function($name){ | ||
var dir = this, | ||
isChainedRequest = $name.split(/[\/\\]/gi).length > 1 ? true : false, | ||
getItem; | ||
if (!isChainedRequest){ | ||
dir.content.forEach((item, index) => { | ||
if(item.name == $name){ | ||
getItem = item; | ||
} | ||
}); | ||
if (getItem) return getItem; | ||
else throw 'Item ' + $name + ' Does Not Exist'; | ||
} else { | ||
var pathUrl = $name.split(/[\/\\]/gi), | ||
operator = dir.get(pathUrl[0]); | ||
for(var i = 1; i < pathUrl.length; i++){ | ||
operator = operator.get(pathUrl[i]); | ||
} | ||
getItem = operator; | ||
if (getItem) return getItem; | ||
} | ||
}; | ||
VirtualDirectory.prototype.mkfile = function($name, $content, $cb){ | ||
var dir = this, | ||
newPath = dir.fullPath + '/' + $name; | ||
if (!$cb && typeof $content == 'object') { $cb = $content; $content = ''; } | ||
fs.exists(newPath, (exists) => { | ||
if (!exists){ | ||
fs.writeFile(newPath, $content, (err) => { | ||
if (err) $cb(err); | ||
fs.stat(newPath, (err, stat) => { | ||
if (err) $cb(err); | ||
dir.content.push(new FileObject(newPath, stat)); | ||
$cb(null); | ||
}); | ||
}); | ||
} else { | ||
$cb('File Already Exists'); | ||
} | ||
}); | ||
}; | ||
VirtualDirectory.prototype.mkdir = function($name, $mask, $cb){ | ||
var dir = this, | ||
newPath = dir.fullPath + '/' + $name; | ||
if (typeof $mask == 'function') { | ||
$cb = $mask; | ||
$mask = 484; | ||
} | ||
fs.mkdir(newPath, $mask, function (err) { | ||
var exists = false; | ||
if (err) if (err.code === 'EEXIST') exists = true; else $cb(err); | ||
if (!exists) { | ||
fs.stat(newPath, (err, stat) => { | ||
if (err) $cb(err); | ||
dir.content.push(new VirtualDirectory(newPath, stat)); | ||
$cb(null); | ||
}); | ||
} else { | ||
$cb(null); | ||
} | ||
}); | ||
}; | ||
VirtualDirectory.prototype.search = function($term, $options, $cb){ | ||
@@ -199,2 +295,9 @@ var dir = this; | ||
VirtualDirectory.prototype.listContents = function(){ | ||
var dir = this; | ||
for (var i = 0; i < dir.content.length; i++){ | ||
console.log(dir.content[i].name); | ||
} | ||
}; | ||
function end() { | ||
@@ -204,4 +307,2 @@ if (scansStarted === scansFinished) $done(VDT); | ||
} | ||
})(); | ||
})(); |
20671
300
226
5