Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version files #793

Merged
merged 10 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions islandora.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ dependencies:
- content_translation
- flysystem
- token
- file_replace
27 changes: 27 additions & 0 deletions src/Flysystem/Adapter/FedoraAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\StreamWrapper;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
use DateTime;

/**
* Fedora adapter for Flysystem.
Expand Down Expand Up @@ -252,6 +253,32 @@ public function write($path, $contents, Config $config) {
$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,
Expand Down
15 changes: 15 additions & 0 deletions tests/src/Kernel/FedoraAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ protected function createAdapterForDirectory() {
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);

Expand Down Expand Up @@ -111,6 +115,9 @@ protected function createAdapterForWrite() {
*/
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);
Expand Down Expand Up @@ -562,6 +569,14 @@ public function testRename() {
$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);
Expand Down