-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (110 loc) · 3.64 KB
/
index.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
// import library
const fs = require('fs');
// Initialize
const app = require('express')();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const sqlite3 = require('sqlite3').verbose();
// video route
var video = require('./routes/video');
var import_media = require('./routes/import_media')();
var db;
// setting global root file
global.__basedir = __dirname;
app.use('/index_style.css', function(req, res){
res.sendFile(__basedir + '/views/index_style.css');
});
app.use('/script.js', function(req, res){
res.sendFile(__basedir + '/views/script.js');
});
// Make io accessible to our router
app.use(function(req,res,next){
req.db = db;
req.io = io;
next();
});
app.use('/video', video);
app.use('/import_media', import_media);
app.get('/', function(req, res){
res.sendFile(__basedir + '/views/index.html');
});
// socket communication
io.on('connection', function(socket){
console.log('A user connected ', socket.id);
let sql = "SELECT id, title FROM movies";
db.all(sql, [], (err, rows) => {
if (err) {
throw err;
}
io.to(socket.id).emit("query_res", rows);
});
socket.on('load_id', function(load_data){
var path = `/video/${load_data.media_type}/${load_data.id}`;
io.to(socket.id).emit("play", path);
});
socket.on('load_seasons', function(series_id){
//GOHERE1
let sql = `SELECT id, title FROM series_seasons where series_id=${series_id}`;
db.all(sql, [], (err, rows) => {
if (err) {
throw err;
}
io.to(socket.id).emit("seasons_query_res", rows);
});
});
socket.on('load_episodes', function(series_seasons_id){
//GOHERE1
let sql = `SELECT id, title FROM series_episodes where series_seasons_id=${series_seasons_id}`;
db.all(sql, [], (err, rows) => {
if (err) {
throw err;
}
io.to(socket.id).emit("episodes_query_res", rows);
});
});
socket.on('edit_id', function(media_id){
let sql = `SELECT id, title FROM movies WHERE id = ${media_id}`;
db.get(sql, [], (err, rows) => {
if(err){
throw err;
}
io.to(socket.id).emit("edit_query_res", rows);
});
});
socket.on('edit_query_req', function(media_meta_edit){
let sql = `UPDATE ${media_meta_edit.db} SET title = "${media_meta_edit.title}" WHERE id = ${media_meta_edit.id}`;
db.run(sql, [], (err) => {
if(err){
io.to(socket.id).emit("req_err", "Update row failed");
throw err;
}
});
});
socket.on('query', function(query){
console.log(query.media_type);
var media_type = query.media_type;
var search_text = query.search_text;
let sql = `SELECT id, title FROM ${media_type} where title LIKE '%${search_text}%'`;
db.all(sql, [], (err, rows) => {
if (err) {
throw err;
}
var query_res = {"media_type": media_type, "rows": rows}
console.log(query_res)
io.to(socket.id).emit("query_res", query_res);
});
});
// User disconnected
socket.on('disconnect', function(){
console.log('A user disconnected ', socket.id);
});
});
http.listen(2030, function(){
console.log("listening on *:2030");
db = new sqlite3.Database('./database/media.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the media database.');
});
});