123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- include_once "templates/base.php";
- session_start();
- require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
- DEFINE("TESTFILE", 'testfile.txt');
- if (!file_exists(TESTFILE)) {
- $fh = fopen(TESTFILE, 'w');
- fseek($fh, 1024*1024*20);
- fwrite($fh, "!", 1);
- fclose($fh);
- }
- $client_id = '<YOUR_CLIENT_ID>';
- $client_secret = '<YOUR_CLIENT_SECRET>';
- $redirect_uri = '<YOUR_REDIRECT_URI>';
- $client = new Google_Client();
- $client->setClientId($client_id);
- $client->setClientSecret($client_secret);
- $client->setRedirectUri($redirect_uri);
- $client->addScope("https://www.googleapis.com/auth/drive");
- $service = new Google_Service_Drive($client);
- if (isset($_REQUEST['logout'])) {
- unset($_SESSION['upload_token']);
- }
- if (isset($_GET['code'])) {
- $client->authenticate($_GET['code']);
- $_SESSION['upload_token'] = $client->getAccessToken();
- $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
- header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
- }
- if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) {
- $client->setAccessToken($_SESSION['upload_token']);
- if ($client->isAccessTokenExpired()) {
- unset($_SESSION['upload_token']);
- }
- } else {
- $authUrl = $client->createAuthUrl();
- }
- if ($client->getAccessToken()) {
- $file = new Google_Service_Drive_DriveFile();
- $file->title = "Big File";
- $chunkSizeBytes = 1 * 1024 * 1024;
-
- $client->setDefer(true);
- $request = $service->files->insert($file);
-
- $media = new Google_Http_MediaFileUpload(
- $client,
- $request,
- 'text/plain',
- null,
- true,
- $chunkSizeBytes
- );
- $media->setFileSize(filesize(TESTFILE));
-
-
- $status = false;
- $handle = fopen(TESTFILE, "rb");
- while (!$status && !feof($handle)) {
-
-
-
- $chunk = readVideoChunk($handle, $chunkSizeBytes);
- $status = $media->nextChunk($chunk);
- }
-
-
- $result = false;
- if ($status != false) {
- $result = $status;
- }
- fclose($handle);
- }
- echo pageHeader("File Upload - Uploading a large file");
- if (strpos($client_id, "googleusercontent") == false) {
- echo missingClientSecretsWarning();
- exit;
- }
- function readVideoChunk ($handle, $chunkSize)
- {
- $byteCount = 0;
- $giantChunk = "";
- while (!feof($handle)) {
-
- $chunk = fread($handle, 8192);
- $byteCount += strlen($chunk);
- $giantChunk .= $chunk;
- if ($byteCount >= $chunkSize)
- {
- return $giantChunk;
- }
- }
- return $giantChunk;
- }
- ?>
- <div class="box">
- <div class="request">
- <?php
- if (isset($authUrl)) {
- echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
- }
- ?>
- </div>
- <div class="shortened">
- <?php
- if (isset($result) && $result) {
- var_dump($result);
- }
- ?>
- </div>
- </div>
- <?php
- echo pageFooter(__FILE__);
|