Exemplo n.º 1
0
function doResumableUpload(Google_Client $client)
{
    // Drive service
    $driveService = new Google_Service_Drive($client);
    $gs_basepath = 'gs://' . CloudStorageTools::getDefaultGoogleStorageBucketName();
    $fileName = UPLOAD_FILENAME;
    // 82.52MB file
    $fileToUploadPath = $gs_basepath . "/{$fileName}";
    $uploadFilesize = filesize($fileToUploadPath);
    $options = array('gs' => array('enable_cache' => false, 'acl' => 'public-read'));
    $ctx = stream_context_create($options);
    if (($handle = fopen($fileToUploadPath, 'r', false, $ctx)) === false) {
        throw new Exception('fopen failed.');
    }
    $mimeType = CloudStorageTools::getContentType($handle);
    // prepare a drive file
    $file = new Google_Service_Drive_DriveFile();
    $file->setTitle($fileName);
    $file->setMimeType($mimeType);
    $file->setFileSize($uploadFilesize);
    // You can also set the parent folder:
    // @see https://developers.google.com/drive/v2/reference/files/insert
    $chunkSizeBytes = 256 * 1024 * 4 * 1;
    // upload in multiples of 256K (1M * n)
    // Call the API with the media upload, defer so it doesn't immediately return.
    $client->setDefer(true);
    $request = $driveService->files->insert($file);
    // Create a media file upload to represent our upload process.
    $media = new Google_Http_MediaFileUpload($client, $request, $mimeType, null, true, $chunkSizeBytes);
    // set the media filesize to the actual filesize.
    $media->setFileSize($uploadFilesize);
    // Upload the various chunks. $status will be false until the process is complete.
    $status = false;
    while (!$status && !feof($handle)) {
        $chunk = readChunk($handle, $chunkSizeBytes);
        $status = $media->nextChunk($chunk);
    }
    fclose($handle);
    // Reset to the client to execute requests immediately in the future.
    $client->setDefer(false);
    var_dump($status);
}
Exemplo n.º 2
0
function jsonMultipleOut($liststr)
{
    global $CHUNK_DIR;
    $key = md5($liststr);
    $combfile = $CHUNK_DIR . $key . '.json.gz';
    if (file_exists($combfile)) {
        echo file_get_contents($combfile);
        return true;
    }
    $list = json_decode($liststr);
    $combined = array();
    $count = count($list);
    for ($i = 0; $i < $count; $i++) {
        $chunk = $list[$i];
        if (array_key_exists('x', $chunk)) {
            $posx = $chunk->x;
            $posz = $chunk->z;
            $chunkFile = $CHUNK_DIR . "c.{$posx}.{$posz}.json";
            if (file_exists($chunkFile)) {
                $chunkData = json_decode(file_get_contents($chunkFile));
            } else {
                $chunkData = readChunk($posx, $posz);
                $cf = fopen($chunkFile, 'wb');
                fwrite($cf, json_encode($chunkData));
                fclose($cf);
            }
            $combined[] = $chunkData;
        }
    }
    $gz = gzencode(json_encode($combined));
    echo $gz;
    $ccf = fopen($combfile, 'wb');
    fwrite($ccf, $gz);
    fclose($ccf);
    return true;
}