Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var sys = require('sys'), | ||
path = require('path'), | ||
fs = require('fs'), | ||
mongoose = require('../mongoose').Mongoose, // get Mongoose | ||
db = mongoose.connect('mongodb://localhost/test'); // connect to localhost, test db | ||
|
||
|
||
mongoose.load(__dirname+'/models/file.js'); // load model | ||
|
||
File = mongoose.get('File',db); | ||
|
||
recursiveFolderScan = function(dir,file){ | ||
var resource = (file) ? path.join(dir,file) : dir; | ||
|
||
var stats = fs.statSync(resource), obj = { dir : dir, file : file }; | ||
for(i in stats) if(typeof stats[i] != 'function') obj[i] = stats[i]; | ||
|
||
new File(obj).save(); // save modified stats object | ||
|
||
if(stats.isDirectory()){ | ||
fs.readdirSync(resource).forEach(function(path){ | ||
recursiveFolderScan(resource,path); | ||
}); | ||
} | ||
}; | ||
|
||
File.drop(function(){ // resetting data for example | ||
|
||
recursiveFolderScan(path.join(process.ENV.PWD)); // process 'examples' directory | ||
|
||
File.modifiedInLastWeek().each(function(doc){ | ||
// using custom getters | ||
sys.puts('\nModified this Week:\t\n\t'+doc.filePath +'\n\tsize: '+doc.sizeInKB+'\n\tmod time: '+doc.ISODateString(doc.mtime)); | ||
|
||
},true).then(function(){ | ||
File.close(); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
Model.define('File', { | ||
|
||
collection : 'fs', | ||
|
||
types: { | ||
dir : String, | ||
file : String, | ||
dev : Number, | ||
ino : Number, | ||
mode : Number, | ||
nlink : Number, | ||
uid : Number, | ||
gid : Number, | ||
rdev: Number, | ||
size: Number, | ||
blksize: Number, | ||
blocks: Number, | ||
atime: Date, | ||
mtime: Date, | ||
ctime: Date | ||
}, | ||
|
||
indexes : ['dir','file','size','ctime','mtime'], | ||
|
||
static : { | ||
|
||
modifiedInLastWeek : function(){ | ||
return this.find().gt({mtime : (Date.now() - 604800000)+'' }); | ||
} | ||
|
||
}, | ||
|
||
methods : { | ||
ISODateString : function(ts){ | ||
var d = new Date(parseInt(ts)); | ||
function pad(n){return n<10 ? '0'+n : n} | ||
return d.getUTCFullYear()+'-' | ||
+ pad(d.getUTCMonth()+1)+'-' | ||
+ pad(d.getUTCDate())+'T' | ||
+ pad(d.getUTCHours())+':' | ||
+ pad(d.getUTCMinutes())+':' | ||
+ pad(d.getUTCSeconds())+'Z' | ||
} | ||
}, | ||
|
||
setters: { | ||
atime : function(date){ | ||
if(!date) return; | ||
if(Object.prototype.toString.call(date) == '[object Date]') return date.getTime()+''; | ||
else return date; | ||
}, | ||
mtime : function(date){ | ||
if(!date) return; | ||
if(Object.prototype.toString.call(date) == '[object Date]') return date.getTime()+''; | ||
else return date; | ||
}, | ||
ctime : function(date){ | ||
if(!date) return; | ||
if(Object.prototype.toString.call(date) == '[object Date]') return date.getTime()+''; | ||
else return date; | ||
} | ||
|
||
}, | ||
|
||
getters: { | ||
sizeInKB : function(v){ return (this.size/1024).toFixed(2) + ' kb'; }, | ||
filePath : function(){ return require('path').join(this.dir,this.file); } | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters