Skip to content
Snippets Groups Projects
Commit 3492a57c authored by Markus Guenther's avatar Markus Guenther
Browse files

[FEATURE] Enrich the file reference response with meta data

parent dbb6e141
No related branches found
Tags 1.6.0
No related merge requests found
......@@ -26,8 +26,9 @@ namespace SGalinski\SgRest\Service;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgImpulse\Domain\Model\Impulse;
use SGalinski\SgRest\Utility\PathUtility;
use TYPO3\CMS\Core\Resource\AbstractFile;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
......@@ -141,7 +142,8 @@ class DataResolveService implements SingletonInterface {
if ($object instanceof FileReference && $identifier) {
return [
'href' => PathUtility::createFileUrl($object),
'uid' => $identifier
'uid' => $identifier,
'metadata' => $this->getFileMetaData($object)
];
}
......@@ -264,4 +266,48 @@ class DataResolveService implements SingletonInterface {
return lcfirst(array_pop($classPath));
}
/**
* Returns an array with meta data of the file.
* - fileSize
* - mime type
* - name
*
* And for images also:
* - height
* - width
* - aspect ratio
*
* @param FileReference $fileReference
* @return array
*/
protected function getFileMetaData(FileReference $fileReference) {
$originalResource = $fileReference->getOriginalResource();
if (!$originalResource) {
return [];
}
/** @var File $originalFile */
$originalFile = $originalResource->getOriginalFile();
if (!$originalFile) {
return [];
}
$metadata = [
'fileSize' => $originalFile->getSize(),
'mimeType' => $originalFile->getMimeType(),
'name' => $originalFile->getName(),
];
if ($originalFile->getType() === AbstractFile::FILETYPE_IMAGE) {
$height = $originalFile->getProperty('height');
$width = $originalFile->getProperty('width');
$metadata['height'] = $height;
$metadata['width'] = $width;
$metadata['aspectRatio'] = $height / $width;
}
return $metadata;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment