forked from hmlovely/localhost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.js
238 lines (220 loc) · 9.66 KB
/
control.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* Created by JetBrains PhpStorm.
* User: hemin
* Date: 12-2-18
* Time: 13:53
* To change this template use File | Settings | File Templates.
* TODO:http://club.cnodejs.org/topic/4f16442ccae1f4aa27001071-后续需兼容大文件
*/
"use strict";
var route = require('./config'),
config = route.config,
url = require('url'),
querystring = require('querystring'),
fs = require('fs'),
jade = require('jade'),
path = require('path'),
mime = require('./mime'),
os = require('os'),
isWin = /(Windows)/i.test(os.type()),
exec = require('child_process').exec;
exports.init = function (req, res) {
var method = req.method;
if (method === 'GET') {
exports.GET(req, res);
} else if (method === 'POST') {
exports.POST(req, res);
}
};
exports.GET = function (req, res) {
var currentConfig = config[req.headers.host];
if (!currentConfig) {
res.end();
return;
}
//搜索功能
function search_key(str, dir, obj) {
//FIXME
}
var root = currentConfig.path;
root += (req.url);
if (-1 != req.url.indexOf("?key=")) {
var ret;
var key = req.url.slice(req.url.indexOf("?key=") + 5);
search_key(key, req.url.slice(0, req.url.indexOf("?key=")), ret);
res.writeHead(200, {'Content-type':'text/plain;charset=utf-8'});
res.write(ret);
res.end();
}
var extname = path.extname(root);
extname = extname !== '' ? extname.substring(1, extname.length) : '';
root = decodeURIComponent(root);
if (path.existsSync(root)) {
fs.stat(root, function (err, stats) {
//如果是目录,则读取目录的所有文件
if (stats.isDirectory()) {
fs.readdir(root, function (err, files) {
res.writeHead(200, {'Content-type':'text/html;charset=utf-8'});
var _path = (req.url === '/' ? '' : req.url),
data = {
list:[]
};
if (files) {
files.forEach(function (item, index) {
try {
var _stats = fs.statSync(root + '/' + item);
data.list.push({
href:_path + '/' + encodeURIComponent(item),
text:'【' + (_stats.isDirectory() ? 'DIR' : 'File') + '】' + item,
isDirecotory:_stats.isDirectory(),
time:_stats.ctime,
size:_stats.size,
filename:encodeURIComponent(item),
path:_path,
isImg:/(png|jpg|jpeg|bmp|gif)/gi.test(item),
index:index
});
} catch (e) {
console.log('Error:' + item);
}
});
}
data.returnParent = {
//由于window和unix操作系统,路径的 / 和 \意义不一样,所以这里需要对win进行处理
href:path.normalize(_path + '/..').replace(/\\/gi, '/'),
isDirecotory:true
};
//页面标题
data['pageTitle'] = decodeURIComponent(path.normalize(_path));
//URL路径
if (isWin) {
data['path'] = data['pageTitle'].split('\\');
} else {
data['path'] = data['pageTitle'].split('/');
}
data['path'] = data['path'].map(function (item, i) {
if (i > 0) {
//判断是否为最后一个元素
if (i < data['path'].length - 1) {
return {
text:item,
href:encodeURIComponent(data['path'].slice(1, i + 1).join('/'))
};
//否则,最后一个元素是不需要链接的
} else {
return {
text:item
}
}
} else {
return {
text:'Root',
href:''
}
}
});
data.config = config;
var fn = jade.compile(fs.readFileSync('./views/layout.jade'));
data.dataString = JSON.stringify(data);
res.end(fn(data));
}
)
}
//如果是文件则读取文件内容
else if (stats.isFile()) {
fs.readFile(root, function (err, data) {
if (!err) {
res.writeHead(200, {'Content-Type':mime.config[extname] ? mime.config[extname] : 'object/stream'});
res.end(data);
} else {
res.end(err.toString());
}
});
} else {
res.writeHead(200, {'Content-type':'text/plain"'});
res.end('unsupport type of file.');
}
}
)
}
else {
res.writeHead(404, {'Content-type':'text/html; charset="utf-8"'});
var fn = jade.compile(fs.readFileSync('./views/404.jade'));
res.end(fn({path:root}));
}
};
exports.POST = function (req, res) {
var _url = url.parse('http://' + req.headers.host + req.url);
var postData = [];
if (_url.pathname === '/modifyConfig') {
console.log('Modify Config!');
req.addListener("data", function (postDataChunk) {
postData.push(postDataChunk);
});
req.addListener('end', function () {
config.currentView = querystring.parse(postData.join()).currentView;
res.end(JSON.stringify(config));
exports.saveConfig();
});
} else if (_url.pathname === '/delete') {
postData = [];
req.addListener("data", function (postDataChunk) {
postData.push(postDataChunk);
});
req.addListener('end', function () {
var filename = querystring.parse(postData.join(''));
var files = config[req.headers.host].path + decodeURIComponent(filename.files);
files = files.replace(/\//gmi, '\\');
if (filename.isdir === 'file') {
fs.unlink(files, function (err) {
console.log(err);
if (!err) {
res.end(JSON.stringify({success:true}))
} else {
res.end(JSON.stringify({error:err.toString()}));
}
});
} else if (filename.isdir === 'dir') {
files = files.replace(/\//gmi, '\\');
try {
exec('rd /S /Q ' + files);
res.end(JSON.stringify({'success':'done'}));
} catch (e) {
res.end(JSON.stringify({"error":e.toString()}));
}
} else {
res.end(JSON.stringify({error:'Not understand!'}));
}
postData = [];
});
} else if (_url.pathname === "/rename") {
console.log('Rename file!');
req.addListener("data", function (postDataChunk) {
postData.push(postDataChunk);
});
req.addListener('end', function () {
var renamefile = querystring.parse(postData.join());
var path = renamefile.path;
var origin = config[req.headers.host].path + path + '\\' + decodeURIComponent(renamefile.origin);
var to = config[req.headers.host].path + path + '\\' + renamefile.to;
origin = origin.replace(/\//gmi, '\\');
to = to.replace(/\//gmi, '\\');
console.log(path, to, origin)
fs.rename(origin, to, function (err) {
if (!err) {
res.end(JSON.stringify({success:true}));
} else {
res.end(JSON.stringify({success:false}));
}
});
});
}
};
exports.saveConfig = function () {
var _tempObj = [];
_tempObj.push('exports.config={};');
Object.keys(config).forEach(function (key) {
_tempObj.push('exports.config.' + [key] + ' =' + JSON.stringify(config[key], undefined, '\t') + ';');
});
fs.writeFile('config.js', _tempObj.join('\r\n\r\n'));
};