forked from Ciambellaro/wmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlienserver.js
160 lines (134 loc) · 4.26 KB
/
lienserver.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
// Dependencies
const Youtube = require("youtube-api"),
fs = require('fs'),
readJson = require("r-json"),
Lien = require("lien"),
Logger = require("bug-killer"),
opn = require("opn"),
prettyBytes = require("pretty-bytes");
var express = require('express');
var app = express();
var multer = require('multer');
var storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, __dirname + "/public/uploads");
},
filename: function (req, file, cb) {
console.log(file);
cb(null , "video.mp4");
}
});
const bodyParser = require('body-parser');
var url = require('url');
var oauth;
var auth;
var access = 0;
// Init lien app
var app = new Lien({
host: "localhost",
port: 8443,
public: __dirname + "/public",
ssl: {
key: "privkey.pem",
cert: "pubcert.pem"
}
});
// I downloaded the file from OAuth2 -> Download JSON
const CREDENTIALS = readJson(`${__dirname}/credentials.json`);
// Listen for load
app.on("load", function(err) {
console.log(err || "server started on port 8443.");
err && process.exit(1);
});
// Add page
//app.get("/", `${__dirname}/login.html`);
app.get('/', lien => {
access = 0;
lien.file(`${__dirname}/login.html`);
});
//app.get("/map", `${__dirname}/index.html`);
app.get('/map', lien => {
lien.file(`${__dirname}/index.html`);
});
var titolo = "";
var coordinate = "";
var scopo = "";
var categoria = "";
var audience = "";
var dettaglio = "";
var lingua = "";
app.post('/auth', lien => {
var upload = multer({
storage: storage
}).single('userVideo');
upload(lien.req, lien.res, function(err) {
//lien.res.end('File is uploaded')
console.log("File is uploaded");
titolo = lien.req.body.titoloClip;
coordinate = lien.req.body.coordin;
scopo = lien.req.body.optionsRadios;
lingua = lien.req.body.lan;
categoria = lien.req.body.cat;
dettaglio = lien.req.body.det;
audience = lien.req.body.aud;
video = lien.req.body.userVideo;
console.log("*******************" + titolo + " " + coordinate + " " + scopo + " " + lingua + " " + categoria + " " + dettaglio + " " + audience + " " + video);
});
access += 1;
oauth = Youtube.authenticate({
type: "oauth",
client_id: CREDENTIALS.web.client_id,
client_secret: CREDENTIALS.web.client_secret,
redirect_url: CREDENTIALS.web.redirect_uris[0]
});
auth = oauth.generateAuthUrl({
access_type: "offline",
scope: ["https://www.googleapis.com/auth/youtube.upload"]
});
console.log(auth);
console.log(oauth);
opn(auth);
lien.redirect("/map");
});
// Handle oauth2 callback
app.get("/oauth2callback", lien => {
Logger.log("Trying to get the token using the following code: " + lien.query.code);
oauth.getToken(lien.query.code, (err, tokens) => {
if (err) {
lien.lien(err, 400);
return Logger.log(err);
}
Logger.log("Got the tokens.");
oauth.setCredentials(tokens);
lien.end("The video is being uploaded. Check out the logs in the terminal.");
var req = Youtube.videos.insert({
resource: {
// Video title and description
snippet: {
title: titolo,
description: coordinate + ":" + scopo + ":" + lingua + ":" + categoria + ":" + dettaglio + ":" + audience
}
// I don't want to spam my subscribers
,
status: {
privacyStatus: "public"
}
}
// This is for the callback function
,
part: "snippet,status"
// Create the readable stream to upload the video
,
media: {
body: fs.createReadStream( __dirname + "/public/uploads/video.mp4")
}
}, (err, data) => {
console.log("Done.");
//process.exit();
});
});
});
// Listen for server errors
app.on("serverError", err => {
console.log(err.stack);
});