Example #1
0
 /**
  * {@inheritdoc}
  */
 public function deleteTempFile($filename)
 {
     // Only delete files that were created by this manager.
     if (in_array($filename, $this->tempfiles)) {
         if (file_exists($filename)) {
             if (!$this->filesystem->unlink($filename)) {
                 throw new \Exception('Could not delete a temporary file.');
             }
         }
     }
 }
 /**
  * Tests upload() metadata sanitisation.
  *
  * @covers ::login
  * @covers ::upload
  * @covers ::doRequest
  *
  * @test
  */
 public function uploadCorrectlySantitisesMetadata()
 {
     $mock_login_response = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Response')->disableOriginalConstructor()->getMock();
     $mock_login_response->expects($this->once())->method('getStatusCode')->willReturn(200);
     $mock_login_response->expects($this->once())->method('getBody')->willReturn(file_get_contents('expected/login-expected-good-response.json', TRUE));
     // This sucks, returnValueMap wasn't working though.
     $this->client->expects($this->at(0))->method('request')->with('POST', self::EXAMPLE_LOGIN_URL, $this->defaultLoginOptions)->willReturn($mock_login_response);
     $mock_upload_response = $this->getMockBuilder('\\GuzzleHttp\\Psr7\\Response')->disableOriginalConstructor()->getMock();
     $mock_upload_response->expects($this->once())->method('getStatusCode')->willReturn(200);
     $mock_upload_response->expects($this->once())->method('getBody')->willReturn(file_get_contents('expected/upload-expected-good-response.json', TRUE));
     $expected_realpath = dirname(__FILE__) . '/expected/cat3.png';
     $mock_sourcepath = 'public://test123';
     $expected_filename = 'cat3.png';
     $unsanitised_metadata = ['is_string' => 'abc', 'is_integer' => 123, 'is_array' => ['a' => 1, 'b' => 2], 'is_object' => (object) ['a' => 1, 'b' => 2], 'libraries' => 101];
     $json_values_expected = ['description' => $expected_filename, 'is_string' => 'abc', 'is_integer' => 123, 'libraries' => '101'];
     $body = ['multipart' => [['name' => 'jsonrequest', 'contents' => $this->serializer->encode($json_values_expected)], ['name' => 'file', 'contents' => file_get_contents($expected_realpath), 'filename' => $expected_filename]]];
     $options = $this->defaultOptions + $body;
     $this->fileSystem->expects($this->once())->method('realpath')->with($mock_sourcepath)->willReturn($expected_realpath);
     $this->client->expects($this->at(1))->method('request')->with('POST', self::EXAMPLE_UPLOAD_URL, $options)->willReturn($mock_upload_response);
     /** @var \Drupal\embridge\EmbridgeAssetEntityInterface|\PHPUnit_Framework_MockObject_MockObject $mock_asset */
     $mock_asset = $this->getMockBuilder('\\Drupal\\embridge\\EmbridgeAssetEntityInterface')->disableOriginalConstructor()->getMock();
     $mock_asset->expects($this->once())->method('getSourcePath')->willReturn($mock_sourcepath);
     $mock_asset->expects($this->once())->method('getFileName')->willReturn($expected_filename);
     $this->emdbClient->upload($mock_asset, $unsanitised_metadata);
 }
  /**
   * @inheritdoc
   */
  public function populateWithFieldData(FillPdfFormInterface $pdf_form, array $field_mapping, array $context) {
    /** @var FileInterface $original_file */
    $original_file = File::load($pdf_form->file->target_id);
    $filename = $original_file->getFileUri();
    $fields = $field_mapping['fields'];

    module_load_include('inc', 'fillpdf', 'xfdf');
    $xfdfname = $filename . '.xfdf';
    $xfdf = create_xfdf(basename($xfdfname), $fields);
    // Generate the file
    $xfdffile = file_save_data($xfdf, $xfdfname, FILE_EXISTS_RENAME);

    // Now feed this to pdftk and save the result to a variable
    $path_to_pdftk = $this->getPdftkPath();
    ob_start();
    passthru($path_to_pdftk . ' ' . escapeshellarg($this->fileSystem->realpath($filename)) . ' fill_form ' . escapeshellarg($this->fileSystem->realpath($xfdffile->getFileUri())) . ' output - ' . ($context['flatten'] ? 'flatten ' : '') . 'drop_xfa');
    $data = ob_get_clean();
    if ($data === FALSE) {
      drupal_set_message($this->t('pdftk not properly installed. No PDF generated.'), 'error');
    }
    $xfdffile->delete();

    if ($data) {
      return $data;
    }

    return FALSE;
  }
  /**
   * @inheritdoc
   */
  public function populateWithFieldData(FillPdfFormInterface $pdf_form, array $field_mapping, array $context) {
    /** @var FileInterface $original_file */
    $original_file = File::load($pdf_form->file->target_id);
    $pdf_data = file_get_contents($original_file->getFileUri());
    $fields = $field_mapping['fields'];

    $require = drupal_get_path('module', 'fillpdf') . '/lib/JavaBridge/java/Java.inc';
    require_once DRUPAL_ROOT . '/' . $require;
    try {
      $fillpdf = new \java('com.ocdevel.FillpdfService', base64_encode($pdf_data), 'bytes');
      foreach ($fields as $key => $field) {
        if (substr($field, 0, 7) == '{image}') {
          // Remove {image} marker.
          $image_filepath = substr($field, 7);
          $image_realpath = $this->fileSystem->realpath($image_filepath);
          $fillpdf->image($key, $image_realpath, 'file');
        }
        else {
          $fillpdf->text($key, $field);
        }
      }
    }
    catch (\JavaException $e) {
      drupal_set_message(java_truncate((string) $e), 'error');
      return NULL;
    }
    try {
      if ($context['flatten']) {
        $populated_pdf = java_values(base64_decode($fillpdf->toByteArray()));
      }
      else {
        $populated_pdf = java_values(base64_decode($fillpdf->toByteArrayUnflattened()));
      }
    }
    catch (\JavaException $e) {
      drupal_set_message(java_truncate((string) $e), 'error');
      return NULL;
    }

    return $populated_pdf;
  }
 /**
  * {@inheritdoc}
  */
 public function upload(EmbridgeAssetEntityInterface $asset, array $metadata = [])
 {
     $this->login();
     $file_path = $this->fileSystem->realpath($asset->getSourcePath());
     $filename = $asset->getFilename();
     // Sanitize metadata values.
     $metadata = array_filter($metadata, 'is_scalar');
     if (isset($metadata['libraries'])) {
         // The libraries value won't stick unless it is a string.
         $metadata['libraries'] = (string) $metadata['libraries'];
     }
     // Build the main request data.
     $json_values = ['description' => $filename];
     $json_values = array_merge($json_values, $metadata);
     $json_request = $this->jsonEncoder->encode($json_values);
     $body = ['multipart' => [['name' => 'jsonrequest', 'contents' => $json_request], ['name' => 'file', 'contents' => file_get_contents($file_path), 'filename' => $filename]]];
     $response_body = $this->doRequest(self::EMBRIDGE_UPLOAD_PATH_DEFAULT, $body);
     $asset->setAssetId($response_body['data']['id']);
     $asset->setSourcePath($response_body['data']['sourcepath']);
     return $asset;
 }
Example #6
0
 /**
  * @covers ::uriScheme
  *
  * @dataProvider providerTestUriScheme
  */
 public function testUriScheme($uri, $expected)
 {
     $this->assertSame($expected, $this->fileSystem->uriScheme($uri));
 }