-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (93 loc) · 3.39 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
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
const fs = require('fs');
// I separated out all the messy google api stuff
var gapi = require('./apimanager');
// file uploading
const fileUpload = require('express-fileupload');
// default options
app.use(fileUpload());
// tell our app where to serve our static files (root dir)
app.use(express.static('public'));
// define a route - what happens when people visit /
app.get('/', function(req, res) {
res.sendFile(__dirname + 'index.html');
console.log("{}{}{} serving index page");
});
app.get('/bowl', function(req, res) {
console.log("{}{}{} serving bowl page");
res.sendFile(__dirname + '/public/bowl.html');
});
// interaction pages
app.get('/give', function(req, res) {
console.log("{}{}{} serving giving page");
res.sendFile(__dirname + '/give.html');
});
// for now take a random one
app.get('/take', function(req, res) {
console.log("{}{}{} taking");
res.sendFile(__dirname + '/take.html');
});
// for now take a random one
app.post('/send', function(req, res) {
console.log("{}{}{} sending");
console.log(req.body.email);
gapi.sendMail({email: req.body.email, link: 'https://drive.google.com/file/d/1BYl92BWZoAydnnBJmXfDTLGVf1WU0NDp/view?usp=sharing',
res: res});
console.log('{}{}{} Sent mail');
res.redirect('/bowl');
})
// interaction pages
app.get('/loadimages', function(req, res) {
console.log("loading images");
gapi.updateFileList({num_results: req.query.num_results, res: res});
});
app.post('/upload', function(req, res) {
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
}
let contribution = req.files.contribution;
console.log('{}{}{} Intended upload of ' + contribution.name);
// mimeType check
// const fileSize = fs.statSync(fileName).size; // error case on too large
// Use the mv() method to place the file somewhere on your server
contribution.mv('./media/uploaded.jpg', function(err) {
if (err)
return res.status(500).send(err);
});
gapi.uploadToCloud({name: contribution.name, // existing name
location: './media/uploaded.jpg', // temporary storage
mimeType: contribution.mimetype // mimetype
});
console.log('{}{}{} Upload of ' + contribution.name);
res.redirect('/bowl');
res.send('File was unable to be uploaded. Please try again.');
});
// ------------ HELPER LINKS BELOW HERE ---------------
// manual reauthing if needed
app.get('/reauth', function(req, res) {
gapi.getReAuthUrl(req, res);
});
// trigger listing the files in the drive
app.get('/testapi', function(req, res) {
gapi.sendMail({email: '[email protected]', link: 'https://drive.google.com/file/d/1BYl92BWZoAydnnBJmXfDTLGVf1WU0NDp/view?usp=sharing',
res: res});
});
// for the oauth flow
app.get('/oauth', function(req, res) {
// console.log("req:", req, "\nres:", res);
console.log("code:", req.query.code);
gapi.oauth2Client.getToken(req.query.code, (err, token) => {
if (err) return console.error('Error retrieving access token(s)', err);
console.log("token(s):", token);
gapi.oauth2Client.credentials = token;
});
res.sendFile(__dirname + '/index.html');
});
//listens for when we want model files
app.use('/models', express.static(__dirname + '/models'));
// tell our app where to listen for connections
app.listen(PORT, function() {
console.log('listening on PORT: ' + PORT);
});