-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.js
302 lines (275 loc) · 7.92 KB
/
database.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
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
const sqlite=require('sqlite3').verbose()
const DEFAULTLIMIT=10
const DEFAULTOFFSET=0
let con=new sqlite.Database('./platform.db',sqlite.OPEN_READWRITE,(err)=>{
if(err){
console.log(`cant connect to sqlite ${err}`)
}
})
function randomString(){
const s ="QWERTYUIOPLKJHGFDSAZXCVBNM1234567890qwertyuioplkjhgfdsazxcvbnm";
let alfabet=[]
for(let i=0;i<s.length;i++){
alfabet.push(s.charAt(i))
}
let ret=''
for(let i=0;i<10;i++){
const char=parseInt(Math.random()*alfabet.length)
ret+=alfabet[char]
}
return ret
}
// this function converts sql result into video object
function readVideo(res){
let v=new video()
v.title=res.title
v.description=res.description
// reason is because of functions like getVideobyViews or getVideobyPopularity i dont need content itself of that videos just hashes,titles,thumbnail and generally basic info of video
try{
v.id=res.id
v.content=res.content
}catch(e){}
v.thumbnail=res.thumbnail
v.likes=res.likes
v.dislikes=res.dislikes
v.tags=res.tags
v.views=res.views
v.time=res.uploaded
v.hash=res.hash
v.length =res.length
return v
}
function createVideoTable(){
const sql=`
create table videos(
id INTEGER PRIMARY KEY AUTOINCREMENT,
title varchar(2000) not null,
description Text ,
content LONGBLOB ,
bytesize int,
thumbnail Text,
likes int default 0,
dislikes int default 0,
tags tinyText,
views int default 0,
length time(0),
hash varchar(60) not null,
uploaded datetime default CURRENT_TIMESTAMP
);`
con.run(sql)
}
class video{
id
title
description
content
thumbnail
likes
dislikes
tags
views
time
hash
length
}
async function addVideo(video){
const sql=`insert into videos (title,description,content,thumbnail,tags,hash,bytesize,length) values ('${video.title}','${video.description}',?,?,'${video.tags}','${randomString()}',${video.content.length},'${video.length}');`
con.all(sql,[video.content,video.thumbnail],(err,res)=>{
if(err){
console.log(err)
console.log(sql)
}
})
}
// for testing purposes
async function getAllVideos(){
const sql=`select * from videos;`
con.all(sql,(err,res)=>{
console.log(res)
})
}
async function getVideobyID(id){
const sql=`select * from videos where id=${id};`
let video;
await new Promise(e=>{
con.all(sql,(err,res)=>{
if(!res[0]){e() ;return }
video=readVideo(res[0])
e()
})
})
return video;
}
function dropAllVideos(){
const sql='drop table videos;'
con.run(sql)
}
async function getVideobyHash(hash){
const sql=`select * from videos where hash='${hash}';`
let video;
await new Promise(e=>{
con.all(sql,(err,res)=>{
if(!res[0]){e() ;return }
video=readVideo(res[0])
e()
})
})
return video;
}
/* it doesnt work as it sould be with blobs */
async function getPartOfVideobyID(id,from,to){
if(from==undefined || from==0){
from=1
}
const sql=`select SUBSTRING(content,${from},${to}) as content from videos where id = ${id} ;`
let buf;
await new Promise(e=>{
con.all(sql,(err,res)=>{
if(!res[0]){e() ;return }
buf=res[0].content
console.log(sql)
e()
})
})
return buf;
}
async function getVideoSizeByID(id) {
const sql=`select bytesize from videos where id = ${id} ;`
let buf;
await new Promise(e=>{
con.all(sql,(err,res)=>{
if(!res[0]){e() ;return }
buf=res[0].bytesize
e()
})
})
return buf;
}
async function getVideoSizeByHash(hash) {
const sql=`select bytesize from videos where hash = '${hash}' ;`
let buf;
await new Promise(e=>{
con.all(sql,(err,res)=>{
if(!res[0]){e() ;return }
buf=res[0].bytesize
console.log(res[0])
e()
})
})
return buf;
}
async function getVideoInfoByID(id){
const sql=`select title,description,thumbnail,likes,dislikes,tags,views,length,id,uploaded from videos where id=${id} ;`
let videos
let def=await new Promise((e,fail)=>{
con.all(sql,(err,res)=>{
if(!res[0]){e() ;return}
videos=res[0]
e()
})
})
return videos;
}
async function getVideosbyViews(limit,offset){
offset==undefined? offset=0:{}
limit==undefined? limit=DEFAULTLIMIT:{}
const sql=`select title,description,thumbnail,likes,dislikes,tags,views,length,id,uploaded from videos order by views desc limit ${limit} offset ${offset} ;`
let videos=[]
let def=await new Promise((e,fail)=>{
con.all(sql,(err,res)=>{
if(!res[0]){e() ;return}
for(let i=0;i<res.length;i++){
videos.push(readVideo(res[i]))
}
e()
})
})
console.log(videos.length)
return videos;
}
async function getVideosbyPopularity(limit,offset){
offset==undefined? offset=0:{}
limit==undefined? limit=DEFAULTLIMIT:{}
const sql=`select title,description,thumbnail,likes,dislikes,tags,views,length,uploaded,id,likes/dislikes as popularity from videos order by popularity desc limit ${limit} offset ${offset} ;`
let videos=[]
await new Promise(e=>{
con.all(sql,(err,res)=>{
if(!res[0]){e() ;return}
for(let i=0;i<res.length;i++){
videos.push(readVideo(res[i]))
}
e()
})
})
return videos;
}
async function getVideosbyUploadDate(limit,offset){
offset==undefined? offset=0:{}
limit==undefined? limit=DEFAULTLIMIT:{}
const sql=`select title,description,thumbnail,likes,dislikes,tags,views,length,uploaded,id from videos order by uploaded desc limit ${limit} offset ${offset} ;`
let videos=[]
await new Promise(e=>{
con.all(sql,(err,res)=>{
if(!res[0]){e() ;return}
for(let i=0;i<res.length;i++){
videos.push(readVideo(res[i]))
}
e()
})
})
return videos;
}
async function getRelatedVideos(id,limit,offset){
limit==undefined? limit=DEFAULTLIMIT:{}
offset==undefined? offset=DEFAULTOFFSET:{}
if(!id){return}
const main=await getVideoInfoByID(id)
let sql=`select title,description,thumbnail,likes,dislikes,tags,views,length,uploaded,id from videos where title LIKE '%${main.title}%' union `
sql+=`select title,description,thumbnail,likes,dislikes,tags,views,length,uploaded,id from videos where description LIKE '%${main.description}%' union `
sql+=`select title,description,thumbnail,likes,dislikes,tags,views,length,uploaded,id from videos where tags LIKE '%${main.tag}%' limit ${limit} offset ${offset};`
let videos=[]
await new Promise(e=>{
con.all(sql,(err,res)=>{
if(err){console.log(sql+'\n'+err);}
if(!res[0]){e() ;return}
for(let i=0;i<res.length;i++){
videos.push(readVideo(res[i]))
}
e()
})
})
//console.log('related videos :: '+JSON.stringify(videos))
return videos;
}
function addView(id){
const sql=`update videos set views=views+1 where id=${id}`
con.exec(sql)
}
function addLike(id){
const sql=`update videos set likes=likes+1 where id=${id}`
con.exec(sql)
}
function addDislike(id){
const sql=`update videos set dislikes=dislikes+1 where id=${id}`
con.exec(sql)
}
module.exports={
addView,
addLike,
addDislike,
createVideoTable,
addVideo,
video,
getAllVideos,
getVideobyID,
getVideobyHash,
dropAllVideos,
getPartOfVideobyID,
getVideoSizeByID,
getVideosbyViews,
getVideosbyPopularity,
getVideosbyUploadDate,
getVideoSizeByHash,
getRelatedVideos,
getVideoInfoByID
}