-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (58 loc) · 2.22 KB
/
app.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
//requiring path and fs modules
const http = require('http');
const path = require('path');
const fs = require('fs');
const express = require('express');
const socketIo = require('socket.io');
const port = 3000;
const homeDirectory = path.join(__dirname, 'Documents');
var app = express();
var server = http.createServer(app);
var io = socketIo.listen(server);
app.use(express.static('./client'));
io.of('/webFS').on('connection', function(socket){
socket.on('folderContent', msg => {
getFolderContent(msg, list => {
socket.emit('folderContent', list)
});
});
});
server.listen(port, function(){
console.log('listening on http://127.0.0.1:3000' /* + port */);
});
function pathSterilisateur(thepath) {
return path.join(homeDirectory, thepath);
}
function getFolderContent(directoryPath, callback) {
realPath = pathSterilisateur(directoryPath);
fs.exists(realPath, rsp => {
console.log('thepath', directoryPath, '(', realPath,') exist :',rsp);
if (rsp) {
fs.readdir(realPath, function (err, files) {
let result = {};
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(file => {
let filePath = path.join(realPath, file);
let fileStats = fs.lstatSync(filePath);
let type;
switch (true) {
case fileStats.isFile(): type = 'file'; break;
case fileStats.isDirectory(): type = 'directory'; break;
case fileStats.isBlockDevice(): type = 'blockDevice'; break;
case fileStats.isCharacterDevice(): type = 'characterDevice'; break;
case fileStats.isSymbolicLink(): type = 'symbolicLink'; break;
case fileStats.isFIFO(): type = 'FIFO'; break;
case fileStats.isSocket(): type = 'socket'; break;
default: type = 'undefined'; break;
}
result[file] = {name:file, type, size: fileStats.size, content: undefined};
});
callback({result, _metadata: {call:directoryPath, date: (new Date()).toISOString()}})
});
} else {
//console.log('ERROR(unkwon):',directoryPath);
}
});
}