-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.js
More file actions
137 lines (120 loc) · 3.23 KB
/
app.js
File metadata and controls
137 lines (120 loc) · 3.23 KB
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
const fs = require("fs");
const express = require("express");
const multer = require("multer");
const OAuth2Data = require("./credentials.json");
var name,pic
const { google } = require("googleapis");
const app = express();
const CLIENT_ID = OAuth2Data.web.client_id;
const CLIENT_SECRET = OAuth2Data.web.client_secret;
const REDIRECT_URL = OAuth2Data.web.redirect_uris[0];
const oAuth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URL
);
var authed = false;
// If modifying these scopes, delete token.json.
const SCOPES =
"https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.profile";
app.set("view engine", "ejs");
var Storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, "./images");
},
filename: function (req, file, callback) {
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
},
});
var upload = multer({
storage: Storage,
}).single("file"); //Field name and max count
app.get("/", (req, res) => {
if (!authed) {
// Generate an OAuth URL and redirect there
var url = oAuth2Client.generateAuthUrl({
access_type: "offline",
scope: SCOPES,
});
console.log(url);
res.render("index", { url: url });
} else {
var oauth2 = google.oauth2({
auth: oAuth2Client,
version: "v2",
});
oauth2.userinfo.get(function (err, response) {
if (err) {
console.log(err);
} else {
console.log(response.data);
name = response.data.name
pic = response.data.picture
res.render("success", {
name: response.data.name,
pic: response.data.picture,
success:false
});
}
});
}
});
app.post("/upload", (req, res) => {
upload(req, res, function (err) {
if (err) {
console.log(err);
return res.end("Something went wrong");
} else {
console.log(req.file.path);
const drive = google.drive({ version: "v3",auth:oAuth2Client });
const fileMetadata = {
name: req.file.filename,
};
const media = {
mimeType: req.file.mimetype,
body: fs.createReadStream(req.file.path),
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id",
},
(err, file) => {
if (err) {
// Handle error
console.error(err);
} else {
fs.unlinkSync(req.file.path)
res.render("success",{name:name,pic:pic,success:true})
}
}
);
}
});
});
app.get('/logout',(req,res) => {
authed = false
res.redirect('/')
})
app.get("/google/callback", function (req, res) {
const code = req.query.code;
if (code) {
// Get an access token based on our OAuth code
oAuth2Client.getToken(code, function (err, tokens) {
if (err) {
console.log("Error authenticating");
console.log(err);
} else {
console.log("Successfully authenticated");
console.log(tokens)
oAuth2Client.setCredentials(tokens);
authed = true;
res.redirect("/");
}
});
}
});
app.listen(5000, () => {
console.log("App is listening on Port 5000");
});