Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Tepper committed Mar 15, 2021
0 parents commit 575e482
Show file tree
Hide file tree
Showing 36 changed files with 2,134 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
See https://www.coelnconcept.de/serviceportale/systeme/typo3/typo3-extensions-von-coeln-concept/cc-image/

1.1.1 2020-11-06 tda <info@coelnconcept.de>

* Fixed support for src attribute in legacy Picture partial for TYPO3 9.5 and 8.7.

1.1.0 2020-08-24 tda <info@coelnconcept.de>

* Fixed support for src attribute in Picture partial. Fixed WebP check.

1.0.2 2020-07-02 tda <info@coelnconcept.de>

* Clear processed file on WebP check.

1.0.1 2020-07-02 tda <info@coelnconcept.de>

* Initial release.
93 changes: 93 additions & 0 deletions Classes/DataProcessing/FilesProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
namespace CoelnConcept\CcImage\DataProcessing;

/***************************************************************
* Copyright notice
*
* (c) 2020 Coeln Concept GmbH
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
use TYPO3\CMS\Frontend\Resource\FileCollector;

/**
* This data processor can be used for processing data for record which contain
* relations to sys_file records (e.g. sys_file_reference records).
*
*
* Example TypoScript configuration:
*
* 10 = CoelnConcept\CcImage\DataProcessing\FilesProcessor
* 10 {
* filereferences {
* data = levelmedia:-1,slide
* }
* as = myfiles
* }
*
* whereas "myfiles" can further be used as a variable {myfiles} inside a Fluid template for iteration.
*/
class FilesProcessor implements DataProcessorInterface {

/**
* Process data of a record to resolve File objects to the view
*
* @param ContentObjectRenderer $cObj The data of the content element or page
* @param array $contentObjectConfiguration The configuration of Content Object
* @param array $processorConfiguration The configuration of this processor
* @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
* @return array the processed data as key/value store
*/
public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData) {
if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
return $processedData;
}

// gather data
/** @var FileCollector $fileCollector */
$fileCollector = GeneralUtility::makeInstance(FileCollector::class);

$filereferences = $cObj->stdWrapValue('filereferences', $processorConfiguration);
if ($filereferences) {
$filereferences = GeneralUtility::intExplode(',', $filereferences, true);
$fileCollector->addFileReferences($filereferences);
}

// make sure to sort the files
$sortingProperty = $cObj->stdWrapValue('sorting', $processorConfiguration);
if ($sortingProperty) {
$sortingDirection = $cObj->stdWrapValue(
'direction',
$processorConfiguration['sorting.'] ?? [],
'ascending'
);

$fileCollector->sort($sortingProperty, $sortingDirection);
}

// set the files into a variable, default "files"
$targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'filereferences');
$processedData[$targetVariableName] = $fileCollector->getFiles();

return $processedData;
}
}
70 changes: 70 additions & 0 deletions Classes/Utility/ConfigurationUtility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace CoelnConcept\CcImage\Utility;

/***************************************************************
*
* Copyright notice
*
* (c) 2020
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;

/**
* class providing configuration checks for webp support.
*/
class ConfigurationUtility {

/**
* Checks the support of webp by providing a webp-image.
*
* @param array $params Field information to be rendered
* @param mixed $pObj The calling parent object.
* @return array|string array with errorType and HTML or only the HTML as string
*/
public function checkWebp(array $params, $pObj) {

include_once(GeneralUtility::getFileAbsFileName('EXT:cc_image/ext_tables.php'));

/** @var \TYPO3\CMS\Core\Resource\ResourceFactory $resourceFactory **/
$resourceFactory = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
$testfile = $resourceFactory->retrieveFileOrFolderObject('EXT:cc_image/Resources/Public/Images/logo_coelnconcept.png');

if ($testfile) {
/** @var \TYPO3\CMS\Core\Resource\ProcessedFileRepository $processedFileRepository **/
$processedFileRepository = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\ProcessedFileRepository::class);
$processedFiles = $processedFileRepository->findAllByOriginalFile($testfile);
foreach ($processedFiles as $processedFile) {
$processedFile->delete();
}
}

$viewRootPath = GeneralUtility::getFileAbsFileName('EXT:cc_image/Resources/Private/Backend/');
/** @var StandaloneView::class $view **/
$view = GeneralUtility::makeInstance(StandaloneView::class);
$view->setTemplatePathAndFilename($viewRootPath . 'Templates/CheckWebp.html');
$view->setLayoutRootPaths([$viewRootPath . 'Layouts/']);
$view->setPartialRootPaths([$viewRootPath . 'Partials/']);

return $view->render();
}
}
81 changes: 81 additions & 0 deletions Classes/ViewHelpers/ArrayNextViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
namespace CoelnConcept\CcImage\ViewHelpers;

/***************************************************************
* Copyright notice
*
* (c) 2020 Coeln Concept GmbH
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;

/**
* Class ArrayNextViewHelper
*
* @package CoelnConcept\CcImage\ViewHelpers
*/
class ArrayNextViewHelper extends AbstractViewHelper {

use CompileWithContentArgumentAndRenderStatic;

/**
* @var boolean
*/
protected $escapeChildren = false;

/**
* @var boolean
*/
protected $escapeOutput = false;

/**
* @return void
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerArgument('subject', 'array', 'Traversable subject, array or \Traversable');
$this->registerArgument('key', 'string', 'current array key');
}

/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed next element
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {

/** @var $array \Traversable */
$subject = $renderChildrenClosure();

$array = is_object($subject)?iterator_to_array($subject, true):(array)$subject;
$keys = array_keys($array);

$pos = array_search($arguments['key'], $keys);
if ($pos === false) return null;

if (!array_key_exists($pos+1, $keys)) return null;
$next = $keys[$pos+1];

return $array[$next];
}
}
117 changes: 117 additions & 0 deletions Classes/ViewHelpers/Image/InlineSvgViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
namespace CoelnConcept\CcImage\ViewHelpers\Image;

/***************************************************************
* Copyright notice
*
* (c) 2020 Coeln Concept GmbH
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
use Exception;

/**
* Class InlineSvgViewHelper
* Returns the contents of an svg image as inline svg.
*
* @package CoelnConcept\CcImage\ViewHelpers\Image
*
* Examples
* ========
*
* Default
* -------
*
* ::
*
* <cc:image.inlineSvg src="EXT:cc_example/Resources/Public/logo_coelnconcept.svg" />
*
*/
class InlineSvgViewHelper extends AbstractTagBasedViewHelper
{
/**
* @var string
*/
protected $tagName = 'svg';

/**
* @var \TYPO3\CMS\Extbase\Service\ImageService
*/
protected $imageService;

/**
* @param \TYPO3\CMS\Extbase\Service\ImageService $imageService
*/
public function injectImageService(\TYPO3\CMS\Extbase\Service\ImageService $imageService)
{
$this->imageService = $imageService;
}

/**
* Initialize arguments.
*/
public function initializeArguments() {
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerArgument('src', 'string', 'a path to a file, a combined FAL identifier or an uid (int). If $treatIdAsReference is set, the integer is considered the uid of the sys_file_reference record. If you already got a FAL object, consider using the $image parameter instead', false, '');
$this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
$this->registerArgument('image', 'object', 'a FAL object');
}

/**
* Return svg as inline string
*
* @throws Exception
* @return string Rendered inline svg
*/
public function render() {
if (($this->arguments['src'] === null && $this->arguments['image'] === null) || ($this->arguments['src'] !== null && $this->arguments['image'] !== null)) {
throw new Exception('You must either specify a string src or a File object.', 1382284106);
}

try {
$image = $this->imageService->getImage($this->arguments['src'], $this->arguments['image'], $this->arguments['treatIdAsReference']);
} catch (ResourceDoesNotExistException $e) {
// thrown if file does not exist
throw new Exception($e->getMessage(), 1509741911, $e);
} catch (\UnexpectedValueException $e) {
// thrown if a file has been replaced with a folder
throw new Exception($e->getMessage(), 1509741912, $e);
} catch (\RuntimeException $e) {
// RuntimeException thrown if a file is outside of a storage
throw new Exception($e->getMessage(), 1509741913, $e);
} catch (\InvalidArgumentException $e) {
// thrown if file storage does not exist
throw new Exception($e->getMessage(), 1509741914, $e);
}

if ($image->getMimeType() != 'image/svg+xml') {
throw new Exception('Mimetype of image '.$image->getPublicUrl().' is not image/svg+xml but: '.$image->getMimeType(), 1593687938);
}

$svg = $image->getContents();

$matches = [];
preg_match('/(<svg.*\/svg>)/is', $svg, $matches);

return trim(preg_replace('/\\>\\s+\\</', '><',$matches[0]));
}
}
Loading

0 comments on commit 575e482

Please sign in to comment.