-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
160 lines (134 loc) · 4.92 KB
/
server.js
File metadata and controls
160 lines (134 loc) · 4.92 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
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
const http = require('http')
const chld = require('child_process');
var exec = require('child_process').exec;
const fs = require('fs');
const useragent = require('express-useragent');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
let del = false;
const server = http.createServer(function(request, response) {
if (request.method == 'POST') {
//console.log('POST')
var body = ''
request.on('data', function(data) {
body += data
})
request.on('end', function() {
openProgram(body);
})
} else {
// Detects if the user is on mobile or not
var source = request.headers['user-agent']
var ua = useragent.parse(source);
var isMobile = ua.isMobile
//console.log('GET')
response.writeHead(200, {'Content-Type': 'text/html'})
const appText = fs.readFileSync("data.txt",'utf8');
const appSplit = appText.split(/\r?\n/);
const lines = appSplit.filter((value, index) => {
return appSplit.indexOf(value) === index;
});
lines.filter(n => n)
response.write('<input id="add_name" style="display: inline-block;" type="text" placeholder="Name"> <br> <div class="same-line"><form method="post" class="hide-mobile main-button" > <input class="hidden" type="text" name="name" value="BrowseFile" /><input type="submit" value="BrowseFile" /> </form> <br> <form method="post" class="hide-mobile main-button"> <input class="hidden" type="text" name="name" value="Add" /><input type="submit" value="Add" onclick="deleteTimer()"/> </form> <br> <form method="post" action=website class="main-button"> <input class="hidden" type="text" name="name" value="Delete"/><input type="submit" value="Delete" onclick="deleteTimer()"/> </form> </div><br> <br><br>')
response.write("<table> <tr>")
var lineNumber = 0;
for(let i = 0 ; i < lines.length ; i++) {
if(lines[i] != "") {
lineNumber++;
//Adds the shortcut to the current line number
response.write("<td>")
let apps = lines[i].split("@")
response.write('<form method="post" > <input class="hidden" type="text" name="name" value=' + lines[i] + ' /><input id="formPost" type="submit" value=' + apps[0] + ' /> </form>');
response.write("</td>")
// If the user is on mobile the shortcuts will be arranged in different ways (default : 10 mobile and 30 pc)
var res = isMobile ? lines.length/100 * 10 : lines.length/100 * 30;
if(lineNumber >= res)
{
//Closes a line and starts a new one
response.write("</tr><tr>")
lineNumber = 0;
}
}
}
//Ends the table
response.write("</table>")
fs.createReadStream('index.html').pipe(response);
}
});
function openProgram(name)
{
console.log(name)
let og = name;
name = decodeURIComponent(name);
if(name == "name=BrowseFile")
{
console.log("File Select")
exec('py scripts\\file_explorer.py');
}
//Saves the new shortcut
if(name.includes("name=Add"))
{
console.log(name);
name = name.replace("name=Add","");
let app = name.replace("name=", "").split("@");
let path = fs.readFileSync('path.txt', 'utf8')
console.log(app)
SaveApps(app[1],path);
}
if(name == "name=Delete")
{
del = true;
}
// Open
if (name != "name=Delete" && del == false && !og.includes("Add") && name != "name=BrowseFile")
{
let app = name.replace("name=", "").split("@");
let path = app[1];
path = path.replaceAll("^","^");
//chld.execSync(path)
exec(path)
}
// Delete the shortcut that you click on by removing it from 'data.txt' and then reloading the page
if(del == true && name != "name=Delete")
{
let app = name.replace("name=", "").split("@");
let path = app[1];
path = path;
const appText = fs.readFileSync("data.txt",'utf8');
const appSplit = appText.split(/\r?\n/);
let removeApp = [];
// The array contains every shortcut from 'data.txt' without the one that the user selected
let newApps = appSplit.filter(item => item !== app[0] + "@" + path);
removeApp = newApps;
newApps.forEach(element => {
console.log(element);
});
fs.writeFile('data.txt','', function() {
console.log('done');
});
// Rewrites the whole data.txt without the deleted shortcut
var stream = fs.createWriteStream("data.txt", {'flags': 'a'});
stream.once('open', function(fd) {
removeApp.forEach(ele => {
stream.write("\r\n" + ele);
});
});
del = false;
}
}
//Adds the new shortcut to the "data.txt" file
function SaveApps(name,path)
{
console.log(name);
var stream = fs.createWriteStream("data.txt", {'flags': 'a'});
stream.once('open', function(fd) {
stream.write("\r\n" + name + "@" + path);
});
}
// chld.exec(".\\hostServer.bat");
// lt --port 3000
const port = 3000
const host = 'localhost'
server.listen(port, host)
console.log(`Listening at http://${host}:${port}`);