-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (102 loc) · 4.37 KB
/
server.js
File metadata and controls
115 lines (102 loc) · 4.37 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
const http = require('http');
const fs = require('fs');
const fsP = require('fs').promises;
const path = require('path');
const formidable = require('formidable');
const {onlineGrayscale} = require("./grayscale/main.js");
let i=1;
http.createServer((request, response)=> {
console.log("request " + request.url);
var filePath = request.url;
if (filePath === '/favicon.ico') {
response.writeHead(200, {'Content-Type': 'image/x-icon'} );
response.end();
return;
}
var extname = String(path.extname(filePath)).toLowerCase();
var mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm'
};
var contentType = mimeTypes[extname] || 'application/octet-stream';
if (filePath === '/') {
filePath = __dirname + '/index.html';
//console.log(filePath);
fsP.readFile(filePath)
.then((content)=>{
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(content, 'utf-8');
})
.catch((err)=>{ console.log(err.code);
});
}else if(extname !== ""){
filePath = __dirname + filePath;
fsP.readFile(filePath)
.then((content)=>{
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end(content, 'utf-8');
})
.catch((err)=>{
if (err.code == 'ENOENT') {
fs.readFile('./404.html', function (error, content) {
response.writeHead(404, { 'Content-Type': 'text/html' });
response.end(content, 'utf-8');
});
}
else {
response.writeHead(500);
response.end('Sorry, check with the site admin for error: ' + err.code + ' ..\n');
}
});
}else{
if(request.url === "/upload" && request.method.toLocaleLowerCase()==="post"){
for(let x = 1; x <= 10; x++){
fsP.unlink(`./grayscale/uploads/${x}.png`)
fsP.unlink(`./grayscale/grayscaled/GS_${x}.png`)
}
const form = formidable({ multiples: true, uploadDir: path.join(__dirname, "grayscale" ,"uploads"), keepExtensions: true});
form.parse(request, (err,fields,files)=>{
fs.readFile('./upload.html', function (error, content) {
response.writeHead(404, { 'Content-Type': 'text/html' });
fsP.readdir(path.join(__dirname,"grayscale", "uploads"))
.then((data)=>{
console.log(data);
data.forEach(elementz => {
elementz =path.join(__dirname,"grayscale", "uploads", elementz);
let finalName = path.join(__dirname,"grayscale", "uploads", `${i}.png`);
console.log(elementz);
console.log("doing it!");
fs.rename(elementz, finalName ,(err)=>{
console.error(err);
})
i++;
});
i = 1;
return data;
})
.then((data)=>setTimeout(() => {
data.forEach((unit)=>{
onlineGrayscale();
});
}, 2000))
.catch((err)=>reject(err))
response.end(content, 'utf-8');
});
});
}
}
}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');