From 9b6a701a4a6a59d52b26fed755b35ea16bf484b5 Mon Sep 17 00:00:00 2001 From: Eli Zoller Date: Tue, 25 Feb 2020 10:21:41 -0700 Subject: [PATCH 1/9] version a replaced file --- src/Flysystem/Adapter/FedoraAdapter.php | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/Flysystem/Adapter/FedoraAdapter.php b/src/Flysystem/Adapter/FedoraAdapter.php index 4aebde6e..b9550162 100644 --- a/src/Flysystem/Adapter/FedoraAdapter.php +++ b/src/Flysystem/Adapter/FedoraAdapter.php @@ -11,6 +11,7 @@ use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\StreamWrapper; use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; +use \DateTime; /** * Fedora adapter for Flysystem. @@ -249,6 +250,39 @@ class FedoraAdapter implements AdapterInterface { * {@inheritdoc} */ public function write($path, $contents, Config $config) { + \Drupal::logger('flysystem adapter')->info('in write for flysystem'); + if ($this->has($path)){ + \Drupal::logger('flysystem adapter')->info('in write for flysystem and already exists in fedora'); + $fedora_url = $path; + $headers = []; + $date = new DateTime(); + $timestamp = $date->format("D, d M Y H:i:s O"); + // create version in Fedora. + try { + \Drupal::logger('flysystem')->info('right before make version ' . $status); + $response = $this->fedora->createVersion( + $fedora_url, + $timestamp, + null, + $headers + ); + $status = $response->getStatusCode(); + if (!in_array($status, [201])) { + $reason = $response->getReasonPhrase(); + throw new \RuntimeException( + "Client error: `POST $fedora_url` resulted in `$status $reason` response: " . + $response->getBody(), + $status + ); + } + // Return the response from Fedora. + \Drupal::logger('flysystem')->info('past making a version with status ' . $status); + // return $response; + } catch (Exception $e) { + \Drupal::logger('flysystem')->error('Caught exception when creating version: ', $e->getMessage(), "\n"); + } + } + $headers = [ 'Content-Type' => $this->mimeTypeGuesser->guess($path), ]; From 9a16c4373b80c0944163da0d00dd4383788dba58 Mon Sep 17 00:00:00 2001 From: Eli Zoller Date: Tue, 18 Aug 2020 05:38:30 -0700 Subject: [PATCH 2/9] php cleanup --- src/Flysystem/Adapter/FedoraAdapter.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Flysystem/Adapter/FedoraAdapter.php b/src/Flysystem/Adapter/FedoraAdapter.php index b9550162..ecc12093 100644 --- a/src/Flysystem/Adapter/FedoraAdapter.php +++ b/src/Flysystem/Adapter/FedoraAdapter.php @@ -11,7 +11,7 @@ use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\StreamWrapper; use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface; -use \DateTime; +use DateTime; /** * Fedora adapter for Flysystem. @@ -251,19 +251,19 @@ class FedoraAdapter implements AdapterInterface { */ public function write($path, $contents, Config $config) { \Drupal::logger('flysystem adapter')->info('in write for flysystem'); - if ($this->has($path)){ + if ($this->has($path)) { \Drupal::logger('flysystem adapter')->info('in write for flysystem and already exists in fedora'); $fedora_url = $path; $headers = []; $date = new DateTime(); $timestamp = $date->format("D, d M Y H:i:s O"); - // create version in Fedora. + // Create version in Fedora. try { - \Drupal::logger('flysystem')->info('right before make version ' . $status); + \Drupal::logger('flysystem')->info('right before make version ' . $fedora_url); $response = $this->fedora->createVersion( $fedora_url, $timestamp, - null, + NULL, $headers ); $status = $response->getStatusCode(); @@ -277,8 +277,8 @@ class FedoraAdapter implements AdapterInterface { } // Return the response from Fedora. \Drupal::logger('flysystem')->info('past making a version with status ' . $status); - // return $response; - } catch (Exception $e) { + } + catch (\Exception $e) { \Drupal::logger('flysystem')->error('Caught exception when creating version: ', $e->getMessage(), "\n"); } } From ea1383e4994bc5378ee67f912cab35d7a28d574b Mon Sep 17 00:00:00 2001 From: elizoller Date: Mon, 24 Aug 2020 16:25:48 +0000 Subject: [PATCH 3/9] add dependency on file_replace --- islandora.info.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/islandora.info.yml b/islandora.info.yml index 0e0a265e..f5da3c93 100644 --- a/islandora.info.yml +++ b/islandora.info.yml @@ -31,3 +31,4 @@ dependencies: - content_translation - flysystem - token + - file_replace From 219925831d8961e4edec6a498c12ee10dd2b39d5 Mon Sep 17 00:00:00 2001 From: elizoller Date: Mon, 24 Aug 2020 16:25:55 +0000 Subject: [PATCH 4/9] clean up logger --- src/Flysystem/Adapter/FedoraAdapter.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Flysystem/Adapter/FedoraAdapter.php b/src/Flysystem/Adapter/FedoraAdapter.php index ecc12093..53a73c5e 100644 --- a/src/Flysystem/Adapter/FedoraAdapter.php +++ b/src/Flysystem/Adapter/FedoraAdapter.php @@ -250,16 +250,14 @@ class FedoraAdapter implements AdapterInterface { * {@inheritdoc} */ public function write($path, $contents, Config $config) { - \Drupal::logger('flysystem adapter')->info('in write for flysystem'); if ($this->has($path)) { - \Drupal::logger('flysystem adapter')->info('in write for flysystem and already exists in fedora'); $fedora_url = $path; $headers = []; $date = new DateTime(); $timestamp = $date->format("D, d M Y H:i:s O"); // Create version in Fedora. try { - \Drupal::logger('flysystem')->info('right before make version ' . $fedora_url); + \Drupal::logger('flysystem')->info('Request being sent to Fedora to make a version for ' . $fedora_url); $response = $this->fedora->createVersion( $fedora_url, $timestamp, @@ -275,11 +273,9 @@ class FedoraAdapter implements AdapterInterface { $status ); } - // Return the response from Fedora. - \Drupal::logger('flysystem')->info('past making a version with status ' . $status); } catch (\Exception $e) { - \Drupal::logger('flysystem')->error('Caught exception when creating version: ', $e->getMessage(), "\n"); + \Drupal::logger('flysystem')->error('Caught exception when creating version: ' . $e->getMessage() . "\n"); } } @@ -404,3 +400,4 @@ class FedoraAdapter implements AdapterInterface { } } + From d1d15fe993b67831f1a0ec445032e92c0c89a97d Mon Sep 17 00:00:00 2001 From: elizoller Date: Mon, 24 Aug 2020 20:18:58 +0000 Subject: [PATCH 5/9] more cleanup of logging --- src/Flysystem/Adapter/FedoraAdapter.php | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Flysystem/Adapter/FedoraAdapter.php b/src/Flysystem/Adapter/FedoraAdapter.php index 53a73c5e..6946117b 100644 --- a/src/Flysystem/Adapter/FedoraAdapter.php +++ b/src/Flysystem/Adapter/FedoraAdapter.php @@ -250,39 +250,36 @@ class FedoraAdapter implements AdapterInterface { * {@inheritdoc} */ public function write($path, $contents, Config $config) { + $headers = [ + 'Content-Type' => $this->mimeTypeGuesser->guess($path), + ]; if ($this->has($path)) { $fedora_url = $path; - $headers = []; $date = new DateTime(); $timestamp = $date->format("D, d M Y H:i:s O"); // Create version in Fedora. try { - \Drupal::logger('flysystem')->info('Request being sent to Fedora to make a version for ' . $fedora_url); $response = $this->fedora->createVersion( $fedora_url, $timestamp, NULL, $headers ); - $status = $response->getStatusCode(); - if (!in_array($status, [201])) { - $reason = $response->getReasonPhrase(); - throw new \RuntimeException( - "Client error: `POST $fedora_url` resulted in `$status $reason` response: " . - $response->getBody(), - $status + if (isset($response) && $response->getStatusCode() == 201) { + \Drupal::logger('fedora_flysystem')->info('Created a version in Fedora for ' . $fedora_url); + } + else { + \Drupal::logger('fedora_flysystem')->error( + "Client error: `Failed to create a Fedora version of $fedora_url`. Response is " . print_r($response, TRUE) ); + } } catch (\Exception $e) { - \Drupal::logger('flysystem')->error('Caught exception when creating version: ' . $e->getMessage() . "\n"); + \Drupal::logger('fedora_flysystem')->error('Caught exception when creating version: ' . $e->getMessage() . "\n"); } } - $headers = [ - 'Content-Type' => $this->mimeTypeGuesser->guess($path), - ]; - $response = $this->fedora->saveResource( $path, $contents, From 316917a12c0cafceb2d757e382ecc6c68b0bb5be Mon Sep 17 00:00:00 2001 From: elizoller Date: Tue, 25 Aug 2020 21:20:05 +0000 Subject: [PATCH 6/9] improve header check in the case that it fails and update tests since they now make another fedora call --- src/Flysystem/Adapter/FedoraAdapter.php | 3 ++- tests/src/Kernel/FedoraAdapterTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Flysystem/Adapter/FedoraAdapter.php b/src/Flysystem/Adapter/FedoraAdapter.php index 6946117b..bd0a0331 100644 --- a/src/Flysystem/Adapter/FedoraAdapter.php +++ b/src/Flysystem/Adapter/FedoraAdapter.php @@ -253,7 +253,8 @@ class FedoraAdapter implements AdapterInterface { $headers = [ 'Content-Type' => $this->mimeTypeGuesser->guess($path), ]; - if ($this->has($path)) { + $heads_response = $this->fedora->getResourceHeaders($path); + if (isset($heads_response) && $heads_response->getStatusCode() == 200) { $fedora_url = $path; $date = new DateTime(); $timestamp = $date->format("D, d M Y H:i:s O"); diff --git a/tests/src/Kernel/FedoraAdapterTest.php b/tests/src/Kernel/FedoraAdapterTest.php index abff8eea..5d17814d 100644 --- a/tests/src/Kernel/FedoraAdapterTest.php +++ b/tests/src/Kernel/FedoraAdapterTest.php @@ -84,6 +84,10 @@ class FedoraAdapterTest extends IslandoraKernelTestBase { protected function createAdapterForWrite() { $fedora_prophecy = $this->prophesize(IFedoraApi::class); + $prophecy = $this->prophesize(Response::class); + $prophecy->getStatusCode()->willReturn(201); + $fedora_prophecy->createVersion('', Argument::any(), NULL, + Argument::any())->willReturn($prophecy->reveal()); $prophecy = $this->prophesize(Response::class); $prophecy->getStatusCode()->willReturn(201); @@ -111,6 +115,9 @@ class FedoraAdapterTest extends IslandoraKernelTestBase { */ protected function createAdapterForWriteFail() { $fedora_prophecy = $this->prophesize(IFedoraApi::class); + $prophecy = $this->prophesize(Response::class); + $prophecy->getStatusCode()->willReturn(500); + $fedora_prophecy->getResourceHeaders('')->willReturn($prophecy->reveal()); $prophecy = $this->prophesize(Response::class); $prophecy->getStatusCode()->willReturn(500); @@ -562,6 +569,14 @@ class FedoraAdapterTest extends IslandoraKernelTestBase { $prophecy->getStatusCode()->willReturn(201); $response = $prophecy->reveal(); + $date = new \DateTime(); + $timestamp = $date->format("D, d M Y H:i:s O"); + $fedora_prophecy->createVersion('', + Argument::any(), NULL, + Argument::any())->willReturn($prophecy->reveal()); + $prophecy = $this->prophesize(Response::class); + $prophecy->getStatusCode()->willReturn(201); + $fedora_prophecy->saveResource(Argument::any(), Argument::any(), Argument::any())->willReturn($response); $prophecy = $this->prophesize(Response::class); From 81c1ccc09c15e3470afc3ff0ff7f35d0cc8c57ed Mon Sep 17 00:00:00 2001 From: elizoller Date: Wed, 26 Aug 2020 18:01:15 +0000 Subject: [PATCH 7/9] update composer.json to include the file_replace module --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index c107fc22..db04a3ba 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ "drupal/migrate_source_csv" : "^2.1", "drupal/token" : "^1.3", "drupal/flysystem" : "^1.0", + "drupal/file_replace": "^1.1", "islandora/crayfish-commons": "dev-dev" }, "require-dev": { From dea2d6dda0e0c4ac1defb8850f96654bccf72c44 Mon Sep 17 00:00:00 2001 From: elizoller Date: Wed, 26 Aug 2020 18:19:24 +0000 Subject: [PATCH 8/9] fix phpcbf --- src/Flysystem/Adapter/FedoraAdapter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Flysystem/Adapter/FedoraAdapter.php b/src/Flysystem/Adapter/FedoraAdapter.php index bd0a0331..347fa72f 100644 --- a/src/Flysystem/Adapter/FedoraAdapter.php +++ b/src/Flysystem/Adapter/FedoraAdapter.php @@ -398,4 +398,3 @@ class FedoraAdapter implements AdapterInterface { } } - From df6e70e69eae25b2a2194cfd9c4a68b48d1fe6d1 Mon Sep 17 00:00:00 2001 From: elizoller Date: Thu, 27 Aug 2020 17:58:56 +0000 Subject: [PATCH 9/9] use existing has function --- src/Flysystem/Adapter/FedoraAdapter.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Flysystem/Adapter/FedoraAdapter.php b/src/Flysystem/Adapter/FedoraAdapter.php index 347fa72f..783a9ed0 100644 --- a/src/Flysystem/Adapter/FedoraAdapter.php +++ b/src/Flysystem/Adapter/FedoraAdapter.php @@ -253,8 +253,7 @@ class FedoraAdapter implements AdapterInterface { $headers = [ 'Content-Type' => $this->mimeTypeGuesser->guess($path), ]; - $heads_response = $this->fedora->getResourceHeaders($path); - if (isset($heads_response) && $heads_response->getStatusCode() == 200) { + if ($this->has($path)) { $fedora_url = $path; $date = new DateTime(); $timestamp = $date->format("D, d M Y H:i:s O");