Skip to content
Snippets Groups Projects
Commit 4eff4bff authored by Fabian Galinski's avatar Fabian Galinski :pouting_cat:
Browse files

[TASK] Apply the image processing of TYPO3 for FileReferences

parent ca0e7add
No related branches found
Tags 1.0.5
No related merge requests found
......@@ -27,7 +27,10 @@ namespace SGalinski\SgRest\Utility;
***************************************************************/
use SGalinski\SgRest\Service\RegistrationService;
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
use TYPO3\CMS\Core\Resource\ProcessedFile;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
......@@ -164,21 +167,58 @@ class PathUtility {
$scheme = (GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://');
$host = GeneralUtility::getIndpEnv('HTTP_HOST') . '/';
$baseUrl = $scheme . $host;
$originalRessource = $file->getOriginalResource();
$originalResource = $file->getOriginalResource();
if ($baseUrl === '' && !$originalRessource) {
if ($baseUrl === '' && !$originalResource) {
$message = 'You are not allowed to access the file.';
throw new \InvalidArgumentException($message, 403);
}
$fileUrl = self::processFileReference($originalResource);
if (!$fileUrl) {
$fileUrl = $originalResource->getPublicUrl();
}
// remove leading and trailing slashes
$baseUrl = rtrim($baseUrl, '/');
$fileUrl = $originalRessource->getPublicUrl();
$fileUrl = ltrim($fileUrl, '/');
return $baseUrl . '/' . $fileUrl;
}
/**
* Returns the processed fileReference url for the given file, but just if the current TYPO3 version is 7.6 or
* above.
*
* @param \TYPO3\CMS\Core\Resource\FileReference $file
* @return string
*/
static protected function processFileReference(\TYPO3\CMS\Core\Resource\FileReference $file) {
$versionNumber = VersionNumberUtility::convertVersionNumberToInteger(
VersionNumberUtility::getNumericTypo3Version()
);
if ($versionNumber < 7006000) {
return '';
}
try {
$processingInstructions = [
'width' => '2048c',
];
return $file->getOriginalFile()->process(ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $processingInstructions)
->getPublicUrl();
} catch (ResourceDoesNotExistException $e) {
// thrown if file does not exist
} catch (\UnexpectedValueException $e) {
// thrown if a file has been replaced with a folder
} catch (\RuntimeException $e) {
// RuntimeException thrown if a file is outside of a storage
} catch (\InvalidArgumentException $e) {
// thrown if file storage does not exist
}
return '';
}
/**
* Return a basic api url like:
* https://{host}/{apiKey}/{entity}
......@@ -203,4 +243,4 @@ class PathUtility {
return $scheme . $host . implode('/', [$apiKey, $entity]);
}
}
\ No newline at end of file
}
......@@ -147,6 +147,31 @@ class <entityNameWithCamelcase>Controller extends AbstractRestController { // Ex
}
```
## Additional Registration Configuration
If you want to use POST/PUT/PATCH/DELETE request, then you need to add the additional configuration "httpPermissions".
```php
$class = 'SGalinski\SgRest\Service\RegistrationService';
$restRegistrationService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($class);
$restRegistrationService->registerAccessGroup(
<apiKey>, // Example: "news"
'SGalinski.<extension_key>', // Example: "SGalinski.sg_news"
<accessGroupName>, // Example: "News" It's the name of the api, which is shown in the user TCA. See: "REST Authentication"
[
<entityName> => [ // Example: "news"
'read' => 'uid, title' // This allows that the API can read the fields "uid" and "title" from the entity "news",
'httpPermissions' => [
'deleteForVerbs' => TRUE,
'putWithIdentifier' => TRUE,
'patchWithIdentifier' => TRUE,
'postWithIdentifier' => TRUE,
],
]
]
);
```
## Hints
1. Always use the function "$this->returnData" in your controller functions, to return the data.
......
......@@ -4,7 +4,7 @@
"description": "The extension provieds a basis REST environment. New endpoints provides a REST environment, so that other extensions only need to register them.",
"homepage": "https://www.sgalinski.de",
"license": "GPL-2.0+",
"version": "1.0.4",
"version": "1.0.5",
"require": {
"typo3/cms-core": ">=6.2.0,<8.0"
},
......
......@@ -19,7 +19,7 @@ $EM_CONF[$_EXTKEY] = array(
'modify_tables' => '',
'clearCacheOnLoad' => 0,
'lockType' => '',
'version' => '1.0.4',
'version' => '1.0.5',
'constraints' => array(
'depends' => array(
'typo3' => '6.2.0-7.6.99',
......
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