Skip to content

Commit

Permalink
[MAINTENANCE] Extract load of IIIF resource to protected function (#1403
Browse files Browse the repository at this point in the history
)

Co-authored-by: Sebastian Meyer <[email protected]>
  • Loading branch information
beatrycze-volk and sebastian-meyer authored Dec 16, 2024
1 parent c6b4973 commit 3d09497
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
29 changes: 22 additions & 7 deletions Classes/Common/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,9 +545,6 @@ public static function &getInstance(string $location, array $settings = [], bool

// Try to load a file from the url
if (GeneralUtility::isValidUrl($location)) {
// Load extension configuration
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey);

$content = Helper::getUrl($location);
if ($content !== false) {
$xml = Helper::getXmlFileAsString($content);
Expand All @@ -560,10 +557,7 @@ public static function &getInstance(string $location, array $settings = [], bool
// Try to load file as IIIF resource instead.
$contentAsJsonArray = json_decode($content, true);
if ($contentAsJsonArray !== null) {
IiifHelper::setUrlReader(IiifUrlReader::getInstance());
IiifHelper::setMaxThumbnailHeight($extConf['iiif']['thumbnailHeight']);
IiifHelper::setMaxThumbnailWidth($extConf['iiif']['thumbnailWidth']);
$iiif = IiifHelper::loadIiifResource($contentAsJsonArray);
$iiif = self::loadIiifResource($contentAsJsonArray);
if ($iiif instanceof IiifResourceInterface) {
$documentFormat = 'IIIF';
}
Expand Down Expand Up @@ -911,6 +905,27 @@ protected function loadFormats(): void
}
}

/**
* Load IIIF resource from resource.
*
* @access protected
*
* @static
*
* @param string|array $resource IIIF resource. Can be an IRI, the JSON document as string
* or a dictionary in form of a PHP associative array
*
* @return NULL|\Ubl\Iiif\Presentation\Common\Model\AbstractIiifEntity An instance of the IIIF resource
*/
protected static function loadIiifResource($resource): mixed
{
$extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'iiif');
IiifHelper::setUrlReader(IiifUrlReader::getInstance());
IiifHelper::setMaxThumbnailHeight($extConf['thumbnailHeight']);
IiifHelper::setMaxThumbnailWidth($extConf['thumbnailWidth']);
return IiifHelper::loadIiifResource($resource);
}

/**
* Register all available namespaces for a \SimpleXMLElement object
*
Expand Down
12 changes: 2 additions & 10 deletions Classes/Common/IiifManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,11 +807,7 @@ protected function loadLocation(string $location): bool
{
$fileResource = GeneralUtility::getUrl($location);
if ($fileResource !== false) {
$conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'iiif');
IiifHelper::setUrlReader(IiifUrlReader::getInstance());
IiifHelper::setMaxThumbnailHeight($conf['thumbnailHeight']);
IiifHelper::setMaxThumbnailWidth($conf['thumbnailWidth']);
$resource = IiifHelper::loadIiifResource($fileResource);
$resource = self::loadIiifResource($fileResource);
if ($resource instanceof ManifestInterface) {
$this->iiif = $resource;
return true;
Expand Down Expand Up @@ -998,11 +994,7 @@ private function setFileUseFulltext(string $iiifId, $iiif): void
*/
public function __wakeup(): void
{
$conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'iiif');
IiifHelper::setUrlReader(IiifUrlReader::getInstance());
IiifHelper::setMaxThumbnailHeight($conf['thumbnailHeight']);
IiifHelper::setMaxThumbnailWidth($conf['thumbnailWidth']);
$resource = IiifHelper::loadIiifResource($this->asJson);
$resource = self::loadIiifResource($this->asJson);
if ($resource instanceof ManifestInterface) {
$this->asJson = '';
$this->iiif = $resource;
Expand Down
6 changes: 1 addition & 5 deletions Classes/Common/MetsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,7 @@ public function getDownloadLocation(string $id): string
$file = $this->getFileInfo($id);
if ($file['mimeType'] === 'application/vnd.kitodo.iiif') {
$file['location'] = (strrpos($file['location'], 'info.json') === strlen($file['location']) - 9) ? $file['location'] : (strrpos($file['location'], '/') === strlen($file['location']) ? $file['location'] . 'info.json' : $file['location'] . '/info.json');
$conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get(self::$extKey, 'iiif');
IiifHelper::setUrlReader(IiifUrlReader::getInstance());
IiifHelper::setMaxThumbnailHeight($conf['thumbnailHeight']);
IiifHelper::setMaxThumbnailWidth($conf['thumbnailWidth']);
$service = IiifHelper::loadIiifResource($file['location']);
$service = self::loadIiifResource($file['location']);
if ($service instanceof AbstractImageService) {
return $service->getImageUrl();
}
Expand Down

0 comments on commit 3d09497

Please sign in to comment.