Skip to content

Commit

Permalink
Use getFileInfo function for getting file url and mime type
Browse files Browse the repository at this point in the history
  • Loading branch information
beatrycze-volk committed Sep 27, 2023
1 parent b4fe188 commit dd2299e
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 51 deletions.
22 changes: 22 additions & 0 deletions Classes/Common/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @subpackage dlf
* @access public
* @property int $cPid This holds the PID for the configuration
* @property-read array $fileInfos Additional information about files (e.g., ADMID), indexed by ID.
* @property-read bool $hasFulltext Are there any fulltext files available?
* @property-read array $metadataArray This holds the documents' parsed metadata array
* @property-read int $numPages The holds the total number of pages
Expand Down Expand Up @@ -71,6 +72,14 @@ abstract class AbstractDocument
*/
public static $extKey = 'dlf';

/**
* Additional information about files (e.g., ADMID), indexed by ID.
*
* @var array
* @access protected
*/
protected $fileInfos = [];

/**
* This holds the configuration for all supported metadata encodings
* @see loadFormats()
Expand Down Expand Up @@ -365,6 +374,19 @@ protected abstract function getDocument();
*/
public abstract function getDownloadLocation($id);

/**
* This gets all file information stored in single array.
*
* @access public
*
* @abstract
*
* @param string $id The "@ID" attribute of the file node (METS) or the "@id" property of the IIIF resource
*
* @return array|null The set of file information
*/
public abstract function getFileInfo($id);

/**
* This gets the location of a file representing a physical page or track
*
Expand Down
17 changes: 17 additions & 0 deletions Classes/Common/IiifManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,23 @@ public function getDownloadLocation($id)
return $fileLocation;
}

/**
* {@inheritDoc}
* @see AbstractDocument::getFileInfo()
*/
public function getFileInfo($id)
{
if (empty($this->fileInfos[$id]['location'])) {
$this->fileInfos[$id]['location'] = $this->getFileLocation($id);
}

if (empty($this->fileInfos[$id]['mimeType'])) {
$this->fileInfos[$id]['mimeType'] = $this->getFileMimeType($id);
}

return $this->fileInfos[$id];
}

/**
* {@inheritDoc}
* @see AbstractDocument::getFileLocation()
Expand Down
64 changes: 30 additions & 34 deletions Classes/Common/MetsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
* @property-read array $mdSec Associative array of METS metadata sections indexed by their IDs.
* @property-read array $dmdSec Subset of `$mdSec` storing only the dmdSec entries; kept for compatibility.
* @property-read array $fileGrps This holds the file ID -> USE concordance
* @property-read array $fileInfos Additional information about files (e.g., ADMID), indexed by ID.
* @property-read bool $hasFulltext Are there any fulltext files available?
* @property-read array $metadataArray This holds the documents' parsed metadata array
* @property-read \SimpleXMLElement $mets This holds the XML file's METS part as \SimpleXMLElement object
Expand Down Expand Up @@ -131,16 +130,6 @@ final class MetsDocument extends AbstractDocument
*/
protected $fileGrpsLoaded = false;

/**
* Additional information about files (e.g., ADMID), indexed by ID.
* TODO: Consider using this for `getFileMimeType()` and `getFileLocation()`.
* @see _getFileInfos()
*
* @var array
* @access protected
*/
protected $fileInfos = [];

/**
* This holds the XML file's METS part as \SimpleXMLElement object
*
Expand Down Expand Up @@ -214,24 +203,42 @@ protected function establishRecordId($pid)
*/
public function getDownloadLocation($id)
{
$fileMimeType = $this->getFileMimeType($id);
$fileLocation = $this->getFileLocation($id);
if ($fileMimeType === 'application/vnd.kitodo.iiif') {
$fileLocation = (strrpos($fileLocation, 'info.json') === strlen($fileLocation) - 9) ? $fileLocation : (strrpos($fileLocation, '/') === strlen($fileLocation) ? $fileLocation . 'info.json' : $fileLocation . '/info.json');
$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);
IiifHelper::setUrlReader(IiifUrlReader::getInstance());
IiifHelper::setMaxThumbnailHeight($conf['iiifThumbnailHeight']);
IiifHelper::setMaxThumbnailWidth($conf['iiifThumbnailWidth']);
$service = IiifHelper::loadIiifResource($fileLocation);
$service = IiifHelper::loadIiifResource($file['location']);
if ($service !== null && $service instanceof AbstractImageService) {
return $service->getImageUrl();
}
} elseif ($fileMimeType === 'application/vnd.netfpx') {
$baseURL = $fileLocation . (strpos($fileLocation, '?') === false ? '?' : '');
} elseif ($file['mimeType'] === 'application/vnd.netfpx') {
$baseURL = $file['location'] . (strpos($file['location'], '?') === false ? '?' : '');
// TODO CVT is an optional IIP server capability; in theory, capabilities should be determined in the object request with '&obj=IIP-server'
return $baseURL . '&CVT=jpeg';
}
return $fileLocation;
return $file['location'];
}

/**
* {@inheritDoc}
* @see AbstractDocument::getFileInfo()
*/
public function getFileInfo($id)
{
$this->_getFileGrps();

if (isset($this->fileInfos[$id]) && empty($this->fileInfos[$id]['location'])) {
$this->fileInfos[$id]['location'] = $this->getFileLocation($id);
}

if (isset($this->fileInfos[$id]) && empty($this->fileInfos[$id]['mimeType'])) {
$this->fileInfos[$id]['mimeType'] = $this->getFileMimeType($id);
}

return $this->fileInfos[$id];
}

/**
Expand Down Expand Up @@ -658,7 +665,7 @@ protected function getMetadataIds($id)
{
// Load amdSecChildIds concordance
$this->_getMdSec();
$this->_getFileInfos();
$fileInfo = $this->getFileInfo($id);

// Get DMDID and ADMID of logical structure node
if (!empty($this->logicalUnits[$id])) {
Expand All @@ -669,9 +676,9 @@ protected function getMetadataIds($id)
if ($mdSec) {
$dmdIds = (string) $mdSec->attributes()->DMDID;
$admIds = (string) $mdSec->attributes()->ADMID;
} else if (isset($this->fileInfos[$id])) {
$dmdIds = $this->fileInfos[$id]['dmdId'];
$admIds = $this->fileInfos[$id]['admId'];
} else if (isset($fileInfo)) {
$dmdIds = $fileInfo['dmdId'];
$admIds = $fileInfo['admId'];
} else {
$dmdIds = '';
$admIds = '';
Expand Down Expand Up @@ -961,17 +968,6 @@ protected function _getFileGrps()
return $this->fileGrps;
}

/**
*
* @access protected
* @return array
*/
protected function _getFileInfos()
{
$this->_getFileGrps();
return $this->fileInfos;
}

/**
* {@inheritDoc}
* @see AbstractDocument::prepareMetadataArray()
Expand Down
12 changes: 7 additions & 5 deletions Classes/Controller/AudioPlayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ public function mainAction()
// Check if there are any audio files available.
$fileGrpsAudio = GeneralUtility::trimExplode(',', $this->extConf['fileGrpAudio']);
while ($fileGrpAudio = array_shift($fileGrpsAudio)) {
$fileGroupAudio = $this->document->getCurrentDocument()->physicalStructureInfo[$this->document->getCurrentDocument()->physicalStructure[$this->requestData['page']]]['files'][$fileGrpAudio];
if (!empty($fileGroupAudio)) {
$physicalStructureInfo = $this->document->getCurrentDocument()->physicalStructureInfo[$this->document->getCurrentDocument()->physicalStructure[$this->requestData['page']]];
$fileId = $physicalStructureInfo['files'][$fileGrpAudio];
if (!empty($fileId)) {
// Get audio data.
$this->audio['url'] = $this->document->getCurrentDocument()->getFileLocation($fileGroupAudio);
$this->audio['label'] = $this->document->getCurrentDocument()->physicalStructureInfo[$this->document->getCurrentDocument()->physicalStructure[$this->requestData['page']]]['label'];
$this->audio['mimetype'] = $this->document->getCurrentDocument()->getFileMimeType($fileGroupAudio);
$file = $this->document->getCurrentDocument()->getFileInfo($fileId);
$this->audio['url'] = $file['location'];
$this->audio['label'] = $physicalStructureInfo['label'];
$this->audio['mimetype'] = $file['mimeType'];
break;
}
}
Expand Down
22 changes: 13 additions & 9 deletions Classes/Controller/PageViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,15 @@ protected function getFulltext($page)
// Get fulltext link.
$fileGrpsFulltext = GeneralUtility::trimExplode(',', $this->extConf['fileGrpFulltext']);
while ($fileGrpFulltext = array_shift($fileGrpsFulltext)) {
$fileGroupFulltext = $this->document->getCurrentDocument()->physicalStructureInfo[$this->document->getCurrentDocument()->physicalStructure[$page]]['files'][$fileGrpFulltext];
if (!empty($fileGroupFulltext)) {
$fulltext['url'] = $this->document->getCurrentDocument()->getFileLocation($fileGroupFulltext);
$physicalStructureInfo = $this->document->getCurrentDocument()->physicalStructureInfo[$this->document->getCurrentDocument()->physicalStructure[$page]];
$fileId = $physicalStructureInfo['files'][$fileGrpFulltext];
if (!empty($fileId)) {
$file = $this->document->getCurrentDocument()->getFileLocation($fileId);
$fulltext['url'] = $file['location'];
if ($this->settings['useInternalProxy']) {
$this->configureProxyUrl($fulltext['url']);
}
$fulltext['mimetype'] = $this->document->getCurrentDocument()->getFileMimeType($fileGroupFulltext);
$fulltext['mimetype'] = $file['mimeType'];
break;
} else {
$this->logger->notice('No full-text file found for page "' . $page . '" in fileGrp "' . $fileGrpFulltext . '"');
Expand Down Expand Up @@ -229,13 +231,15 @@ protected function getImage($page)
$fileGrpsImages = GeneralUtility::trimExplode(',', $this->extConf['fileGrpImages']);
while ($fileGrpImages = array_pop($fileGrpsImages)) {
// Get image link.
$fileGroupImage = $this->document->getCurrentDocument()->physicalStructureInfo[$this->document->getCurrentDocument()->physicalStructure[$page]]['files'][$fileGrpImages];
if (!empty($fileGroupImage)) {
$image['url'] = $this->document->getCurrentDocument()->getFileLocation($fileGroupImage);
$image['mimetype'] = $this->document->getCurrentDocument()->getFileMimeType($fileGroupImage);
$physicalStructureInfo = $this->document->getCurrentDocument()->physicalStructureInfo[$this->document->getCurrentDocument()->physicalStructure[$page]];
$fileId = $physicalStructureInfo['files'][$fileGrpImages];
if (!empty($fileId)) {
$file = $this->document->getCurrentDocument()->getFileInfo($fileId);
$image['url'] = $file['location'];
$image['mimetype'] = $file['mimeType'];

// Only deliver static images via the internal PageViewProxy.
// (For IIP and IIIF, the viewer needs to build and access a separate metadata URL, see `getMetdadataURL` in `OLSources.js`.)
// (For IIP and IIIF, the viewer needs to build and access a separate metadata URL, see `getMetadataURL` in `OLSources.js`.)
if ($this->settings['useInternalProxy'] && !str_contains(strtolower($image['mimetype']), 'application')) {
$this->configureProxyUrl($image['url']);
}
Expand Down
7 changes: 4 additions & 3 deletions Classes/Controller/ToolboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,11 @@ private function getImage($page)
$fileGrps = GeneralUtility::trimExplode(',', $this->settings['fileGrpsImageDownload']);
while ($fileGrp = @array_pop($fileGrps)) {
// Get image link.
$fileGroup = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]]['files'][$fileGrp];
$physicalStructureInfo = $this->doc->physicalStructureInfo[$this->doc->physicalStructure[$page]];
$fileId = $physicalStructureInfo['files'][$fileGrp];
if (!empty($fileGroup)) {
$image['url'] = $this->doc->getDownloadLocation($fileGroup);
$image['mimetype'] = $this->doc->getFileMimeType($fileGroup);
$image['url'] = $this->doc->getDownloadLocation($fileId);
$image['mimetype'] = $this->doc->getFileMimeType($fileId);
switch ($image['mimetype']) {
case 'image/jpeg':
$image['mimetypeLabel'] = ' (JPG)';
Expand Down

0 comments on commit dd2299e

Please sign in to comment.