-
Notifications
You must be signed in to change notification settings - Fork 0
/
maker.js
434 lines (410 loc) · 16.5 KB
/
maker.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
var authConfig = {
version: "1.0",
dailyLimit: true, // Whether to limit each mailbox to submit requests only once a day
client_id: '', // Google Client ID
client_secret: '', // Google Client Secret
refresh_token: '', // Refresh token
domain: "", //College name to display
black_list: ["[email protected]"]
};
var gd;
var today;
var html = `
<html lang="us-en">
<head>
<meta charset="utf-8">
<title>Google Shared Drive</title>
<meta name="Mecha" content="noindex">
<link rel="apple-touch-icon" sizes="180x180" href="USE YOUR OWN ICON">
<link rel="icon" sizes="32x32" href="USE YOUR OWN ICON">
<link rel="icon" sizes="16x16" href="USE YOUR OWN ICON">
<link rel="mask-icon" href="USE YOUR OWN ICON" color="#5bbad5">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://raw.githubusercontent.com/LeeluPradhan/G-Index/master/css/mail-box-style.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://raw.githubusercontent.com/LeeluPradhan/G-Index/master/css/mail-box-style.css" crossorigin="anonymous">
</head>
<body style="background-image: url('Your Background Image');">
<br>
<center>
<a href="/"><img src="Head Logo - Use transparent PNG" height="50px"></a>
<br>
<br>
</center>
<main>
<div class="container min-height">
<div>
<div class="card waiting-screen" style="padding:50px">
<div class="card-block">
<div class="row">
<div class="col-sm-8 offset-sm-2 col-md-6 offset-md-3 text-center">
<h1><img src="//cdn.jsdelivr.net/gh/jscdn/images@master/google/google-logo-t.png" alt="Google Logo" style="width:50px;height:50px;"></h1> ${ authConfig.domain ? `
<h5>Create Shared Drive from: ${authConfig.domain}</h5>` : "" }
<p>
Multiple back-end API requests, the process takes a long time, please be patient!
<br>
<span style="color: red"><b>Never Submit Again, It'll ruin the System</b></span>
<br>
<span style="color: red"><b>PS: One Team Drive request per day per Email-id</b></span>
</p>
<br />
<div class="info-form text-left">
<form id="teamDriveForm">
<div class="form-group">
<label for="teamDriveName" class="sr-only">
Share Drive Name (can be changed later)
</label>
<input type="text" class="form-control" id="teamDriveName" placeholder="Name for your New Drive" />
</div>
<div class="form-group">
<label for="emailAddress" class="sr-only">
Your GMail Address
</label>
<input type="email" class="form-control" id="emailAddress" placeholder="[email protected]" />
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="customTheme" value="" />
<label class="form-check-label" for="customTheme">
Custom Shared Drive Theme Header (can be changed later)
</label>
</div>
<div id="customThemeSection" class="d-none">
<div id="teamDriveThemePreview"></div>
<div id="teamDriveThemeOptions">
<div class="form-check">
<input class="form-check-input" type="radio" name="teamDriveTheme" id="teamDriveThemeOptionRandom" value="random" checked />
<label class="form-check-label" for="teamDriveThemeOptionRandom">
random
</label>
</div>
</div>
</div>
<br />
<center>
<button type="submit" class="btn btn-primary">Submit</button> <a class="btn btn-danger" href="Change as per your requirement" role="button">Exit</a> <a class="btn btn-info" href="Change as per your requirement" role="button">Source</a></center>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="loadMe" tabindex="-1" role="dialog" aria-labelledby="loadMeLabel">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-body text-center">
<div class="d-flex justify-content-center">
<div class="spinner-border" role="status">
<span class="sr-only">Processing...</span>
</div>
</div>
<div clas="loader-txt">
<p>Processing...</p>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
var teamDriveThemes;
$("input[id=customTheme]").change(function() {
if ($(this).is(":checked")) {
$("#customThemeSection").removeClass("d-none");
} else {
$("#customThemeSection").addClass("d-none");
$("input[name=teamDriveTheme]")[0].click();
}
});
$.get("/teamDriveThemes", function(json) {
teamDriveThemes = json.teamDriveThemes;
$.each(json.teamDriveThemes, function(i, item) {
$("#teamDriveThemeOptions").append(\`
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="teamDriveTheme"
id="teamDriveThemeOption-\${item.id}"
value="\${item.id}"
/>
<label class="form-check-label" for="teamDriveThemeOption-\${item.id}">
\${item.id}
</label>
</div>
\`);
});
$("input[name=teamDriveTheme]").change(function() {
var themeId = this.value;
if (themeId === "random") {
$("#teamDriveThemePreview").html("");
} else {
var theme = teamDriveThemes.find(function(t) {
return t.id == themeId;
});
$("#teamDriveThemePreview").html(
\`
<div class="card" style="background-color: \${theme.colorRgb}">
<img src="\${theme.backgroundImageLink}" class="card-img-top" alt="\${theme.id}" />
<div class="card-body">
<h5 class="card-text" style="color: white">
\${theme.id}
</h5>
</div>
</div>
\`
);
}
});
$("#teamDriveForm").on("submit", function(event) {
event.preventDefault();
$("#loadMe").modal({
backdrop: "static", //remove ability to close modal with click
keyboard: false, //remove option to close with keyboard
show: true //Display loader!
});
$.ajax({
type: "POST",
url: "/drive",
data: JSON.stringify({
teamDriveName: $("input[id=teamDriveName]").val(),
teamDriveThemeId: $("input[name=teamDriveTheme]:checked").val(),
emailAddress: $("input[id=emailAddress]").val()
}),
success: function(data) {
$("#loadMe").modal("hide");
alert("Success!");
},
error: function(request, status, error) {
$("#loadMe").modal("hide");
alert("Process Failed!" + request.responseText);
},
contentType: "application/json"
});
});
});
</script>
<style type="text/css">
.card-img-top {
width: 100%;
object-fit: cover;
}
</style>
</main>
</body>
</html>
`;
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
var dailyLimit = [];
/**
* Fetch and log a request
* @param {Request} request
*/
async function handleRequest(request) {
if (authConfig.dailyLimit) {
if (!today) today = new Date().getDate();
// Remove email rate limit every day
if (new Date().getDate() != today) {
today = new Date().getDate();
dailyLimit.length = 0;
}
}
if (gd == undefined) {
gd = new googleDrive(authConfig);
}
let url = new URL(request.url);
let path = url.pathname;
switch (path) {
case "/teamDriveThemes":
let teamDriveThemes = await gd.getTeamDriveThemes();
return new Response(JSON.stringify(teamDriveThemes), {
status: 200,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
}
});
case "/drive":
if (request.method === "POST") {
const requestBody = await request.json();
if (authConfig.dailyLimit) {
if (dailyLimit.includes(requestBody.emailAddress)) {
return new Response("Submit only one request per day.", {
status: 429
});
} else {
dailyLimit.push(requestBody.emailAddress);
}
}
if (authConfig.black_list.includes(requestBody.emailAddress)) {
return new Response("Failed", {
status: 429
});
}
try {
let result = await gd.createAndShareTeamDrive(requestBody);
return new Response("OK", {
status: 200
});
} catch (err) {
return new Response(err.toString(), {
status: 500
});
}
} else if (request.method === "OPTIONS") {
return new Response("", {
status: 200,
headers: {
"Access-Control-Allow-Origin": "*"
}
});
} else {
return new Response("Bad Request", {
status: 400
});
}
default:
return new Response(html, {
status: 200,
headers: {
"Content-Type": "text/html; charset=utf-8",
"Access-Control-Allow-Origin": "*"
}
});
}
}
// https://stackoverflow.com/a/2117523
function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, c => {
// tslint:disable-next-line:one-variable-per-declaration
const r = (Math.random() * 16) | 0,
// tslint:disable-next-line:triple-equals
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
class googleDrive {
constructor(authConfig) {
this.authConfig = authConfig;
this.accessToken();
}
async getTeamDriveThemes() {
let url = "https://www.googleapis.com/drive/v3/about";
let requestOption = await this.requestOption();
let params = { fields: "teamDriveThemes" };
url += "?" + this.enQuery(params);
let response = await fetch(url, requestOption);
return await response.json();
}
async createAndShareTeamDrive(requestBody) {
// Create team drive
console.log("Creating TeamDrive");
let url = "https://www.googleapis.com/drive/v3/drives";
let requestOption = await this.requestOption(
{ "Content-Type": "application/json" },
"POST"
);
let params = { requestId: uuidv4() };
url += "?" + this.enQuery(params);
let post_data = {
name: requestBody.teamDriveName
};
if (
requestBody.teamDriveThemeId &&
requestBody.teamDriveThemeId !== "random"
) {
post_data.themeId = requestBody.teamDriveThemeId;
}
requestOption.body = JSON.stringify(post_data);
let response = await fetch(url, requestOption);
let result = await response.json();
const teamDriveId = result.id;
console.log("Created TeamDrive ID", teamDriveId);
// Get created drive user permission ID
console.log(`Getting creator permission ID`);
url = `https://www.googleapis.com/drive/v3/files/${teamDriveId}/permissions`;
params = { supportsAllDrives: true };
params.fields = "permissions(id,emailAddress)";
url += "?" + this.enQuery(params);
requestOption = await this.requestOption();
response = await fetch(url, requestOption);
result = await response.json();
const currentUserPermissionID = result.permissions[0].id;
console.log(currentUserPermissionID);
// Share team drive with email address
console.log(`Sharing the team drive to ${requestBody.emailAddress}`);
url = `https://www.googleapis.com/drive/v3/files/${teamDriveId}/permissions`;
params = { supportsAllDrives: true };
url += "?" + this.enQuery(params);
requestOption = await this.requestOption(
{ "Content-Type": "application/json" },
"POST"
);
post_data = {
role: "organizer",
type: "user",
emailAddress: requestBody.emailAddress
};
requestOption.body = JSON.stringify(post_data);
response = await fetch(url, requestOption);
await response.json();
// Delete creator from the team drive
console.log("Deleting creator from the team drive");
url = `https://www.googleapis.com/drive/v3/files/${teamDriveId}/permissions/${currentUserPermissionID}`;
params = { supportsAllDrives: true };
url += "?" + this.enQuery(params);
requestOption = await this.requestOption({}, "DELETE");
response = await fetch(url, requestOption);
return await response.text();
}
async accessToken() {
console.log("accessToken");
if (
this.authConfig.expires == undefined ||
this.authConfig.expires < Date.now()
) {
const obj = await this.fetchAccessToken();
if (obj.access_token != undefined) {
this.authConfig.accessToken = obj.access_token;
this.authConfig.expires = Date.now() + 3500 * 1000;
}
}
return this.authConfig.accessToken;
}
async fetchAccessToken() {
console.log("fetchAccessToken");
const url = "https://www.googleapis.com/oauth2/v4/token";
const headers = {
"Content-Type": "application/x-www-form-urlencoded"
};
const post_data = {
client_id: this.authConfig.client_id,
client_secret: this.authConfig.client_secret,
refresh_token: this.authConfig.refresh_token,
grant_type: "refresh_token"
};
let requestOption = {
method: "POST",
headers: headers,
body: this.enQuery(post_data)
};
const response = await fetch(url, requestOption);
return await response.json();
}
async requestOption(headers = {}, method = "GET") {
const accessToken = await this.accessToken();
headers["authorization"] = "Bearer " + accessToken;
return { method: method, headers: headers };
}
enQuery(data) {
const ret = [];
for (let d in data) {
ret.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
}
return ret.join("&");
}
}