|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2011 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +include_once __DIR__ . '/../vendor/autoload.php'; |
| 19 | +include_once "templates/base.php"; |
| 20 | + |
| 21 | +echo pageHeader("File Download - Downloading a large file"); |
| 22 | + |
| 23 | +/************************************************* |
| 24 | + * Ensure you've downloaded your oauth credentials |
| 25 | + ************************************************/ |
| 26 | +if (!$oauth_credentials = getOAuthCredentialsFile()) { |
| 27 | + echo missingOAuth2CredentialsWarning(); |
| 28 | + return; |
| 29 | +} |
| 30 | + |
| 31 | +/************************************************ |
| 32 | + * The redirect URI is to the current page, e.g: |
| 33 | + * http://localhost:8080/large-file-download.php |
| 34 | + ************************************************/ |
| 35 | +$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; |
| 36 | + |
| 37 | +$client = new Google_Client(); |
| 38 | +$client->setAuthConfig($oauth_credentials); |
| 39 | +$client->setRedirectUri($redirect_uri); |
| 40 | +$client->addScope("https://www.googleapis.com/auth/drive"); |
| 41 | +$service = new Google_Service_Drive($client); |
| 42 | + |
| 43 | +/************************************************ |
| 44 | + * If we have a code back from the OAuth 2.0 flow, |
| 45 | + * we need to exchange that with the |
| 46 | + * Google_Client::fetchAccessTokenWithAuthCode() |
| 47 | + * function. We store the resultant access token |
| 48 | + * bundle in the session, and redirect to ourself. |
| 49 | + ************************************************/ |
| 50 | +if (isset($_GET['code'])) { |
| 51 | + $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); |
| 52 | + $client->setAccessToken($token); |
| 53 | + |
| 54 | + // store in the session also |
| 55 | + $_SESSION['upload_token'] = $token; |
| 56 | + |
| 57 | + // redirect back to the example |
| 58 | + header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); |
| 59 | +} |
| 60 | + |
| 61 | +// set the access token as part of the client |
| 62 | +if (!empty($_SESSION['upload_token'])) { |
| 63 | + $client->setAccessToken($_SESSION['upload_token']); |
| 64 | + if ($client->isAccessTokenExpired()) { |
| 65 | + unset($_SESSION['upload_token']); |
| 66 | + } |
| 67 | +} else { |
| 68 | + $authUrl = $client->createAuthUrl(); |
| 69 | +} |
| 70 | + |
| 71 | +/************************************************ |
| 72 | + * If we're signed in then lets try to download our |
| 73 | + * file. |
| 74 | + ************************************************/ |
| 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 " |
| 84 | + <h3 class='warn'> |
| 85 | + Before you can use this sample, you need to |
| 86 | + <a href='/large-file-upload.php'>upload a large file to Drive</a>. |
| 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()); |
| 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 | +} |
| 130 | +?> |
| 131 | + |
| 132 | +<div class="box"> |
| 133 | +<?php if (isset($authUrl)): ?> |
| 134 | + <div class="request"> |
| 135 | + <a class='login' href='<?= $authUrl ?>'>Connect Me!</a> |
| 136 | + </div> |
| 137 | +<?php elseif(isset($_GET['downloaded'])): ?> |
| 138 | + <div class="shortened"> |
| 139 | + <p>Your call was successful! Check your filesystem for the file:</p> |
| 140 | + <p><code><?= __DIR__ . DIRECTORY_SEPARATOR ?>Big File (downloaded)</code></p> |
| 141 | + </div> |
| 142 | +<?php else: ?> |
| 143 | + <form method="POST"> |
| 144 | + <input type="submit" value="Click here to download a large (20MB) test file" /> |
| 145 | + </form> |
| 146 | +<?php endif ?> |
| 147 | +</div> |
| 148 | + |
| 149 | +<?= pageFooter(__FILE__) ?> |
0 commit comments