Example #1
0
 /**
  * @param File $inputFile
  * @param string $rootFolderId
  * @return Google_Service_Drive_DriveFile
  * @throws \Exception
  */
 public function add(File $inputFile, string $rootFolderId = '') : Google_Service_Drive_DriveFile
 {
     $chunkSize = 1024 * 1024;
     $file = new Google_Service_Drive_DriveFile();
     $file->title = $inputFile->getTitle();
     $file->description = $inputFile->getDescription();
     if ($rootFolderId != '') {
         $parent = new \Google_Service_Drive_ParentReference();
         $parent->setId($rootFolderId);
         $file->setParents([$parent]);
     }
     $request = $this->service->files->insert($file);
     $media = new Google_Http_MediaFileUpload($this->client, $request, $inputFile->getMimeType(), null, true, $chunkSize);
     $size = filesize($inputFile->getLocalPath() . $inputFile->getLocalFileName());
     if ($size == false) {
         return $file;
     }
     $media->setFileSize($size);
     $status = false;
     $handle = fopen($inputFile->getLocalPath() . $inputFile->getLocalFileName(), 'rb');
     while (!$status && !feof($handle)) {
         $chunk = $this->readPhotoChunk($handle, $chunkSize);
         $status = $media->nextChunk($chunk);
     }
     if ($status == false) {
         throw new \Exception('Upload returned false');
     }
     fclose($handle);
     return $status;
 }