-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
192 lines (179 loc) · 5.26 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
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
const express = require("express");
const app = express();
const fs = require("fs");
const mysql = require("mysql");
const stream = require("./index.ts");
const path = require("path");
const dotenv = require("dotenv");
dotenv.config({path: path.join(__dirname, './.env')});
const pool = mysql.createPool({
host : process.env.DB_HOST, //아이피
user : process.env.DB_USER, //계정이름
password : process.env.DB_PASSWORD, //비밀번호
port : process.env.DB_PORT, //포트
database : process.env.DB_DATABASE, //
connectionLimit : process.env.DB_CONNECTIONLIMIT //DB 이름
});
pool.getConnection((err, con) => {
con.release();
if(err){
throw err;
} else {
console.log('MySQL 연결 성공!');
}
});
selectQuery = async (sql, pool) => {
return new Promise((resolve, reject) => {
pool.getConnection((err, con) => {
if(err){
con.release();
reject(err);
}
con.query(sql, (err, rows) => {
con.release();
if(err) {
reject(err);
}
else {
resolve(rows);
}
});
});
}).catch(err => {throw err});
}
transactionQuery = async (sql, pool) => {
return new Promise((resolve, reject) => {
pool.getConnection((err, con) => {
if(err){
con.release();
reject(err);
}
con.beginTransaction();
con.query(sql, (err, rows) => {
if(err) {
con.rollback();
con.release();
reject(err);
}
else {
con.commit();
con.release();
resolve(rows);
}
});
});
}).catch(err => {throw err});
}
app.get("/", async function (req, res) {
lists = '';
rows = await selectQuery('select * from video', pool);
for (var i = 0; i < Object.keys(rows).length; i++) {
lists += `<p>
<a href = "video/${encodeURIComponent(rows[i].url)}">${rows[i].title}</a>
생성시간 : ${rows[i].DATE}
<p>`;
};
res.send(`
<!DOCTYPE html>
<html lang="ko-KR">
<body>
<h1>메인 화면</h1>
${lists}
</body>
<a href = "upload">동영상 업로드</a>
</html>
`)
});
app.get("/upload", function (req, res) {
res.send(`
<!DOCTYPE html>
<html lang="ko-KR">
<body>
<h1>제목 및 스트리밍 url 입력하시오</h1>
</body>
<form action='/insert' method = 'get'>
제목 : <input name='upload_title' type='text'/><br>
스트리밍 url : <input name='upload_url' type='text'/><br>
<input type='submit' value='업로드'>
</form>
</html>
`)
});
app.get("/insert", async function (req, res) {
res.send(`
<!DOCTYPE html>
<html lang="ko-KR">
업로드 중, 시간이 다소 걸릴 수 있습니다.
</html>
`)
url = await stream.main(req.query.upload_url);
console.log('업로드 완료!!');
console.log(url);
transactionQuery(`
INSERT INTO
video
VALUES
(
'${req.query.upload_title}',
'${req.query.url}',
DEFAULT
)
`, pool);
});
app.get("/video/:url", function (req, res) {
res.send(`
<!DOCTYPE html>
<html lang="ko-KR">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>제목</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//amp.azure.net/libs/amp/latest/skins/amp-default/azuremediaplayer.min.css" rel="stylesheet">
<script src="//amp.azure.net/libs/amp/latest/azuremediaplayer.min.js"></script>
</head>
<body>
<video id="azuremediaplayer" class="azuremediaplayer amp-default-skin amp-big-play-centered" tabindex="0"> </video>
</div>
<script>
var myOptions = {
autoplay: true,
controls: true,
width: "640",
height: "400",
poster: "",
playbackSpeed: {
enabled: true,
initialSpeed: 1.0,
speedLevels: [
{ name: "x4.0", value: 4.0 },
{ name: "x3.0", value: 3.0 },
{ name: "x2.0", value: 2.0 },
{ name: "x1.75", value: 1.75 },
{ name: "x1.5", value: 1.5 },
{ name: "x1.25", value: 1.25 },
{ name: "normal", value: 1.0 },
{ name: "x0.75", value: 0.75 },
{ name: "x0.5", value: 0.5 },
]
}
};
var myPlayer = amp("azuremediaplayer", myOptions);
myPlayer.src(
[
{ src: "${decodeURIComponent(req.params.url)}", type: "application/vnd.ms-sstr+xml" },
],
[
{ src: "", srclang: "en", kind: "subtitles", label: "English" },
]
);
</script>
</body>
</html>
`
)
});
app.listen(8080, function () {
console.log("Listening on port 8080!");
});