‪TYPO3CMS  ‪main
FileUpdateOnlineMediaController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseFactoryInterface;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Message\StreamFactoryInterface;
30 
36 #[AsController]
38 {
39  public function ‪__construct(
40  protected readonly ‪ResourceFactory $resourceFactory,
41  protected readonly ‪OnlineMediaHelperRegistry $onlineMediaHelperRegistry,
42  protected readonly ‪PreviewService $previewService,
43  protected readonly ResponseFactoryInterface $responseFactory,
44  protected readonly StreamFactoryInterface $streamFactory
45  ) {}
46 
47  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
48  {
49  $resource = $request->getParsedBody()['resource'] ?? [];
50 
51  if (($resource['type'] ?? '') !== 'file' || !isset($resource['uid'])) {
52  return $this->‪createResponse(['success' => false], 400);
53  }
54 
55  $fileObject = null;
56  try {
57  $fileObject = $this->resourceFactory->getFileObject($resource['uid']);
58  } catch (‪FileDoesNotExistException $e) {
59  }
60 
61  if ($fileObject === null
62  || !($onlineMediaHelper = $this->onlineMediaHelperRegistry->getOnlineMediaHelper($fileObject))
63  || !$fileObject->checkActionPermission('editMeta')
64  || !$fileObject->getMetaData()->offsetExists('uid')
65  || !$this->getBackendUser()->check('tables_modify', 'sys_file_metadata')
66  ) {
67  return $this->‪createResponse(['success' => false], 400);
68  }
69 
70  try {
71  $this->previewService->updatePreviewImage($fileObject);
72  } catch (\InvalidArgumentException $e) {
73  return $this->‪createResponse(['success' => false], 400);
74  }
75 
76  // Update remaining meta data from online media helper
77  $fileObject->getMetaData()->add($onlineMediaHelper->getMetaData($fileObject))->save();
78 
79  return $this->‪createResponse(['success' => true]);
80  }
81 
82  protected function ‪createResponse(array $data = [], int $status = 200): ResponseInterface
83  {
84  return $this->responseFactory->createResponse($status)
85  ->withHeader('Content-Type', 'application/json; charset=utf-8')
86  ->withBody($this->streamFactory->createStream((string)json_encode($data)));
87  }
88 
90  {
91  return ‪$GLOBALS['BE_USER'];
92  }
93 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:27
‪TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
Definition: FileDoesNotExistException.php:21
‪TYPO3\CMS\Filelist\Controller\FileUpdateOnlineMediaController\getBackendUser
‪getBackendUser()
Definition: FileUpdateOnlineMediaController.php:89
‪TYPO3\CMS\Filelist\Controller
‪TYPO3\CMS\Filelist\Controller\FileUpdateOnlineMediaController\createResponse
‪createResponse(array $data=[], int $status=200)
Definition: FileUpdateOnlineMediaController.php:82
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Resource\OnlineMedia\Service\PreviewService
Definition: PreviewService.php:28
‪TYPO3\CMS\Filelist\Controller\FileUpdateOnlineMediaController\__construct
‪__construct(protected readonly ResourceFactory $resourceFactory, protected readonly OnlineMediaHelperRegistry $onlineMediaHelperRegistry, protected readonly PreviewService $previewService, protected readonly ResponseFactoryInterface $responseFactory, protected readonly StreamFactoryInterface $streamFactory)
Definition: FileUpdateOnlineMediaController.php:39
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Filelist\Controller\FileUpdateOnlineMediaController\handleRequest
‪handleRequest(ServerRequestInterface $request)
Definition: FileUpdateOnlineMediaController.php:47
‪TYPO3\CMS\Filelist\Controller\FileUpdateOnlineMediaController
Definition: FileUpdateOnlineMediaController.php:38
‪TYPO3\CMS\Backend\Attribute\AsController
Definition: AsController.php:25