-
Notifications
You must be signed in to change notification settings - Fork 29
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
Media versioning #96
Changes from 6 commits
d46ce38
620b5e4
e7338ce
6a65e3c
efd2203
419d1f3
a5b717d
3909fd6
cfaf024
8f54843
f870cb1
4252083
37b95bb
e93f569
d694430
31367b2
6abec98
fd24812
d11815f
7e23fbd
5737cc2
352043b
9b4c607
257728c
54b1170
40b4e34
853ecec
291ebe0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
$urls = $this->milliner->getGeminiUrls($uuid, $token); | ||
$this->log->info($urls); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hate myself for nitpicking on this, but you should use |
||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getGeminiUrls( | ||
$uuid, | ||
$token = null | ||
) { | ||
$urls = $this->gemini->getUrls($uuid, $token); | ||
return $urls; | ||
} | ||
} |
There was a problem hiding this comment.
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.