From e25570a882d6b439a1f705d9747a9095438f7a48 Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Thu, 21 Jul 2016 19:42:34 +0200 Subject: [PATCH 1/2] Leverage the streaming support, even if its not real streaming yet --- src/EncryptStreamWrapper.php | 58 +++++++++++++----------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/src/EncryptStreamWrapper.php b/src/EncryptStreamWrapper.php index 9769bbd..79db574 100644 --- a/src/EncryptStreamWrapper.php +++ b/src/EncryptStreamWrapper.php @@ -7,6 +7,7 @@ use Drupal\Core\StreamWrapper\StreamWrapperInterface; use Drupal\Core\Url; use Drupal\encrypt\EncryptionProfileInterface; +use Drupal\encrypt\Stream\NoStreaming; /** * Provides a scheme wrapper which encrypts / decrypts automatically. @@ -88,37 +89,11 @@ public static function basePath() { } /** - * Decrypts a given file. - * - * @param string $raw_file - * The encrypted content of the raw file. - * @param \Drupal\encrypt\EncryptionProfileInterface $encryption_profile - * The used encryption profile. - * - * @return string - * The decrypted string. + * @return \Drupal\encrypt\Stream\EncryptStreamInterface */ - protected function decrypt($raw_file, EncryptionProfileInterface $encryption_profile) { - /** @var \Drupal\encrypt\EncryptService $encryption */ - $encryption = \Drupal::service('encryption'); - return $encryption->decrypt($raw_file, $encryption_profile); - } - - /** - * Encrypts a given file. - * - * @param string $raw_file - * The descrypted content of the raw file. - * @param \Drupal\encrypt\EncryptionProfileInterface $encryption_profile - * The used encryption profile. - * - * @return string - * The encrypted string. - */ - protected function encrypt($raw_file, EncryptionProfileInterface $encryption_profile) { - /** @var \Drupal\encrypt\EncryptService $encryption */ - $encryption = \Drupal::service('encryption'); - return $encryption->encrypt($raw_file, $encryption_profile); + protected function getStreamEncryption() { + // @todo Use the swappabilty in the encrypt module, once its there. + return new NoStreaming(\Drupal::service('encryption')); } /** @@ -157,12 +132,15 @@ public function stream_open($uri, $mode, $options, &$opened_path) { $this->handle = fopen('php://memory', 'w+b'); // If file exists, decrypt and load it into memory. if (file_exists($path)) { - $raw_file = file_get_contents($path); - $decrypted_file = $this->decrypt($raw_file, $encryption_profile); - $this->setFileInfo($decrypted_file, $uri); + // Write to memory. - fwrite($this->handle, $decrypted_file); + $encrypted_resource = fopen($path, 'r'); + $this->getStreamEncryption()->decrypt($encrypted_resource, $this->handle, $encryption_profile); + fclose($encrypted_resource); rewind($this->handle); + + // @todo What do we do with this ... + // $this->setFileInfo($decrypted_file, $uri); } // Set $opened_path. if ((bool) $this->handle && $options & STREAM_USE_PATH) { @@ -202,10 +180,14 @@ public function stream_close() { // Encrypt file and save. rewind($this->handle); - $file_contents = stream_get_contents($this->handle); - file_put_contents($this->getLocalPath(), $this->encrypt($file_contents, $encryption_profile)); - // Store important file info. - $this->setFileInfo($file_contents, $this->uri); + + $output_resource = fopen($this->getLocalPath(), 'w+'); + $this->getStreamEncryption()->encrypt($this->handle, $output_resource, $encryption_profile); + fclose($output_resource); + + // @todo what do we do with this. + // $this->setFileInfo($file_contents, $this->uri); + // Close handle and reset manual key. fclose($this->handle); } From f10cf4ace34bb4c1ad513f0e7f58a4949789d532 Mon Sep 17 00:00:00 2001 From: Daniel Wehner Date: Thu, 21 Jul 2016 19:50:20 +0200 Subject: [PATCH 2/2] leverage the swappablity --- src/EncryptStreamWrapper.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/EncryptStreamWrapper.php b/src/EncryptStreamWrapper.php index 79db574..7cc6055 100644 --- a/src/EncryptStreamWrapper.php +++ b/src/EncryptStreamWrapper.php @@ -89,11 +89,14 @@ public static function basePath() { } /** + * Gets an instance of the encryption stream. + * * @return \Drupal\encrypt\Stream\EncryptStreamInterface + * The stream interface. */ - protected function getStreamEncryption() { - // @todo Use the swappabilty in the encrypt module, once its there. - return new NoStreaming(\Drupal::service('encryption')); + protected function getStreamEncryption(EncryptionProfileInterface $encryption_profile) { + $encryption_method = $encryption_profile->getEncryptionMethod(); + return $encryption_method->getStreamingInstance(); } /** @@ -135,7 +138,7 @@ public function stream_open($uri, $mode, $options, &$opened_path) { // Write to memory. $encrypted_resource = fopen($path, 'r'); - $this->getStreamEncryption()->decrypt($encrypted_resource, $this->handle, $encryption_profile); + $this->getStreamEncryption($encryption_profile)->decrypt($encrypted_resource, $this->handle, $encryption_profile); fclose($encrypted_resource); rewind($this->handle); @@ -182,7 +185,7 @@ public function stream_close() { rewind($this->handle); $output_resource = fopen($this->getLocalPath(), 'w+'); - $this->getStreamEncryption()->encrypt($this->handle, $output_resource, $encryption_profile); + $this->getStreamEncryption($encryption_profile)->encrypt($this->handle, $output_resource, $encryption_profile); fclose($output_resource); // @todo what do we do with this.