forked from GeraldWodni/theforth.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
158 lines (132 loc) · 5.37 KB
/
ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// theforth.com ajax callbacks
// (c)copyright 2014 by Gerald Wodni <[email protected]>
var _ = require('underscore');
var async = require('async');
var jade = require('jade');
var fs = require('fs');
var path = require('path');
module.exports = {
setup: function( k ) {
function renderTemplate( req, res, template, data ) {
//var filename = __dirname + '/views/admin/items/' + template + '.jade';
console.log( "RENDER".bold.blue, req.path, typeof res.json === "function", template, data );
async.map( data, function( item, done ) {
k.renderJade( req,
{
send: function( html ) {
done( null, html );
}
}, "admin/items/"+ template, item );
},
function( err, results ) {
if( err )
return res.json( { error: err } );
res.json( { html: results.join( " " ) } );
});
}
function ajaxError( res, err ) {
console.log( err );
res.statusCode = 500;
res.json( { error: err } );
};
function getUserFolder( req ) {
return path.join( k.kernOpts.websitesRoot, k.modules.hierarchy.website( k.kernOpts.websitesRoot, req.kern.website ), "files", req.session.loggedInUsername );
};
// http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
//var walk = function(dir, done) {
// var results = [];
// fs.readdir(dir, function(err, list) {
// if (err) return done(err);
// var pending = list.length;
// if (!pending) return done(null, results);
// list.forEach(function(file) {
// file = dir + '/' + file;
// fs.stat(file, function(err, stat) {
// if (stat && stat.isDirectory()) {
// walk(file, function(err, res) {
// results = results.concat(res);
// if (!--pending) done(null, results);
// });
// } else {
// results.push(file);
// if (!--pending) done(null, results);
// }
// });
// });
// });
//};
function readDirs( res, prefix, dir, done ) {
var results = [];
fs.readdir( dir, function( err, items ) {
if( err )
return ajaxError( res, err );
var pending = items.length;
var results = [];
if( !pending )
return done( null, results );
items.forEach( function( item ) {
var p = path.join( dir, item );
fs.stat( p, function( err, stat ) {
if( err )
return ajaxError( res, err );
if( stat.isDirectory() ) {
var subPrefix = path.join( prefix, item );
readDirs( res, subPrefix, p, function( err, subItems ) {
results.push( {
name: item, path: prefix, isDirectory: true, children: subItems
});
if( !--pending )
done( null, results );
});
}
else {
results.push( { name: item, path: path.join( prefix, item ) } );
if( !--pending )
done( null, results );
}
});
});
});
};
k.router.get("/ls", function( req, res ) {
var folder = getUserFolder( req );
readDirs( res, "", folder, function( err, items ) {
console.log( "READDIRS".magenta.bold, items );
//res.json({ status: "DONE!"} );
res.json( items );
});
return;
fs.readdir( folder, function( err, items ) {
if( err )
return ajaxError( res, err );
async.map( items, function( item, d ) {
//console.log( folder, item );
fs.stat( path.join( folder, item), d );
},
function( err, stats ) {
if( err )
return ajaxError( res, err );
var directories = [];
var files = [];
for( var i = 0; i < stats.length; i++ )
if( stats[i].isDirectory() )
directories.push( { name: items[i] } );
else
files.push( { name: items[i] } );
res.json( { directories: directories, files: files } );
});
});
});
k.router.get("/load/:path", function( req, res ) {
var folder = getUserFolder( req );
/* TODO: use realpath and compare prefixes */
var filename = path.join( folder, req.requestData.escapedLink( 'path' ) );
console.log( "LOAD:", filename );
fs.readFile( filename, function( err, data ) {
if( err )
return ajaxError( res, err );
res.json( { content: data.toString() } );
});
});
}
};