-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
254 lines (237 loc) · 8.53 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css"
integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I"
crossorigin="anonymous"
/>
<link rel="stylesheet" href="css/style.css" />
<title>Drive API Quickstart</title>
<meta charset="utf-8" />
</head>
<body>
<p>Hackathon 2020</p>
<!--Add buttons to initiate auth sequence and sign out-->
<button id="authorize_button" style="display: none;">Authorize</button>
<button id="signout_button" style="display: none;">Sign Out</button>
<pre id="content" style="white-space: pre-wrap;"></pre>
<div id="main">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">
SIH-2020
</a>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNav"
aria-controls="navbarNav"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">
Home
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">
About
</a>
</li>
</ul>
</div>
</div>
</nav>
<section>
<div class="container">
<div class="row gx-5 gy-5" id="row"></div>
</div>
</section>
<section>
<div class="container">
<div class="row gx-5 gy-5" id="row-res"></div>
</div>
</section>
</div>
<script type="text/javascript">
// Client ID and API key from the Developer Console
var CLIENT_ID =
"213878148327-3sndofu05bntb9m0qh3kioa3a5vghrdb.apps.googleusercontent.com";
var API_KEY = "AIzaSyCWm75mcCpOcsARjw02Sae1jAm1ACAvrYA";
// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = [
"https://www.googleapis.com/discovery/v1/apis/drive/v3/rest",
];
// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = "https://www.googleapis.com/auth/drive.metadata.readonly";
var authorizeButton = document.getElementById("authorize_button");
var signoutButton = document.getElementById("signout_button");
/**
* On load, called to load the auth2 library and API client library.
*/
function handleClientLoad() {
gapi.load("client:auth2", initClient);
}
/**
* Initializes the API client library and sets up sign-in state
* listeners.
*/
function initClient() {
gapi.client
.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES,
})
.then(
function () {
// Listen for sign-in state changes.
gapi.auth2
.getAuthInstance()
.isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
},
function (error) {
appendPre(JSON.stringify(error, null, 2));
}
);
}
/**
* Called when the signed in status changes, to update the UI
* appropriately. After a sign-in, the API is called.
*/
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = "none";
signoutButton.style.display = "block";
listFiles();
} else {
authorizeButton.style.display = "block";
signoutButton.style.display = "none";
}
}
/**
* Sign in the user upon button click.
*/
function handleAuthClick(event) {
gapi.auth2.getAuthInstance().signIn();
}
/**
* Sign out the user upon button click.
*/
function handleSignoutClick(event) {
gapi.auth2.getAuthInstance().signOut();
}
/**
* Append a pre element to the body containing the given message
* as its text node. Used to display the results of the API call.
*
* @param {string} message Text to be placed in pre element.
*/
function appendPre(message) {
var row = document.getElementById("row");
var div = document.createElement("div");
div.className = "col-lg-4 col-xs-12";
var img = document.createElement("img");
img.src = message;
div.append(img);
row.append(div);
}
function appendRes(message) {
var row = document.getElementById("row-res");
var div = document.createElement("div");
div.className = "col-lg-4 col-xs-12";
var img = document.createElement("img");
var caption = document.createElement("figcaption");
caption.innerHTML = "Result";
div.append(caption);
img.id = "image";
img.src = message;
div.appendChild(img);
row.append(div);
}
/**
* Print files.
*/
function listFiles() {
gapi.client.drive.files
.list({
pageSize: 10,
fields: "nextPageToken, files(id, name)",
})
.then(function (response) {
var files = response.result.files;
if (files && files.length > 0) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
console.log(file);
if (
(file.name != "result.png" ||
file.name != "result.jpg" ||
file.name != "result.jpeg") &&
(file.name.substring(file.name.lastIndexOf(".") + 1) ==
"jpg" ||
(file.name.substring(file.name.lastIndexOf(".") + 1) ==
"jpeg") |
(file.name.substring(file.name.lastIndexOf(".") + 1) ==
"png"))
) {
appendPre(
`https://drive.google.com/uc?export=download&id=${file.id}`
);
}
if (
(file.name == "result.png" ||
file.name == "result.jpg" ||
file.name == "result.jpeg") &&
(file.name.substring(file.name.lastIndexOf(".") + 1) ==
"jpg" ||
(file.name.substring(file.name.lastIndexOf(".") + 1) ==
"jpeg") |
(file.name.substring(file.name.lastIndexOf(".") + 1) ==
"png"))
) {
appendRes(
`https://drive.google.com/uc?export=download&id=${file.id}`
);
}
}
} else {
document.write("No file found in drive");
}
});
}
</script>
<script
async
defer
src="https://apis.google.com/js/api.js"
onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()"
></script>
<script
src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"
></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/js/bootstrap.min.js"
integrity="sha384-oesi62hOLfzrys4LxRF63OJCXdXDipiYWBnvTl9Y9/TRlw5xlKIEHpNyvvDShgf/"
crossorigin="anonymous"
></script>
</body>
</html>
<!-- [END drive_quickstart] -->