-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
117 lines (97 loc) · 3.15 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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
// functions to communicate with drive.js file which communicates with Google Drive API
const drive = require('./drive/drive');
const fs = require('fs');
const app = express();
app.set('view engine', 'ejs');
app.set('views', `${__dirname}/public`);
// parse request body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// expose /public folder
app.use(express.static(path.join(__dirname, 'public')));
// serve index.html for the user
app.get('/', (req, res) => {
if (req.query.defaultLabels) {
req.query.defaultLabels = JSON.parse(req.query.defaultLabels);
}
res.render('index', {defaultData: `'${JSON.stringify(req.query)}'`});
});
// serve generator.html for the user
app.get('/generator', (req, res) => {
res.sendFile(path.join(__dirname, 'public/generator.html'));
});
// list all videos inside Google Drive folder with provided id
app.get('/list', (req, res) => {
const folderId = req.query.folderId;
const onDriveReponse = (folderData, error = false, errorMsg) => {
if (error) {
res.json({
error: true,
errorMsg: errorMsg
});
} else { // success
res.json({
error: false,
folderData
});
}
}
drive.list(onDriveReponse, folderId);
});
app.post('/video', (req, res) => {
const folderId = req.body.folderId;
const videoIds = req.body.videoIds;
const jsonId = req.body.jsonId;
const videoNames = req.body.videoNames;
const onDownloadFinished = (videoIds, videoNames, jsonId, error = false, errorMsg) => {
if (error) {
res.json({
error: true,
errorMsg: errorMsg
});
} else { // success
res.json({
error: false,
videoIds,
videoNames,
jsonId
});
}
}
drive.download(onDownloadFinished, folderId, videoIds, videoNames, jsonId);
});
app.get('/videoStatus', (req, res) => {
const filenames = JSON.parse(decodeURIComponent(req.query.filenames));
const filepaths = filenames.map(filename => path.join(__dirname + `/public/tmp/${filename}`));
if (filepaths.every(file => fs.existsSync(file))) {
res.json({
all: true
});
}
else {
res.json({
all: false
});
}
});
app.get('/videoFile', (req, res) => {
const filepath = path.join(__dirname + `/public/tmp/${req.query.fileId}.${req.query.ext}`);
res.sendFile(filepath);
});
app.get('/jsonFile', (req, res) => {
res.sendFile(path.join(__dirname + `/public/tmp/${req.query.fileId}.json`));
});
// user clicked on submit button, upload JSON file to server
app.post('/submit', (req, res) => {
const data = JSON.parse(req.body.data);
const onSuccess = () => { res.sendStatus(200); }
drive.upload(onSuccess, data, req.body.folderId);
});
app.listen(process.env.PORT || 3000, () =>
console.log(`Server listening on port ${process.env.PORT || 3000}!`)
);