Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
308 lines (272 loc) · 7.89 KB
/
Copy pathindex.php
File metadata and controls
308 lines (272 loc) · 7.89 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
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
<html>
<body>
<?php
$valid = 1;
$fileId = '';
if( isset($_GET["state"]) && $_GET["state"] )
{
$state = json_decode($_GET["state"]);
if( $state && is_object( $state ) )
{
if( $state->action == 'open' )
{
if( isset( $state->ids ) && $state->ids )
$fileId = $state->ids[0];
else
{
echo 'A document created in Drive does not support direct download. You should first convert it to a downloadable format';
$valid = 0;
}
}
else if( $state->action == 'create' )
{
if( isset( $state->folderId ) && $state->folderId )
$fileId = $state->folderId;
else
{
echo 'Use the service with \'Open\' or \'New\' buttons in Drive UI';
$valid = 0;
}
}
else
{
echo 'Invalid state';
$valid = 0;
}
}
else
{
echo 'Use the service with \'Open\' or \'New\' buttons in Drive UI';
$valid = 0;
}
}
else
{
echo 'Use the service with \'Open\' or \'New\' buttons in Drive UI';
$valid = 0;
}?>
<input id="fileId" type="hidden" value="<?php echo $fileId; ?>" />
<pre>Source code available at: <a href="https://github.com/yasirkula/DownloadLinkGeneratorForGoogleDrive">https://github.com/yasirkula/DownloadLinkGeneratorForGoogleDrive</a> (using <i>HTML</i>, <i>PHP</i> and <i>Javascript</i>)</br>
Have any questions? Drop me a mail at <a href="mailto:yasirkula@gmail.com">yasirkula@gmail.com</a></br>
Note that the file(s) or the parent folder must be shared publicly for the download links to work everywhere.</pre>
<div id="authorize-div" style="display: none">
You need to authorize access to Drive first: <button id="authorize-button" onclick="handleAuthClick()">Authorize</button>
</div>
<?php if( $valid == 1 ) { ?>
<pre id="status">Status: <span style="text-style=bold; color:blue;">please wait...</span></pre>
<pre id="result"></pre>
<pre id="error" style="color: red; text-style: bold;"></pre>
<?php } ?>
<script type="text/javascript">
var CLIENT_ID = 'YOUR_APP_CLIENT_ID';
var SCOPES = 'https://www.googleapis.com/auth/drive.install https://www.googleapis.com/auth/drive.metadata.readonly';
var statusText = document.getElementById('status');
var resultText = document.getElementById('result');
var errorText = document.getElementById('error');
var waitingFoldersStack = [];
function checkAuth()
{
gapi.auth.authorize( {
'client_id': CLIENT_ID,
'scope': SCOPES,
'immediate': true
}, handleAuthResult );
}
function handleAuthResult( authResult )
{
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
authorizeDiv.style.display = 'none';
if( isValidOp() )
loadDriveApi();
} else {
authorizeDiv.style.display = 'inline';
if( isValidOp() )
statusText.innerHTML = "";
}
}
function handleAuthClick()
{
gapi.auth.authorize( {
client_id: CLIENT_ID,
scope: SCOPES,
immediate: false
}, handleAuthResult );
return false;
}
function loadDriveApi()
{
gapi.client.load('drive', 'v3', handleRequest);
}
function handleRequest()
{
if( !isValidOp() )
return;
statusText.innerHTML = "Status: <span style=\"text-style=bold; color:blue;\">please wait...</span>";
var request = gapi.client.drive.files.get({
'fileId': getFileId(),
'fields': "mimeType, webContentLink"
});
request.execute( function(resp) {
if( !resp.error )
{
if( resp.mimeType == 'application/vnd.google-apps.folder' )
{
getFilesRecursively( getFileId(), "" );
}
else
{
if( resp.webContentLink )
resultText.innerHTML = resp.webContentLink;
else
resultText.innerHTML = "File is not shared";
statusText.innerHTML = "Status: <span style=\"text-style=bold; color:green;\">finished</span>";
}
}
else
handleError( resp.error );
});
}
function getFilesRecursively( folderId, relativePath )
{
var getFiles = function(request)
{
request.execute(function(resp)
{
if( !resp.error )
{
var files = resp.files;
if (files && files.length > 0)
{
for (var i = 0; i < files.length; i++)
{
var file = files[i];
if( file.webContentLink )
resultText.innerHTML += relativePath + file.name + " <a href=\"" + file.webContentLink + "\">" + file.webContentLink + "</a>\r\n";
}
}
var nextPageToken = resp.nextPageToken;
if (nextPageToken)
{
request = gapi.client.drive.files.list({
'q': "trashed=false and '" + folderId + "' in parents and mimeType != 'application/vnd.google-apps.folder'",
'pageSize': 1000,
'fields': "nextPageToken, files(name, webContentLink)",
'pageToken': nextPageToken
});
getFiles(request);
}
else
{
if( waitingFoldersStack.length > 0 )
{
var folderToEnter = waitingFoldersStack.shift();
console.log( "Entering folder: " + folderToEnter._relativePath );
getFilesRecursively( folderToEnter._id, folderToEnter._relativePath );
}
else
{
statusText.innerHTML = "Status: <span style=\"text-style=bold; color:green;\">finished</span>";
}
}
}
else
handleError( resp.error );
});
}
var getFolders = function(request)
{
request.execute(function(resp)
{
if( !resp.error )
{
var folders = resp.files;
if (folders && folders.length > 0)
{
for (var i = 0; i < folders.length; i++)
{
var folder = folders[i];
waitingFoldersStack.push( { _id: folder.id, _relativePath: relativePath + folder.name + "\\" } )
}
}
var nextPageToken = resp.nextPageToken;
if (nextPageToken)
{
request = gapi.client.drive.files.list({
'q': "trashed=false and '" + folderId + "' in parents and mimeType = 'application/vnd.google-apps.folder'",
'pageSize': 1000,
'fields': "nextPageToken, files(id, name)",
'pageToken': nextPageToken
});
getFolders(request);
}
else
{
request = gapi.client.drive.files.list({
'q': "trashed=false and '" + folderId + "' in parents and mimeType != 'application/vnd.google-apps.folder'",
'pageSize': 1000,
'fields': "nextPageToken, files(name, webContentLink)"
});
getFiles(request);
}
}
else
handleError( resp.error );
});
}
var request = gapi.client.drive.files.list({
'q': "trashed=false and '" + folderId + "' in parents and mimeType = 'application/vnd.google-apps.folder'",
'pageSize': 1000,
'fields': "nextPageToken, files(id, name)"
});
getFolders( request );
}
function getFileId()
{
var val = document.getElementById('fileId').value;
if( !val )
return "";
return val;
}
function isValidOp()
{
return getFileId().length > 0;
}
function handleError( err )
{
console.log( "ERROR: " + JSON.stringify( err ) );
var reason = "";
var msg = "";
if( err.errors && err.errors.length > 0 )
{
reason = err.errors[0].reason;
msg = err.errors[0].message;
}
else if( err.data && err.data.length > 0 )
{
reason = err.data[0].reason;
msg = err.data[0].message;
}
if( err.code == 401 )
{
handleAuthClick();
}
else if( err.code == 403 )
{
if( reason == "rateLimitExceeded" || reason == "userRateLimitExceeded" )
errorText.innerHTML = "Too many requests; try again in a minute.";
else if( reason == "dailyLimitExceeded" )
errorText.innerHTML = "App reached daily limit (just wow O_O ); service will be available tomorrow.";
else
errorText.innerHTML = err.code + ": " + err.message + "(" + reason + ": " + msg + ")\r\n";
}
else
{
errorText.innerHTML = err.code + ": " + err.message + " (" + reason + ": " + msg + ")\r\n";
}
statusText.innerHTML = "Status: <span style=\"text-style=bold; color:red;\">see error log below</span>";
}
</script>
<script src="https://apis.google.com/js/client.js?onload=checkAuth"></script>
</body>
</html>