diff --git a/Classes/Utility/PathUtility.php b/Classes/Utility/PathUtility.php index 4a0de9838471dae37ae464ea8cbfe84e59b595be..8cae1df30390f065f06b10a543cf53e65a7481ff 100644 --- a/Classes/Utility/PathUtility.php +++ b/Classes/Utility/PathUtility.php @@ -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 +} diff --git a/README.md b/README.md index c3e1c116f7953a1645fb3b551a848ce519e594e6..228c4049d37f88945fa5858515436632357b5818 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json index 1c10fbda9f256c0d93935e155f86acb262e51b78..04e565aa5d1fde46edabac301ddf465bc2e5eec0 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/ext_emconf.php b/ext_emconf.php index e0a4668640336ca0408e5033b384b0614afba002..8014b3160d017c5c89243b94da0c59911700622c 100644 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -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',