-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
281 lines (254 loc) · 8.56 KB
/
app.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
/**
* Module dependencies.
*/
// var rimraf = require("rimraf");
var express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
http = require('http'),
path = require('path'),
cleanup = require('./cleanup');
//var methodOverride = require('method-override');
var session = require('express-session');
var app = express();
var mysql = require('mysql');
var bodyParser = require("body-parser");
var connection = mysql.createConnection({
// host: '192.168.1.119',
host:'localhost',
user: 'comp5322',
password: 'comp5322project',
database: 'comp5322'
});
connection.connect();
global.db = connection;
//CK @ 2018-03-31, integrate with Shawn's code start
// First Integration Part
const url = require('url')
const fs = require('fs')
const util = require('util')
const events = require('events')
const template = require('art-template')
const formidable = require('formidable')
const socket_io = require('socket.io')
const fluent_ffmpeg = require('fluent-ffmpeg')
function display_upload_file_html(request, response) {
return new Promise((resolve, reject) => {
let file_path = __dirname + '/views/upload_file.html'
response.writeHead(200, {
'Content-type': 'text/html'
})
response.end(template(file_path, {
timestamp:new Date().getTime()
}))
resolve()
})
}
global.display_upload_file_html = display_upload_file_html;
function display_my_video_html(request, response) {
return new Promise((resolve, reject) => {
let file_path = __dirname + '/views/my_video.html'
response.writeHead(200, {
'Content-type': 'text/html'
})
response.end(template(file_path, {
title: 'My Meme',
videoId: request.params.videoId
}))
resolve()
})
}
global.display_my_video_html = display_my_video_html;
function display_static_resoures(request, response) {
return new Promise((resolve, reject) => {
let pathname = url.parse(request.url, true).pathname
let file_path = __dirname + pathname
fs.readFile(file_path, (err, file_content) => {
if (err) {
reject(err)
return
}
response.writeHead(200)
response.end(file_content)
resolve()
})
})
}
global.display_static_resoures = display_static_resoures;
function upload_process(request, response) {
return new Promise((resolve, reject) => {
if (request.method.toLowerCase() !== 'post') {
return
}
let form = new formidable.IncomingForm()
form.uploadDir = __dirname + '/static/uploaded'
form.maxFileSize = 50 * 1024 * 1024
form.keepExtensions = true
form.parse(request, function(err, fields, files) {
if (err) {
reject(err)
} else {
let single_file = files.file
response.writeHead(200,{
'Content-type':'text/plain'
})
response.end('success')
resolve(single_file)
}
})
})
}
global.upload_process = upload_process;
function insert_vid(uploaded_file, request, response) {
return new Promise((resolve, reject) => {
let file_path = uploaded_file.path
let former_name = uploaded_file.name
let userId = request.session.userId
if (request.method.toLowerCase() !== 'post') {
console.log('return');
return
}
var basename = path.basename(file_path);
var filename = basename.split('.')[0];
let sql = 'INSERT INTO videos (\`name\`, \`userid\`,\`former_name\`) VALUES (?,?,?);'
db.query(sql, [filename, userId, mysql.escape(former_name)], function(err, results) {
if (err) {
reject(err)
} else {
resolve(results.affectedRows)
}
});
})
}
global.insert_vid = insert_vid;
function ffmpeg_thumbnail(uploaded_file){
let file_path = uploaded_file.path
let filename = path.parse(file_path).name
let ffmpeg_instance = fluent_ffmpeg(file_path)
return new Promise((resolve, reject) => {
ffmpeg_instance
.thumbnail({
timestamps: ['50%'],
filename: filename + '.png',
folder: 'static/video/',
size: '320x240'
})
.on('error', (err, stdout, stderr) => {
reject(err)
})
.on('end', () => {
resolve()
})
})
}
function ffmpeg_segmentation(uploaded_file) {
let file_path = uploaded_file.path
let filename = path.parse(file_path).name
let ffmpeg_instance = fluent_ffmpeg(file_path)
return new Promise((resolve, reject) => {
ffmpeg_instance.addOptions([
'-profile:v baseline', // baseline profile (level 3.0) for H264 video codec
'-level 3.0',
'-start_number 0', // start the first .ts segment at index 0
'-hls_time 10', // 10 second segment duration
'-hls_list_size 0', // Maxmimum number of playlist entries (0 means all entries/infinite)
'-f hls' // HLS format
])
.output('static/video/' + filename + '.m3u8')
.on('progress', progress => {
eventEmit.emit('segmentation_process', progress.percent)
})
.on('error', (err, stdout, stderr) => {
reject(err)
})
.on('end', () => {
eventEmit.emit('segmentation_process', 100)
resolve()
})
.run()
})
}
global.ffmpeg_segmentation = ffmpeg_segmentation;
//Integrate with Shawn's code end
// all environments
app.set('port', process.env.PORT || 8081);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 3600000
}
}))
app.get('/', routes.index); //call for main index page
app.get('/signup', user.signup); //call for signup page
app.post('/signup', user.signup); //call for signup post
app.get('/login', routes.index); //call for login page
app.post('/login', user.login); //call for login post
app.get('/home/dashboard', user.dashboard); //call for dashboard page after login
app.get('/home/logout', user.logout); //call for logout
app.get('/home/profile', user.profile); //to render users profile
app.get('/home/upload_video', user.uploadvideo); //to render upload_file.html
app.get('/home/vid_listing', user.vid_listing);
app.get('/videos/:videoId', user.myvideo); //to render my_video.html
app.get('/static/*', display_static_resoures); //static resources
app.post('/upload', async function(request, response) {
try{
let uploaded_file = await upload_process(request, response)
await insert_vid(uploaded_file, request, response)
await ffmpeg_thumbnail(uploaded_file)
await ffmpeg_segmentation(uploaded_file)
}catch(err){
console.error('Upload function point occurs an exception:',err)
}
});
//Middleware
let eventEmit = new events.EventEmitter()
let server = http.createServer(app)
let io = socket_io.listen(server)
io.sockets.on('connection', function (socket) {
eventEmit.on('segmentation_process',(data)=>{
socket.emit('push_from_server', data)
})
})
// process.stdin.resume();//so the program will not close instantly
function exitHandler(options, err) {
if (options.cleanup) {
console.log('Cleaning up...');
cleanup([path.normalize(__dirname + './static/uploaded/*.mp4'),
path.normalize(__dirname + './static/video/*.ts'),
path.normalize(__dirname + './static/video/*.m3u8')
]);
}
if (err) console.log(err.stack);
if (options.exit) process.exit();
}
//do something when app is closing
process.on('exit', exitHandler.bind(null, {
cleanup: true
}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {
exit: true
}));
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, {
exit: true
}));
process.on('SIGUSR2', exitHandler.bind(null, {
exit: true
}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {
exit: true
}));
server.listen(8081, () => {
console.log('http://localhost:8081')
})