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;
 }
Пример #3
0
 public function remoteFileExists(File $file)
 {
     if ($file->getGoogleId() == null) {
         return false;
     }
     try {
         $remoteFile = $this->getFile($file->getGoogleId());
         if ($remoteFile['trashed'] === true) {
             return false;
         }
         return true;
     } catch (BadResponseException $e) {
         if ($e->getResponse()->getStatusCode() == 404) {
             return false;
         }
         throw $e;
     }
 }
 public function testGetRemoteFile()
 {
     $this->createConfig();
     $this->createAccount();
     $file = new File(['id' => 0, 'title' => 'Test Sheet', 'tableId' => $this->tableId, 'targetFolder' => '0B8ceg4OWLR3lelQzMm9pcDEyNHc', 'type' => 'sheet']);
     // add files to config
     $this->configuration->addFile($this->accountId, $file->toArray());
     // run
     $job = $this->processJob($this->componentName . '/run');
     $this->assertEquals('success', $job->getStatus());
     $files = $this->configuration->getFiles($this->accountId);
     /** @var File $file */
     $file = array_shift($files);
     // call the API
     $this->httpClient->restart();
     $this->httpClient->request('GET', sprintf('%s/remote-file/%s/%s', $this->componentName, $this->accountId, $file->getGoogleId()));
     $response = $this->httpClient->getResponse();
     $body = json_decode($response->getContent(), true);
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertArrayHasKey('id', $body);
     $this->assertArrayHasKey('name', $body);
     $this->assertArrayHasKey('title', $body);
     $this->assertArrayHasKey('parents', $body);
     $this->assertArrayHasKey('mimeType', $body);
     $this->assertArrayHasKey('trashed', $body);
     $this->assertArrayHasKey('alternateLink', $body);
     $this->assertArrayHasKey('webViewLink', $body);
     $this->assertNotEmpty($body['id']);
     $this->assertNotEmpty($body['name']);
     $this->assertNotEmpty($body['title']);
     $this->assertNotEmpty($body['alternateLink']);
     $this->assertNotEmpty($body['webViewLink']);
 }
 public function getCellsFeed(File $file)
 {
     return json_decode($this->api->request(sprintf(self::SPREADSHEET_CELL, $file->getGoogleId(), $file->getSheetId()) . '?alt=json', 'GET', ['Accept' => 'application/json', 'Content-Type' => 'application/atom+xml', 'GData-Version' => '3.0'])->getBody(), true);
 }
 public function execute(Job $job)
 {
     $this->configuration->setStorageApi($this->storageApi);
     $accounts = $this->configuration->getAccounts();
     $options = $job->getParams();
     $status = [];
     if (isset($options['external'])) {
         // load files by tag from SAPI
         if (!isset($options['external']['account'])) {
             throw new UserException("Missing field 'account'");
         }
         try {
             $this->configuration->create();
         } catch (\Exception $e) {
             // create configuration if not exists
         }
         $account = new Account($this->configuration, uniqid('external'));
         $account->fromArray($options['external']['account']);
         $writer = $this->writerFactory->create($account);
         if (!isset($options['external']['query'])) {
             throw new UserException("Missing field 'query'");
         }
         $queryString = $options['external']['query'];
         $queryString .= ' -tags:wr-google-drive-processed';
         if (isset($options['external']['filterByRunId']) && $options['external']['filterByRunId']) {
             $parentRunId = $this->getParentRunId($job->getRunId());
             if ($parentRunId) {
                 $queryString .= ' +tags:runId-' . $parentRunId;
             }
         }
         $uploadedFiles = $this->storageApi->listFiles((new ListFilesOptions())->setQuery($queryString));
         if (empty($uploadedFiles)) {
             throw new UserException("No file matches your query '" . $queryString . "'");
         }
         foreach ($uploadedFiles as $uploadedFile) {
             $tmpFile = $this->temp->createTmpFile('wr-gd');
             file_put_contents($tmpFile->getPathname(), fopen($uploadedFile['url'], 'r'));
             $file = new File(['id' => $uploadedFile['id'], 'title' => $uploadedFile['name'], 'targetFolder' => isset($options['external']['targetFolder']) ? $options['external']['targetFolder'] : null, 'type' => File::TYPE_FILE, 'pathname' => $tmpFile, 'size' => $uploadedFile['sizeBytes']]);
             $gdFiles = $writer->listFiles(['q' => "trashed=false and name='" . $uploadedFile['name'] . "'"]);
             if (!empty($gdFiles['files'])) {
                 $lastGdFile = array_shift($gdFiles['files']);
                 $file->setGoogleId($lastGdFile['id']);
             }
             $file = $writer->process($file);
             // tag file 'wr-google-drive-processed'
             $this->storageApi->addFileTag($uploadedFile['id'], 'wr-google-drive-processed');
         }
         // delete temporary account
         $this->configuration->removeAccount($account->getAccountId());
     } else {
         $fileFilter = null;
         if (isset($options['config'])) {
             if (!isset($accounts[$options['config']])) {
                 throw new UserException("Config '" . $options['config'] . "' does not exist.");
             }
             $accounts = [$options['config'] => $accounts[$options['config']]];
             if (isset($options['file'])) {
                 /** @var Account $account */
                 $account = $accounts[$options['config']];
                 if (null == $account->getFile($options['file'])) {
                     throw new UserException("File '" . $options['file'] . "' not found");
                 }
                 $fileFilter = $options['file'];
             }
         }
         /** @var Account $account */
         foreach ($accounts as $accountId => $account) {
             $writer = $this->writerFactory->create($account);
             $files = $account->getFiles();
             /** @var File $file */
             foreach ($files as $file) {
                 if ($fileFilter != null && $file->getId() != $fileFilter) {
                     continue;
                 }
                 $file->setPathname($this->temp->createTmpFile()->getPathname());
                 try {
                     $tableExporter = new TableExporter($this->storageApi);
                     $tableExporter->exportTable($file->getTableId(), $file->getPathname(), []);
                 } catch (ClientException $e) {
                     throw new UserException($e->getMessage(), $e, ['file' => $file->toArray()]);
                 }
                 $file = $writer->process($file);
                 $status[$account->getAccountName()][$file->getTitle()] = 'ok';
             }
             // updated changes to files
             $account->save();
         }
     }
     return $status;
 }