Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 28 additions & 54 deletions Classes/Command/TemporaryFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@
namespace Fab\MediaUpload\Command;

use Fab\MediaUpload\FileUpload\UploadManager;
use Symfony\Component\Console\Input\InputOption;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Exception;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -19,90 +15,69 @@
/**
* Class TemporaryFileCommand
* @author Jörg Velletti <typo3@velletti.de>
* @package Fab\MediaUpload\Command;
* @package Fab\MediaUpload\Command
*/
class TemporaryFileCommand extends Command {

/**
* @var array
*/
private $allowedTables = [] ;

/**
* @var array
*/
private $extConf = [] ;



class TemporaryFileCommand extends Command
{
/**
* Configure the command by defining the name, options and arguments
*/
protected function configure()
protected function configure(): void
{
$this->setDescription('Remove temporarely files from Media Upload.')
->setHelp('Get list of Options: .' . LF . 'use the --help option.')
$this->setDescription('Remove temporary files from Media Upload.')
->setHelp('Get list of Options: ' . PHP_EOL . 'use the --help option.')
->addArgument(
'rundry',
InputArgument::OPTIONAL,
'if rundry is given, will only List files ',
'0' );
'0'
);
}

/**
* Executes the current command.
*
* This method is not abstract because you can use this class
* as a concrete class. In this case, instead of defining the
* execute() method, you set the code to execute by passing
* a Closure to the setCode() method.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int 0 if everything went fine, or an exit code
*
* @see setCode()
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{

$io = new SymfonyStyle($input, $output);
$io->title($this->getDescription());
// Bootstrap::initializeBackendAuthentication();
$structure = $this->getStructureOfFiles();

$structure = $this->getStructureOfFiles();

if ($input->getArgument('rundry') ) {
$io->writeln('Argument rundry given. Only list temp files ' );
$io->writeln('' );
if( is_array($structure['files'])) {
$io->writeln( implode(PHP_EOL, $structure['files']) );
$io->writeln('' );
if ($input->getArgument('rundry')) {
$io->writeln('Argument rundry given. Only list temp files');
$io->writeln('');
if (is_array($structure['files'])) {
$io->writeln(implode(PHP_EOL, $structure['files']));
$io->writeln('');
}
$io->writeln(sprintf('%s temporary file(s).', $structure['numberOfFiles']) );
return 0 ;
$io->writeln(sprintf('%s temporary file(s).', $structure['numberOfFiles']));
return Command::SUCCESS;
} else {
GeneralUtility::rmdir(GeneralUtility::getFileAbsFileName(UploadManager::UPLOAD_FOLDER), true);
GeneralUtility::mkdir_deep(GeneralUtility::getFileAbsFileName(UploadManager::UPLOAD_FOLDER));
$io->writeln(sprintf(sprintf('I have removed %s file(s).', $structure['numberOfFiles']) ) );
return 0 ;
$io->writeln(sprintf('I have removed %s file(s).', $structure['numberOfFiles']));
return Command::SUCCESS;
}

}

/**
* @return array
*/
protected function getStructureOfFiles()
protected function getStructureOfFiles(): array
{
if( !is_dir(GeneralUtility::getFileAbsFileName(UploadManager::UPLOAD_FOLDER))) {
mkdir(GeneralUtility::getFileAbsFileName(UploadManager::UPLOAD_FOLDER)) ;
$uploadFolderPath = GeneralUtility::getFileAbsFileName(UploadManager::UPLOAD_FOLDER);

if (!is_dir($uploadFolderPath)) {
mkdir($uploadFolderPath, 0755, true);
}
$Directory = new RecursiveDirectoryIterator(GeneralUtility::getFileAbsFileName(UploadManager::UPLOAD_FOLDER));

$Directory = new RecursiveDirectoryIterator($uploadFolderPath);
$iterator = new RecursiveIteratorIterator($Directory);

$counter = 0;
$structure = [];

/** @var SplFileInfo $file */
foreach ($iterator as $file) {
if ($file->isFile()) {
Expand All @@ -115,5 +90,4 @@ protected function getStructureOfFiles()
return $structure;
}


}
}
81 changes: 41 additions & 40 deletions Classes/Controller/MediaUploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

use Fab\MediaUpload\FileUpload\UploadManager;
use Fab\MediaUpload\Utility\UuidUtility;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Http\Stream;
use TYPO3\CMS\Core\Resource\Exception;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;

/**
Expand All @@ -23,10 +26,18 @@
class MediaUploadController extends ActionController
{

protected EventDispatcherInterface $eventDispatcher;

public function __construct(EventDispatcherInterface $eventDispatcher)
{
parent::__construct();
$this->eventDispatcher = $eventDispatcher;
}

/**
* Initialize actions. These actions are meant to be called by an logged-in FE User.
*/
public function initializeAction()
public function initializeAction(): void
{

// Perhaps it should go into a validator?
Expand All @@ -38,11 +49,11 @@ public function initializeAction()
}
} elseif (!empty($allowedFrontendGroups)) {

$isAllowed = FALSE;
$frontendGroups = GeneralUtility::trimExplode(',', $allowedFrontendGroups, TRUE);
$isAllowed = false;
$frontendGroups = GeneralUtility::trimExplode(',', $allowedFrontendGroups, true);
foreach ($frontendGroups as $frontendGroup) {
if (GeneralUtility::inList($this->getFrontendUser()->user['usergroup'], $frontendGroup)) {
$isAllowed = TRUE;
$isAllowed = true;
break;
}
}
Expand All @@ -53,17 +64,16 @@ public function initializeAction()
}
}

$this->emitBeforeHandleUploadSignal();
// Note: Signal replaced by PSR-14 events in TYPO3 v12
// You would need to create a custom event class if needed
}

/**
* Delete a file being just uploaded.
*
* @return string
*/
public function deleteAction()
public function deleteAction(): ResponseInterface
{
$folderIdentifier = GeneralUtility::_POST('qquuid');
$folderIdentifier = $this->request->getParsedBody()['qquuid'] ?? '';

$error = '';

Expand All @@ -87,19 +97,24 @@ public function deleteAction()
}

if ($error !== '') {
$this->throwStatus(404, $error);
throw new \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException('File operation failed: ' . $error, 1387123456);
}

return json_encode(['success' => true]);
$jsonResponse = json_encode(['success' => true]);

$body = new Stream('php://temp', 'rw');
$body->write($jsonResponse);

return (new Response())
->withHeader('content-type', 'application/json; charset=utf-8')
->withBody($body)
->withStatus(200);
}

/**
* Handle file upload.
*
* @param int $storageIdentifier
* @return string
*/
public function uploadAction(int $storageIdentifier): string
public function uploadAction(int $storageIdentifier): ResponseInterface
{
/** @var ResourceFactory $factory */
$factory = GeneralUtility::makeInstance(ResourceFactory::class) ;
Expand All @@ -120,36 +135,22 @@ public function uploadAction(int $storageIdentifier): string
$result = ['error' => $e->getMessage()];
}

return json_encode($result);
}
$jsonResponse = json_encode($result);

/**
* Returns an instance of the current Frontend User.
*
* @return FrontendUserAuthentication
*/
protected function getFrontendUser()
{
return $GLOBALS['TSFE']->fe_user;
}
$body = new Stream('php://temp', 'rw');
$body->write($jsonResponse);

/**
* Signal that is emitted before upload processing is called.
*
* @return void
*/
protected function emitBeforeHandleUploadSignal()
{
$this->getSignalSlotDispatcher()->dispatch(MediaUploadController::class, 'beforeHandleUpload');
return (new Response())
->withHeader('content-type', 'application/json; charset=utf-8')
->withBody($body)
->withStatus(200);
}

/**
* Get the SignalSlot dispatcher.
*
* @return Dispatcher
* Returns an instance of the current Frontend User.
*/
protected function getSignalSlotDispatcher()
protected function getFrontendUser(): FrontendUserAuthentication
{
return $this->objectManager->get(Dispatcher::class);
return $GLOBALS['TSFE']->fe_user;
}
}
17 changes: 12 additions & 5 deletions Classes/FileUpload/Base64File.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,29 @@ class Base64File extends \Fab\MediaUpload\FileUpload\UploadedFileAbstract
protected $extension;

/**
* @param array $postData
* @return \Fab\MediaUpload\FileUpload\Base64File
* @throws RuntimeException
*/
public function __construct()
public function __construct(array $postData = [])
{

// Processes the encoded image data and returns the decoded image
$encodedImage = GeneralUtility::_POST($this->inputName);
$encodedImage = $postData[$this->inputName] ?? $_POST[$this->inputName] ?? '';

if (empty($encodedImage)) {
throw new RuntimeException('No image data provided', 1469376025);
}

if (preg_match('/^data:image\/(jpg|jpeg|png)/i', $encodedImage, $matches)) {
$this->extension = $matches[1];
} else {
throw new RuntimeException('File extension is not recognized', 1469376026);
}

// Remove the mime-type header
$data = reset(array_reverse(explode('base64,', $encodedImage)));
$array = array_reverse(explode('base64,', $encodedImage));
$data = reset($array);

// Use strict mode to prevent characters from outside the base64 range
$this->image = base64_decode($data, true);
Expand All @@ -76,7 +83,7 @@ public function __construct()
* @return boolean TRUE on success
* @throws RuntimeException
*/
public function save()
public function save(): bool
{

if (is_null($this->uploadFolder)) {
Expand Down Expand Up @@ -111,7 +118,7 @@ public function getSize()
if (isset($GLOBALS['_SERVER']['CONTENT_LENGTH'])) {
return (int)$GLOBALS['_SERVER']['CONTENT_LENGTH'];
} else {
throw new RuntimeException('Getting content length is not supported.');
throw new RuntimeException('Getting content length is not supported.', 7658672952);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Classes/FileUpload/ImageOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct($storage = NULL)
* @param string $className
* @return void
*/
public function add($className)
public function add($className): void
{
$this->optimizers[] = $className;
}
Expand All @@ -68,7 +68,7 @@ public function add($className)
* @param string $className
* @return void
*/
public function remove($className)
public function remove($className): void
{
if (in_array($className, $this->optimizers)) {
$key = array_search($className, $this->optimizers);
Expand Down
6 changes: 3 additions & 3 deletions Classes/FileUpload/Optimizer/JpegExifOrient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class JpegExifOrient
* @return void
* @throws \RuntimeException
*/
public static function setOrientation($filename, $orientation)
public static function setOrientation(string $filename, int $orientation): void
{
$exif_data = array(); // Buffer
$offsetJfif = 0;
Expand Down Expand Up @@ -242,7 +242,7 @@ public static function setOrientation($filename, $orientation)
* @return integer
* @throws \RuntimeException
*/
protected static function read_1_byte($handle)
protected static function read_1_byte($handle): int
{
$c = fgetc($handle);
if ($c === FALSE) {
Expand All @@ -259,7 +259,7 @@ protected static function read_1_byte($handle)
* @return integer
* @throws \RuntimeException
*/
protected static function read_2_bytes($handle)
protected static function read_2_bytes($handle): int
{
$c1 = fgetc($handle);
if ($c1 === FALSE) {
Expand Down
Loading