-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
110 lines (83 loc) · 2.69 KB
/
server.js
File metadata and controls
110 lines (83 loc) · 2.69 KB
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
const net = require('net');
const chalk = require('chalk');
const fs = require('fs');
const options = {
host: 'localhost',
port: 8080
};
const FILEPATH = './files/';
const server = new net.Server();
server.listen(options);
console.log(chalk.green("Fileserver is now running"));
server.on('connection', (client) => {
console.log(chalk.yellow(`Client @${client.address().address}:${client.address().port}(${client.address().family}) connected`));
client.on('data', (message) => {
const data = JSON.parse(message);
console.log(data);
if (data.command === 'list') {
console.log('Reading Directory..');
const files = fs.readdirSync(FILEPATH);
client.write(JSON.stringify({
data: files,
type: 'list'
}) + '|');
}
if (data.command === 'getfile') {
const files = fs.readdirSync(FILEPATH);
let doesExist = false;
let date = new Date();
let size = 0;
let elapsed;
//Search for the file
for (let file of files) {
if (file.includes(data.arguments)) {
doesExist = true;
}
}
//If file doesnt exist, let the client know
if (!doesExist) {
client.write(JSON.stringify({
type: 'failure',
message: 'File does not exist on the server'
}));
//Otherwise carry on with file fetching
} else {
let stream = fs.createReadStream(FILEPATH + data.arguments);
//Let the client know we are ready to write the stream
client.write(JSON.stringify({
type: 'fileinit',
data: data.arguments
}));
//Once the file is readable, begin stream
stream.on('readable', function() {
console.log(chalk.yellow("Connected user fetched file " + FILEPATH + data.arguments));
let chunk;
//While stream is reading... and chunk is not empty
while ((chunk = this.read()) !== null) {
size += chunk.length;
client.write('|' + JSON.stringify({
chunk: chunk,
type: 'chunk'
}));
}
});
stream.on('error', (error)=>{
client.write('|' + JSON.stringify({
type: 'failire',
message: 'File could not be read, please try again. Error: ' + error
}));
});
stream.on('end', function() {
elapsed = new Date() - date;
client.write('|' + JSON.stringify({
type: 'success',
message: `${size}kb file transferred in ${(elapsed).toFixed(2)}ms`
}));
});
}
}
});
});
server.on('data', (client) => {
console.log("");
});