|
24 | 24 | * Ensure you've downloaded your oauth credentials |
25 | 25 | ************************************************/ |
26 | 26 | if (!$oauth_credentials = getOAuthCredentialsFile()) { |
27 | | - echo missingOAuth2CredentialsWarning(); |
28 | | - return; |
| 27 | + echo missingOAuth2CredentialsWarning(); |
| 28 | + return; |
29 | 29 | } |
30 | 30 |
|
31 | 31 | /************************************************ |
|
48 | 48 | * bundle in the session, and redirect to ourself. |
49 | 49 | ************************************************/ |
50 | 50 | if (isset($_GET['code'])) { |
51 | | - $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); |
52 | | - $client->setAccessToken($token); |
| 51 | + $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); |
| 52 | + $client->setAccessToken($token); |
53 | 53 |
|
54 | | - // store in the session also |
55 | | - $_SESSION['upload_token'] = $token; |
| 54 | + // store in the session also |
| 55 | + $_SESSION['upload_token'] = $token; |
56 | 56 |
|
57 | | - // redirect back to the example |
58 | | - header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); |
| 57 | + // redirect back to the example |
| 58 | + header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); |
59 | 59 | } |
60 | 60 |
|
61 | 61 | // set the access token as part of the client |
62 | 62 | if (!empty($_SESSION['upload_token'])) { |
63 | | - $client->setAccessToken($_SESSION['upload_token']); |
64 | | - if ($client->isAccessTokenExpired()) { |
65 | | - unset($_SESSION['upload_token']); |
66 | | - } |
| 63 | + $client->setAccessToken($_SESSION['upload_token']); |
| 64 | + if ($client->isAccessTokenExpired()) { |
| 65 | + unset($_SESSION['upload_token']); |
| 66 | + } |
67 | 67 | } else { |
68 | | - $authUrl = $client->createAuthUrl(); |
| 68 | + $authUrl = $client->createAuthUrl(); |
69 | 69 | } |
70 | 70 |
|
71 | 71 | /************************************************ |
72 | 72 | * If we're signed in then lets try to download our |
73 | 73 | * file. |
74 | 74 | ************************************************/ |
75 | 75 | if ($client->getAccessToken()) { |
76 | | - // Check for "Big File" and include the file ID and size |
77 | | - $files = $service->files->listFiles([ |
78 | | - 'q' => "name='Big File'", |
79 | | - 'fields' => 'files(id,size)' |
80 | | - ]); |
81 | | - |
82 | | - if (count($files) == 0) { |
83 | | - echo " |
| 76 | + // Check for "Big File" and include the file ID and size |
| 77 | + $files = $service->files->listFiles([ |
| 78 | + 'q' => "name='Big File'", |
| 79 | + 'fields' => 'files(id,size)' |
| 80 | + ]); |
| 81 | + |
| 82 | + if (count($files) == 0) { |
| 83 | + echo " |
84 | 84 | <h3 class='warn'> |
85 | 85 | Before you can use this sample, you need to |
86 | 86 | <a href='/large-file-upload.php'>upload a large file to Drive</a>. |
87 | 87 | </h3>"; |
88 | | - return; |
89 | | - } |
90 | | - |
91 | | - // If this is a POST, download the file |
92 | | - if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
93 | | - // Determine the file's size and ID |
94 | | - $fileId = $files[0]->id; |
95 | | - $fileSize = intval($files[0]->size); |
96 | | - |
97 | | - // Get the authorized Guzzle HTTP client |
98 | | - $http = $client->authorize(); |
99 | | - |
100 | | - // Open a file for writing |
101 | | - $fp = fopen('Big File (downloaded)', 'w'); |
102 | | - |
103 | | - // Download in 1 MB chunks |
104 | | - $chunkSizeBytes = 1 * 1024 * 1024; |
105 | | - $chunkStart = 0; |
106 | | - |
107 | | - // Iterate over each chunk and write it to our file |
108 | | - while ($chunkStart < $fileSize) { |
109 | | - $chunkEnd = $chunkStart + $chunkSizeBytes; |
110 | | - $response = $http->request( |
111 | | - 'GET', |
112 | | - sprintf('/drive/v3/files/%s', $fileId), |
113 | | - [ |
114 | | - 'query' => ['alt' => 'media'], |
115 | | - 'headers' => [ |
116 | | - 'Range' => sprintf('bytes=%s-%s', $chunkStart, $chunkEnd) |
117 | | - ] |
118 | | - ] |
119 | | - ); |
120 | | - $chunkStart = $chunkEnd + 1; |
121 | | - fwrite($fp, $response->getBody()->getContents()); |
| 88 | + return; |
122 | 89 | } |
123 | | - // close the file pointer |
124 | | - fclose($fp); |
125 | 90 |
|
126 | | - // redirect back to this example |
127 | | - header('Location: ' . filter_var($redirect_uri . '?downloaded', FILTER_SANITIZE_URL)); |
128 | | - } |
| 91 | + // If this is a POST, download the file |
| 92 | + if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
| 93 | + // Determine the file's size and ID |
| 94 | + $fileId = $files[0]->id; |
| 95 | + $fileSize = intval($files[0]->size); |
| 96 | + |
| 97 | + // Get the authorized Guzzle HTTP client |
| 98 | + $http = $client->authorize(); |
| 99 | + |
| 100 | + // Open a file for writing |
| 101 | + $fp = fopen('Big File (downloaded)', 'w'); |
| 102 | + |
| 103 | + // Download in 1 MB chunks |
| 104 | + $chunkSizeBytes = 1 * 1024 * 1024; |
| 105 | + $chunkStart = 0; |
| 106 | + |
| 107 | + // Iterate over each chunk and write it to our file |
| 108 | + while ($chunkStart < $fileSize) { |
| 109 | + $chunkEnd = $chunkStart + $chunkSizeBytes; |
| 110 | + $response = $http->request( |
| 111 | + 'GET', |
| 112 | + sprintf('/drive/v3/files/%s', $fileId), |
| 113 | + [ |
| 114 | + 'query' => ['alt' => 'media'], |
| 115 | + 'headers' => [ |
| 116 | + 'Range' => sprintf('bytes=%s-%s', $chunkStart, $chunkEnd) |
| 117 | + ] |
| 118 | + ] |
| 119 | + ); |
| 120 | + $chunkStart = $chunkEnd + 1; |
| 121 | + fwrite($fp, $response->getBody()->getContents()); |
| 122 | + } |
| 123 | + // close the file pointer |
| 124 | + fclose($fp); |
| 125 | + |
| 126 | + // redirect back to this example |
| 127 | + header('Location: ' . filter_var($redirect_uri . '?downloaded', FILTER_SANITIZE_URL)); |
| 128 | + } |
129 | 129 | } |
130 | 130 | ?> |
131 | 131 |
|
132 | 132 | <div class="box"> |
133 | | -<?php if (isset($authUrl)): ?> |
| 133 | +<?php if (isset($authUrl)) : ?> |
134 | 134 | <div class="request"> |
135 | 135 | <a class='login' href='<?= $authUrl ?>'>Connect Me!</a> |
136 | 136 | </div> |
137 | | -<?php elseif(isset($_GET['downloaded'])): ?> |
| 137 | +<?php elseif (isset($_GET['downloaded'])) : ?> |
138 | 138 | <div class="shortened"> |
139 | 139 | <p>Your call was successful! Check your filesystem for the file:</p> |
140 | 140 | <p><code><?= __DIR__ . DIRECTORY_SEPARATOR ?>Big File (downloaded)</code></p> |
141 | 141 | </div> |
142 | | -<?php else: ?> |
| 142 | +<?php else : ?> |
143 | 143 | <form method="POST"> |
144 | 144 | <input type="submit" value="Click here to download a large (20MB) test file" /> |
145 | 145 | </form> |
146 | 146 | <?php endif ?> |
147 | 147 | </div> |
148 | 148 |
|
149 | | -<?= pageFooter(__FILE__) ?> |
| 149 | +<?= pageFooter(__FILE__) |
0 commit comments