public function process(File $file)
 {
     if (null == $file->getGoogleId() || $file->isOperationCreate()) {
         // create new file
         $response = $this->googleDriveApi->insertFile($file);
         // update file with googleId
         $file->setGoogleId($response['id']);
         $this->logger->info("File created", ['file' => $file->toArray(), 'response' => $response]);
     } else {
         // overwrite existing file
         try {
             $response = $this->googleDriveApi->updateFile($file);
             $this->logger->info("File updated", ['file' => $file->toArray(), 'response' => $response]);
         } catch (BadResponseException $e) {
             $statusCode = $e->getResponse()->getStatusCode();
             if ($statusCode == 404) {
                 // file not found - create new one and issue a warning
                 $response = $this->googleDriveApi->insertFile($file);
                 $file->setGoogleId($response['id']);
                 $this->logger->info("File not found, created new one", ['file' => $file->toArray(), 'response' => $response]);
             } else {
                 throw $e;
             }
         }
     }
     return $file;
 }
 public function process(File $file)
 {
     if (null == $file->getGoogleId() || $file->isOperationCreate()) {
         // create new file
         $fileRes = $this->googleDriveApi->insertFile($file);
         // get list of worksheets in file, there shall be only one
         $sheets = $this->googleDriveApi->getWorksheets($fileRes['id']);
         $sheet = array_shift($sheets);
         // update file
         $file->setGoogleId($fileRes['id']);
         $file->setSheetId($sheet['wsid']);
         $this->logger->info("Sheet created", ['file' => $file->toArray()]);
     } else {
         if ($file->isOperationUpdate()) {
             if (null == $file->getSheetId()) {
                 // create new sheet in existing file
                 $response = $this->googleDriveApi->createWorksheet($file);
                 $crawler = new Crawler($response->getBody()->getContents());
                 $uriArr = explode('/', $crawler->filter('default|id')->text());
                 $file->setSheetId(array_pop($uriArr));
                 $this->logger->info("Sheet created in existing file", ['file' => $file->toArray()]);
             } else {
                 // update content of existing file
                 try {
                     // update metadata first - cols and rows count
                     $this->googleDriveApi->updateWorksheet($file);
                     $this->logger->debug("Worksheet metadata updated", ['file' => $file->toArray()]);
                     // update cells content
                     $timestart = microtime(true);
                     $status = $this->googleDriveApi->updateCells($file);
                     $timeend = microtime(true);
                     $apiCallDuration = $timeend - $timestart;
                     if (count($status['errors'])) {
                         $this->logger->warning("Some cells might not be imported properly", ['errors' => $status['errors']]);
                     }
                     $this->logger->debug("Cells updated", ['file' => $file->toArray(), 'apiCallDuration' => $apiCallDuration, 'status' => $status]);
                 } catch (RequestException $e) {
                     $statusCode = $e->getResponse()->getStatusCode();
                     if ($statusCode >= 500 && $statusCode < 600) {
                         throw new UserException(sprintf("Google Drive Server Error: %s - %s. Please try again later.", $statusCode, $e->getResponse()->getReasonPhrase()), $e, ['file' => $file->toArray()]);
                     }
                     throw new UserException("Cells update failed: " . $e->getMessage(), $e, ['file' => $file->toArray()]);
                 }
                 $this->logger->info("Sheet updated", ['file' => $file->toArray()]);
             }
         } else {
             // @TODO: append sheet
         }
     }
     return $file;
 }
Ejemplo n.º 3
0
 private function insertResumable(File $file)
 {
     $convert = $file->getType() == File::TYPE_SHEET ? 'true' : 'false';
     $title = $file->isOperationCreate() ? $file->getTitle() . ' (' . date('Y-m-d H:i:s') . ')' : $file->getTitle();
     $url = sprintf('%s?uploadType=resumable', self::FILE_UPLOAD);
     $body = ['name' => $title];
     if ($convert) {
         $body['mimeType'] = 'application/vnd.google-apps.spreadsheet';
     }
     if ($file->getTargetFolder()) {
         $url .= '&addParents=' . $file->getTargetFolder();
     }
     $response = $this->api->request($url, 'POST', ['Content-Type' => 'application/json; charset=UTF-8', 'Content-Length' => mb_strlen(serialize($body), '8bit'), 'X-Upload-Content-Type' => 'text/csv', 'X-Upload-Content-Length' => $file->getSize()], ['json' => $body]);
     $locationUri = $response->getHeaderLine('Location');
     return $this->putFile($file, $locationUri);
 }