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

Media versioning #96

Merged
merged 28 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d46ce38
add separate routes in milliner for versioning nodes and media
elizoller Feb 21, 2020
620b5e4
separate createVersion for node and Media as much as possible, create…
elizoller Feb 21, 2020
e7338ce
remove excess log statements
elizoller Feb 21, 2020
6a65e3c
code cleanup
elizoller Feb 21, 2020
efd2203
fix node versioning - no gemini from the controller so made a helper…
elizoller Feb 21, 2020
419d1f3
adding and updating tests - still some of these fail
elizoller Feb 21, 2020
a5b717d
Bump symfony/http-foundation from 3.4.30 to 3.4.36 in /Hypercube (#92)
dependabot[bot] Mar 26, 2020
3909fd6
Bump symfony/http-foundation from 3.4.30 to 3.4.36 in /Milliner (#94)
dependabot[bot] Mar 26, 2020
cfaf024
Bump symfony/http-foundation from 3.4.30 to 3.4.36 in /Recast (#91)
dependabot[bot] Mar 26, 2020
8f54843
Bump symfony/http-foundation from 3.4.30 to 3.4.38 in /Gemini (#101)
dependabot[bot] Mar 26, 2020
f870cb1
Bump symfony/http-foundation from 3.4.30 to 3.4.38 in /Homarus (#98)
dependabot[bot] Mar 26, 2020
4252083
Update CONTRIBUTING.md (#104)
manez Aug 25, 2020
37b95bb
Update .travis.yml (#105)
dannylamb Aug 27, 2020
e93f569
Master -> Main Rename (#106)
dannylamb Aug 31, 2020
d694430
removing unnecessary logging
elizoller Sep 9, 2020
31367b2
update Milliner composer dependencies
elizoller Sep 9, 2020
6abec98
add separate routes in milliner for versioning nodes and media
elizoller Feb 21, 2020
fd24812
separate createVersion for node and Media as much as possible, create…
elizoller Feb 21, 2020
d11815f
remove excess log statements
elizoller Feb 21, 2020
7e23fbd
code cleanup
elizoller Feb 21, 2020
5737cc2
fix node versioning - no gemini from the controller so made a helper…
elizoller Feb 21, 2020
352043b
adding and updating tests - still some of these fail
elizoller Feb 21, 2020
9b4c607
removing unnecessary logging
elizoller Sep 9, 2020
257728c
update Milliner composer dependencies
elizoller Sep 9, 2020
54b1170
Merge branch 'media_versioning' of https://github.com/asulibraries/cr…
elizoller Sep 9, 2020
40b4e34
fix erroring test
elizoller Sep 9, 2020
853ecec
fix spacing in test
elizoller Sep 9, 2020
291ebe0
update tests - copy paste from @dannylamb
elizoller Sep 10, 2020
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
47 changes: 45 additions & 2 deletions Milliner/src/Controller/MillinerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,55 @@ public function saveExternal($uuid, Request $request)
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function createVersion($uuid, Request $request)
public function createNodeVersion($uuid, Request $request)
{
$token = $request->headers->get("Authorization", null);
try {
$this->log->info("in create Node version with " . $uuid);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would delete this or set the level to debug.

$urls = $this->milliner->getGeminiUrls($uuid, $token);
$this->log->info($urls);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would delete these two lines or set the levels to debug.

$this->log->info("fedora url is " . $urls['fedora']);
if (!empty($urls)) {
$fedora_url = $urls['fedora'];
$response = $this->milliner->createVersion(
$fedora_url,
$token
);
return new Response(
$response->getBody(),
$response->getStatusCode()
);
} else {
return new Response(404);
}
} catch (\Exception $e) {
$this->log->error("", ['Exception' => $e]);
$code = $e->getCode() == 0 ? 500 : $e->getCode();
return new Response($e->getMessage(), $code);
}
}

/**
* @param string $source_field
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function createMediaVersion($source_field, Request $request)
{
$token = $request->headers->get("Authorization", null);
$json_url = $request->headers->get("Content-Location");

if (empty($json_url)) {
$this->log->info("json url is EMPTY");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use error instead of info here

return new Response("Expected JSON url in Content-Location header", 400);
} else {
$this->log->info("json url is" . $json_url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make this debug or delete it

}
try {
$urls = $this->milliner->getFileFromMedia($source_field, $json_url, $token);
$fedora_file_url = $urls['fedora'];
$response = $this->milliner->createVersion(
$uuid,
$fedora_file_url,
$token
);
return new Response(
Expand Down
138 changes: 79 additions & 59 deletions Milliner/src/Service/MillinerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ protected function updateNode(
$fedora_url,
$token = null
) {

// Get the RDF from Fedora.
$headers = empty($token) ? [] : ['Authorization' => $token];
$headers['Accept'] = 'application/ld+json';
Expand Down Expand Up @@ -375,38 +376,10 @@ public function saveMedia(
$json_url,
$token = null
) {
// First get the media json from Drupal.
$headers = empty($token) ? [] : ['Authorization' => $token];
$drupal_response = $this->drupal->get(
$json_url,
['headers' => $headers]
);

$jsonld_url = $this->getLinkHeader($drupal_response, "alternate", "application/ld+json");

$media_json = json_decode(
$drupal_response->getBody(),
true
);

if (!isset($media_json[$source_field]) || empty($media_json[$source_field])) {
throw new \RuntimeException(
"Cannot parse file UUID from $json_url. Ensure $source_field exists on the media and is populated.",
500
);
}
$file_uuid = $media_json[$source_field][0]['target_uuid'];

// Get the file's LDP-NR counterpart in Fedora.
$urls = $this->gemini->getUrls($file_uuid, $token);
if (empty($urls)) {
$file_url = $media_json[$source_field][0]['url'];
throw new \RuntimeException(
"$file_url has not been mapped in Gemini with uuid $file_uuid",
404
);
}
$urls = $this->getFileFromMedia($source_field, $json_url, $token);
$fedora_file_url = $urls['fedora'];
$jsonld_url = $urls['jsonld'];

// Now look for the 'describedby' link header on the file in Fedora.
// I'm using the drupal http client because I have the full
Expand Down Expand Up @@ -554,39 +527,86 @@ public function saveExternal(
* {@inheritDoc}
*/
public function createVersion(
$uuid,
$fedora_url,
$token = null
) {
$urls = $this->gemini->getUrls($uuid, $token);
if (!empty($urls)) {
$fedora_url = $urls['fedora'];
$headers = empty($token) ? [] : ['Authorization' => $token];
$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
$this->log->error("the fedora url in service is " . $fedora_url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make this debug or delete it.

$headers = empty($token) ? [] : ['Authorization' => $token];
$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
);
$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
);
$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.
return $response;
} catch (Exception $e) {
$this->log->error('Caught exception when creating version: ', $e->getMessage(), "\n");
}
} else {
return new Response(404);
// Return the response from Fedora.
return $response;
} catch (Exception $e) {
$this->log->error('Caught exception when creating version: ', $e->getMessage(), "\n");
}
}

/**
* {@inheritDoc}
*/
public function getFileFromMedia(
$source_field,
$json_url,
$token = null
) {
// First get the media json from Drupal.
$headers = empty($token) ? [] : ['Authorization' => $token];
$drupal_response = $this->drupal->get(
$json_url,
['headers' => $headers]
);

$jsonld_url = $this->getLinkHeader($drupal_response, "alternate", "application/ld+json");
$media_json = json_decode(
$drupal_response->getBody(),
true
);

if (!isset($media_json[$source_field]) || empty($media_json[$source_field])) {
throw new \RuntimeException(
"Cannot parse file UUID from $json_url. Ensure $source_field exists on the media and is populated.",
500
);
}
$file_uuid = $media_json[$source_field][0]['target_uuid'];

// Get the file's LDP-NR counterpart in Fedora.
$urls = $this->gemini->getUrls($file_uuid, $token);
if (empty($urls)) {
$file_url = $media_json[$source_field][0]['url'];
throw new \RuntimeException(
"$file_url has not been mapped in Gemini with uuid $file_uuid",
404
);
}
return array('fedora'=>$urls['fedora'], 'jsonld' =>$jsonld_url, 'drupal'=>$urls['drupal']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate myself for nitpicking on this, but you should use [] instead of array here

}

/**
* {@inheritDoc}
*/
public function getGeminiUrls(
$uuid,
$token = null
) {
$urls = $this->gemini->getUrls($uuid, $token);
return $urls;
}
}
30 changes: 29 additions & 1 deletion Milliner/src/Service/MillinerServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,42 @@ public function saveExternal(
);

/**
* @param $uuid
* @param $fedora_url
* @param $token
*
* @throws \Exception
*
* @return \GuzzleHttp\Psr7\Response
*/
public function createVersion(
$fedora_url,
$token = null
);

/**
* @param $json_url
* @param $jsonld_url
* @param $token
*
* @throws \Exception
*
* @return array
*/
public function getFileFromMedia(
$source_field,
$json_url,
$token = null
);

/**
* @param $uuid
* @param $token
*
* @throws \Exception
*
* @return array
*/
public function getGeminiUrls(
$uuid,
$token = null
);
Expand Down
3 changes: 2 additions & 1 deletion Milliner/src/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@

$app->post('/node/{uuid}', "milliner.controller:saveNode");
$app->delete('/node/{uuid}', "milliner.controller:deleteNode");
$app->post('/node/{uuid}/version', "milliner.controller:createNodeVersion");
$app->post('/media/{source_field}', "milliner.controller:saveMedia");
$app->post('/media/{source_field}/version', 'milliner.controller:createMediaVersion');
$app->post('/external/{uuid}', "milliner.controller:saveExternal");
$app->post('/version/{uuid}', "milliner.controller:createVersion");

return $app;
59 changes: 3 additions & 56 deletions Milliner/tests/Islandora/Milliner/Tests/CreateVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testCreateVersionReturnsFedora201()
);

$response = $milliner->createVersion(
"9541c0c1-5bee-4973-a9d0-e55c1658bc81",
$mapping['fedora'],
"Bearer islandora"
);

Expand Down Expand Up @@ -145,7 +145,7 @@ public function testCreateVersionReturnsFedora404()
);

$response = $milliner->createVersion(
"9541c0c1-5bee-4973-a9d0-9998",
$mapping['fedora'],
"Bearer islandora"
);

Expand Down Expand Up @@ -206,7 +206,7 @@ public function testcreateVersionThrowsOnFedoraSaveError()
);

$response = $milliner->createVersion(
"9541c0c1-5bee-4973-a9d0-e55c1658bc81",
$mapping['fedora'],
"Bearer islandora"
);

Expand All @@ -216,57 +216,4 @@ public function testcreateVersionThrowsOnFedoraSaveError()
"Milliner must return 403 when Fedora returns 403. Received: $status"
);
}

/**
* @covers ::__construct
* @covers ::createVersion
* @expectedExceptionCode 404
*/

public function testCreateVersionReturns404IfNotInGemini()
{
$mapping = [];
$gemini = $this->prophesize(GeminiClient::class);
$gemini->getUrls(Argument::any(), Argument::any())
->willReturn($mapping);
$gemini->saveUrls(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->willReturn(true);
$gemini = $gemini->reveal();

$drupal_response = new Response(
200,
['Content-Type' => 'application/ld+json'],
file_get_contents(__DIR__ . '/../../../../static/Content.jsonld')
);
$drupal = $this->prophesize(Client::class);
$drupal->get(Argument::any(), Argument::any())
->willReturn($drupal_response);
$drupal = $drupal->reveal();

$fedora_response = new Response(404);
$fedora = $this->prophesize(IFedoraApi::class);
$fedora->createVersion(Argument::any(), Argument::any(), Argument::any(), Argument::any())
->willReturn($fedora_response);
$fedora = $fedora->reveal();

$milliner = new MillinerService(
$fedora,
$drupal,
$gemini,
$this->logger,
$this->modifiedDatePredicate,
false
);

$response = $milliner->createVersion(
"9541c0c1-5bee-4973-a9d0-9998",
"Bearer islandora"
);

$status = $response->getStatusCode();
$this->assertTrue(
$status == 404,
"Milliner must return 404 when not in Gemini. Received: $status"
);
}
}
Loading