-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
139 lines (110 loc) · 3.46 KB
/
main.js
File metadata and controls
139 lines (110 loc) · 3.46 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
// Pins4Peace
var fs = require("fs");
var express = require('express');
var app = express();
var http = require('http').createServer(function (req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
});
// var http = require('http').createServer(app);
const privateKey = fs.readFileSync('/etc/letsencrypt/live/pins4peace.space/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/pins4peace.space/cert.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/pins4peace.space/chain.pem', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
var https = require('https').createServer(credentials,app);
var io = require('socket.io')(https);
const path = require('path');
const axios = require('axios')
var md5 = require("blueimp-md5");
var data = require("./data/logs.json");
var fs = require('fs');
const request = require('request');
var FormData = require('form-data');
var multer = require('multer');
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null,'./videos/');
},
filename: function(req, file, cb) {
cb(null, data["logs"].length + path.extname(file.originalname));
}
});
var upload = multer({storage: storage})
io.on('connection',(socket) => {
console.log('a user connected');
socket.on('disconnect',() => {
console.log('user disconnected');
});
socket.on('reportVideo',(id) => {
data["logs"][id]["ban"]++;
updateJSON();
});
socket.on('addReaction',(id,reaction) => {
data["logs"][id]["reactions"][reaction]++;
updateJSON();
});
});
app.use("/videos", express.static(__dirname + '/videos'));
app.use("/data", express.static(__dirname + '/data'));
app.use("/Photos", express.static(__dirname + '/UI/Photos'));
app.use("/css", express.static(__dirname + '/UI/css'));
app.get('/',(req,res) => {
res.sendFile(__dirname + '/Client/map.html');
});
app.post('/addStory', upload.single("video"), function (req, res, next) {
addStory(req.body.Address,req.body.Name,req.body.Title,path.extname(req.file.originalname));
return res.redirect('/');
});
http.listen(80,() => {
console.log('listening on *:80');
});
https.listen(443,() => {
console.log('listening on *:443');
});
// File system
function writeFile(location,name,value) {
fs.writeFile(location + '/' + name,value,function writeJSON(err) {
if(err) {
return console.log(err);
}
});
}
function writeJSON(location,name,jsonOBJ) {
writeFile(location,name,JSON.stringify(jsonOBJ));
}
//========================================================
function updateJSON() {
writeJSON("./data/","logs.json",data);
}
// Each story has:
// ID
// Address - (Longtitude/Latitude)
// Name of person
// Title
// Ban Counter
function addStory(address, name, title, ext) {
request({url: "https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyADkEWk0rw92U2RLe3_8z0ejK1MQ-mUs9w&language=en&address=" + address},
(error,res,body) => {
if(error) {
console.log("Error with google map: " + error);
return;
}else{
var jsonObj = JSON.parse(body);
data["logs"].push({
"id": data["logs"].length,
"lat": jsonObj.results[0].geometry.location.lat,
"lng": jsonObj.results[0].geometry.location.lng,
"name": name,
"title": title,
"ext": ext,
"ban": 0,
"reactions":[0,0,0,0,0]
});
updateJSON();
}
});
}