-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·317 lines (262 loc) · 9.08 KB
/
server.js
File metadata and controls
executable file
·317 lines (262 loc) · 9.08 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
const express = require("express");
const app = express();
var redirect = require('express-http-to-https').redirectToHTTPS
const port = 8000;
const myModule = require('./mongo2.js');
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');
var ytdl = require('ytdl-core');
const {Readable} = require('stream');
ffmpeg.setFfmpegPath(ffmpegPath);
app.use(redirect([/localhost:8000/]));
app.use(express.urlencoded({extended : true}));
app.use(express.json({limit: '16mb'}));
app.use('/', express.static(__dirname + '/client'));
app.use((req,res,next) => {
res.setHeader("Acces-Control-Allow-Origin", '*');
res.setHeader("Acces-Control-Allow-Headers", "Origin, X-Request-With, Content-Type, Accept");
res.setHeader("Acces-Control-Allow-Methods", "GET, POST");
next();
});
app.get("/categorie", function (req, res) { //richiesta del json con le categorie
//console.log("requested: categorie.json");
res.sendFile(__dirname+"/client/categorie.json");
});
// aggiunta di un nuovo luogo collezione place
app.post("/new_place", async (req, res) => {
try {
let doc = await myModule.add_one(req);
res.send(JSON.stringify(doc));
} catch (err) { //catch the error if the database isn't connected
res.send(err);
}
});
//elimina un posto nella collezione place
//bisogna passare OLC e token dell'utente che lo ha creato
app.post("/del_place", async (req, res) => {
try {
let doc = await myModule.del_one(req);
res.send(JSON.stringify(doc));
} catch (err) { //catch the error if the database isn't connected
res.send(err);
}
});
//trova informazioni su posto collezione place
//se voglio ricevere tutti gli oggetti, mi basta passare ad esempio category:"", una stringa vuota
app.post("/find_place", async (req, res) => {
try{
let doc = await myModule.find_place(req);
res.send(JSON.stringify(doc));
}
catch(err){
res.send(err);
}
});
//mostra DB collezione place
app.get("/list_place", async (req, res) => {
try {
let ret = await myModule.showdb_place();
var resend = JSON.stringify(ret);
res.send(resend);
}
catch (err){
res.send("errore nella stampa della lista place")
}
});
//drop totale collezione place
app.get("/drop_place", async (req, res) => {
try {
let ret = await myModule.clear_place();
res.send(JSON.stringify(ret));
}
catch (err){
res.send(JSON.stringify(err));
}
});
//creazione nuova recensione collezione review
app.post("/new_review", async (req, res) =>{
try {
let doc = await myModule.add_review(req);
res.send(doc);
} catch (err) { //catch the error if the database isn't connected
res.send(err);
}
})
//trova informazioni su recensioni dato luogo collezione review
app.post("/find_review", async (req, res) => {
try{
let doc = await myModule.find_review(req);
res.send(JSON.stringify(doc));
}
catch(err){
res.send(err);
}
});
//mostra DB collezione review
app.get("/list_review", async (req, res) => {
try {
let ret = await myModule.showdb_review();
res.send(JSON.stringify(ret));
}
catch (err){
res.send("errore nella stampa lista review")
}
});
//drop totale collezione review
app.get("/drop_review", async (req, res) => {
try {
let ret = await myModule.clear_review();
res.send(JSON.stringify(ret));
}
catch (err){
res.send(err);
}
});
// aggiunta di un nuovo percorso alla collezione route
//bisogna passare array percorso e token user che lo sta creando
//{"route":["olc1","olc2","olc3","olc4"], "namer":"nome posto"}
//
//VIETATO PASSARE OLC, VA PASSATO ARRAY DEI LUOGHI RICERCATI
/*
Con questa funzione possiamo sia creare che modificare i percorsi.
Se non e' presente un percorso (passandogli array luoghi e user oppure namer e user) viene aggiunto.
se invece è presente vengono modificati i valori dell'array del percorso oppure il nome del percorso.
*/
app.post("/new_route", async (req, res) => {
try {
let doc = await myModule.add_route(req);
res.send(JSON.stringify(doc));
} catch (err) { //catch the error if the database isn't connected
res.send(err);
}
});
// aggiunta di un nuovo percorso alla collezione route
//va passato l'array intero dei posti del percorso che si vuole eliminare e token user che lo ha creato
//{route:["olc1","olc2","olc3","olc4"]}
//VIETATO PASSARE OLC, VA PASSATO ARRAY DEI LUOGHI RICERCATI o NAMER
/*
L'eliminazione va sempre effettuata passando il token dell'utente, solo l'owner puo' eliminare il suo percorso.
In piu' passiamo o il campo route(passandogli l'array con l'esatto percorso), oppure il nome del percorso che gli abbiamo assegnato.
*/
app.post("/del_route", async (req, res) => {
try {
let doc = await myModule.del_route(req);
res.send(JSON.stringify(doc));
} catch (err) { //catch the error if the database isn't connected
res.send(err);
}
});
//trova percorsi predefiniti dato luogo collezione routes
//passare: OLC:"OLC ricercato"
//{"OLC":"FHOLC888"}
/*
La richiesta viene effettuata con:
1) OLC se si ha bisogno di tutti i percorsi in partenza da quel luogo.
2) token dell'utente se si ha bisogno di tutti i percorsi creati dall'utente.
3) namer se abbiamo bisogno di ricercare un percorso in particolare e sappiamo il nome assegnatogli dall'utente.
Passare solo uno dei 3 ricercati non 2 insieme alla volta, va fatta una richiesta alla volta per ognuno
se servono piu' informazioni.
*/
app.post("/find_route", async (req, res) => {
try{
let doc = await myModule.find_route(req);
res.send(JSON.stringify(doc));
}
catch(err){
res.send(err);
}
});
// aggiunta di un nuovo utente alla collezione preferences
// se l'utente esiste gia' vengono aggiornati i valori delle preferenze
app.post("/add_preference", async (req, res) => {
try {
let doc = await myModule.add_pref(req);
res.send(JSON.stringify(doc));
} catch (err) { //catch the error if the database isn't connected
res.send(err);
}
});
// trova preferenze utente nella collezione preferences
// passare il token utente: token: "*********"
app.post("/find_preference", async (req, res) => {
try {
let doc = await myModule.find_pref(req);
res.send(JSON.stringify(doc));
} catch (err) { //catch the error if the database isn't connected
res.send(err);
}
});
// Genera un video dato un'audio passato come parametro, utilizzando un immagine di sfondo unica per tutti i video,
// viene ritornata al chiamante
app.post('/audio_to_video', async (req,res)=>{
var buffer = Buffer.from(req.body.chunks, 'base64');
var readable = new Readable();
readable._read = () => {}
readable.push(buffer);
readable.push(null)
var command = ffmpeg('/webapp/youtube.jpg')
//var command = ffmpeg('youtube.jpg')
.fps(1)
.size('1920x1080')
.addInput(readable)
.format('webm');
var ffstream = await command.pipe();
var chunks = [];
ffstream.on('data', function(chunk) {
chunks.push(chunk);
});
ffstream.on('end', function() {
var result = Buffer.concat(chunks);
res.send(result.toString('base64'));
});
});
//Permette di apportare delle modifiche all'audio passato come paramentro e successivamente ritorna l'audio modificato
//al chiamante
app.post('/modify_video', async (req,res)=>{
var buffer = Buffer.from(req.body.chunks, 'base64');
var readable = new Readable();
readable._read = () => {}
readable.push(buffer);
readable.push(null)
var command = ffmpeg(readable)
.format('webm')
.setStartTime(req.body.start)
.setDuration(req.body.end-req.body.start)
.audioFilters('volume=' + (req.body.volume/10))
.on('end', function() {
console.log('file has been modified succesfully');
})
.on('error', function(err) {
console.log('an error happened: ' + err.message);
});
var ffstream = command.pipe();
var chunks = [];
ffstream.on('data', function(chunk) {
chunks.push(chunk);
});
ffstream.on('end', function() {
var result = Buffer.concat(chunks);
res.send(result.toString('base64'));
});
});
//permette di scaricare un video da youtube (in questo caso solo audio poichè viene applicato il filtro 'audioonly')
//inoltre tramite l'ausilio di fluent-ffmpeg questo viene convertito in formato webm
app.post('/audio_from_yt', async (req,res)=>{
var id = req.body.id;
res.setHeader('Content-disposition', 'attachment; filename=test.pdf');
res.set('Content-Type', 'application/json');
var yta = ytdl('https://www.youtube.com/watch?v='+id,{filter:"audioonly"});
var command = ffmpeg()
.input(yta)
.format('webm'); //da provare come weba
var ffstream = command.pipe();
var chunks = [];
ffstream.on('data', function(chunk) {
chunks.push(chunk);
});
ffstream.on('end', function() {
var result = Buffer.concat(chunks);
res.send(result.toString('base64'));
});
});
app.listen(port, () => console.log("Server started on port: " + port));