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": { 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 diff --git a/src/Flysystem/Adapter/FedoraAdapter.php b/src/Flysystem/Adapter/FedoraAdapter.php index 4aebde6e..783a9ed0 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. @@ -252,6 +253,32 @@ class FedoraAdapter implements AdapterInterface { $headers = [ 'Content-Type' => $this->mimeTypeGuesser->guess($path), ]; + if ($this->has($path)) { + $fedora_url = $path; + $date = new DateTime(); + $timestamp = $date->format("D, d M Y H:i:s O"); + // Create version in Fedora. + try { + $response = $this->fedora->createVersion( + $fedora_url, + $timestamp, + NULL, + $headers + ); + 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('fedora_flysystem')->error('Caught exception when creating version: ' . $e->getMessage() . "\n"); + } + } $response = $this->fedora->saveResource( $path, 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);