-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
148 lines (121 loc) · 4.19 KB
/
server.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
'use strict';
const server = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs');
const port = 4200;
function serverHandler(request, response) {
const uri = url.parse(request.url).pathname;
let filename = path.join(process.cwd(), uri);
const isWin = !!process.platform.match(/^win/);
console.log('request');
if (filename && filename.toString().indexOf(isWin ? '\\uploadFile' : '/uploadFile') != -1 && request.method.toLowerCase() == 'post') {
console.log('uploadRequest');
uploadFile(request, response);
return;
}
fs.exists(filename, function(exists) {
if (!exists) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write('404 Not Found: ' + filename + '\n');
response.end();
return;
}
if (filename.indexOf('favicon.ico') !== -1) {
return;
}
if (fs.statSync(filename).isDirectory() && !isWin) {
filename += '/index.html';
} else if (fs.statSync(filename).isDirectory() && !!isWin) {
filename += '\\index.html';
}
fs.readFile(filename, 'binary', function(err, file) {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.write(err + '\n');
response.end();
return;
}
let contentType;
if (filename.indexOf('.html') !== -1) {
contentType = 'text/html';
}
if (filename.indexOf('.js') !== -1) {
contentType = 'application/javascript';
}
if (contentType) {
response.writeHead(200, {
'Content-Type': contentType
});
} else response.writeHead(200);
response.write(file, 'binary');
response.end();
});
});
}
let app;
app = server.createServer(serverHandler);
app = app.listen(port, process.env.IP || "0.0.0.0", function() {
const addr = app.address();
if (addr.address == '0.0.0.0') {
addr.address = 'localhost';
}
app.address = addr.address;
console.log("Server listening at", 'http://' + addr.address + ":" + addr.port);
});
function uploadFile(request, response) {
// parse a file upload
const mime = require('mime');
const formidable = require('formidable');
const util = require('util');
const cloudinary = require('cloudinary');
const cloudinaryConfig = require('./src/cloudinaryConfig');
cloudinary.config(cloudinaryConfig);
console.log(cloudinaryConfig);
const form = new formidable.IncomingForm();
form.keepExtensions = true;
form.maxFieldsSize = 10 * 1024 * 1024;
form.maxFields = 1000;
form.multiples = false;
form.parse(request, function(err, fields, files) {
cloudinary.v2.uploader.upload(files.file.path,
{ resource_type: "video", format: "webm", effect: "reverse" },
function(err, result) {
if (err) {
console.log(err);
response.writeHead(400);
response.write(JSON.stringify(err));
response.end();
return;
}
let url = result.url;
response.writeHead(200, getHeaders('Content-Type', 'application/json'));
response.write(JSON.stringify({
fileURL: url
}));
response.end();
}
);
});
}
function getHeaders(opt, val) {
try {
const headers = {};
headers["Access-Control-Allow-Origin"] = "http://redroom.online";
headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS";
headers["Access-Control-Allow-Credentials"] = true;
headers["Access-Control-Max-Age"] = '86400'; // 24 hours
headers["Access-Control-Allow-Headers"] = "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept";
if (opt) {
headers[opt] = val;
}
return headers;
} catch (err) {
console.log(err);
return {};
}
}