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); }
/** * Checks if a file exists * * @param string $file path to file and filename * @param string $url the live URL to the file * @param bool $is_update perform an update instead of creating a new file * * @since 1.0 * * @return bool */ public function file_upload($file, $url, $is_update = false) { $this->_file_uploaded = false; // get the google drive service if (!isset($this->_google_drive_service)) { $this->_google_drive_service = new Google_Service_Drive($this->_google_drive_cdn->get_google_client()); } // get the google drive service instance $service = $this->_google_drive_service; if (!$service instanceof Google_Service_Drive) { return $url; } // get the folder id $folder_id = WPB_Google_Drive_Cdn_Settings::get_setting('folder_id', 'wpbgdc_folders'); // create the folder name $file_folder_to_str = str_replace(WP_CONTENT_DIR, '', str_replace(basename($file), '', $file)); $file_folder_to_str = str_replace('/', '_', $file_folder_to_str); $file_folder_to_str = sanitize_key($file_folder_to_str); // create the file basename $file_basename = $file_folder_to_str . basename($file); // get the google drive URL $google_drive_url = WPB_Google_Drive_Cdn_Settings::get_setting('folder_link', 'wpbgdc_folders'); try { // search for the file. $files = $service->files->listFiles(array('q' => "title='" . $file_basename . "' and '" . $folder_id . "' in parents")); } catch (Exception $e) { // if there is an error fetching the file, return the normal url return $url; } // checks if the file is a CSS file $is_css_file = substr($file, -4, 4) == '.css'; // search in the list of files for the current file foreach ($files->items as $g_file) { if (!$g_file instanceof Google_Service_Drive_DriveFile) { continue; } // file exists if ($g_file->getTitle() == $file_basename) { // get the date of the current file $file_date = filemtime($file); /** * check if we should update the file * this is the case when either * the date of the file on Google Drive is lower than the local file OR * when the etag is not correct (this is only used for css files at the moment) */ if (strtotime($g_file->getModifiedDate()) < $file_date or $is_css_file && $g_file->getEtag() != $this->get_etag_by_live_url($url)) { $additionalParams = array(); if ($is_css_file) { $additionalParams['data'] = $this->invoke_css_file($file, $url); $additionalParams['mimeType'] = 'text/css'; } else { $additionalParams['data'] = file_get_contents($file); } try { $g_file = $service->files->update($g_file->getId(), $g_file, $additionalParams); } catch (Exception $e) { $this->_google_drive_cdn->set_error($e->getMessage() . '(wpbgdc: file_upload 1; File: ' . $url . ')', false); } $this->_file_uploaded = true; } // update the file cache entry as well $this->set_cached_url($url, $google_drive_url . $g_file->getTitle(), $g_file->getId(), $g_file->getEtag()); // create the URL out of the Drive URL and return it. return $google_drive_url . $g_file->getTitle(); } } if ($is_css_file) { $mime_type = 'text/css'; } else { $mime_type = wp_check_filetype($file); $mime_type = $mime_type['type']; } $parent = new Google_Service_Drive_ParentReference(); $parent->setId($folder_id); // upload the file $new_file = new Google_Service_Drive_DriveFile(); $new_file->setTitle($file_basename); $new_file->setMimeType($mime_type); $new_file->setParents(array($parent)); $new_file->setFileSize(filesize($file)); $additionalParams = array('mimeType' => $mime_type, 'uploadType' => 'media'); if ($is_css_file) { $additionalParams['data'] = $this->invoke_css_file($file, $url); } else { $additionalParams['data'] = file_get_contents($file); } try { $createdFile = $service->files->insert($new_file, $additionalParams); } catch (Exception $e) { $this->_google_drive_cdn->set_error($e->getMessage() . '(wpbgdc: file_upload 2; File: ' . $url . ')', false); return $url; } $file_drive_url = $google_drive_url . $createdFile->getTitle(); $this->set_cached_url($url, $file_drive_url, $createdFile->getId(), $createdFile->getEtag()); $this->_file_uploaded = true; return $file_drive_url; }
function send_file_togoogle($morsle, $title, $filetobeuploaded, $mimetype, $collectionid = null) { // Now lets try and send the metadata as well using multipart! $service = new Google_Service_Drive($morsle->client); $file = new Google_Service_Drive_DriveFile(); $file->setTitle($title); $file->setMimeType($mimetype); $file->setFileSize(filesize($filetobeuploaded)); // Set the parent folder. if ($collectionid !== null) { $parent = new Google_Service_Drive_ParentReference(); $parent->setId($collectionid); $file->setParents(array($parent)); } $result2 = $service->files->insert($file, array('data' => file_get_contents($filetobeuploaded), 'mimeType' => $mimetype, 'uploadType' => 'multipart', 'convert' => 'true')); }